﻿function toggleTabBoxes(menuElement, display) {
    var childnodes = menuElement.getElementsByTagName("DIV");

    for (var i = 0; i < childnodes.length; i++) {
        if (display == 1) {
            childnodes[i].className = childnodes[i].className.replace(" displaynone", "");
        }
        else {
            childnodes[i].className = childnodes[i].className + " displaynone";
        }
    }
}

function toggleSelectedImage(menuElement) {
    $('.gallerytableitem').removeClass('gallerytableitemon');
    $(menuElement).parent().addClass('gallerytableitemon');
}

function Redirect(dataType, value) {
    if (value == "") {
        document.location = location.protocol + "//" + location.host + location.pathname + "#cinematek";
    }
    else if (!value.startsWith("all") && (dataType == "film" || dataType == "series" || dataType == "event")) {
        document.location = location.protocol + "//" + location.host + location.pathname + "?" + dataType + "=" + value;
    }
    else {
        document.location = location.protocol + "//" + location.host + location.pathname + "?" + dataType + "=" + value + "#cinematek";
    }
}

if (!String.prototype.startsWith) {
    String.prototype.startsWith = function(str) {
        return !this.indexOf(str);
    }
}


function ToggleSummary(imageID, textID) {
    jQuery('#' + textID).toggle('fast');
    
    var image = jQuery('#' + imageID);
    if (jQuery(image).attr("src").match("_down") != null) {
        jQuery(image).attr("src", "/layouts/images/arrow_expand_up.gif");
        //console.log('up');
    }
    else {
        jQuery(image).attr("src", "/layouts/images/arrow_expand_down.gif");
        //console.log('down');
    }
}


function ShowAddedToMitProgramBox(e, title, text, date) {
    $(".cinematek_add_box").toggle().css("z-index", "200").css("position", "absolute");
    var centerDiv = $(".cinematek_add_box");    
    if ((e.pageY == null && e.pageX == null) || (e.pageY == undefined && e.pageX == undefined))
    {
        var winH = $(window).height();
        var winW = $(window).width();
        centerDiv.css('top', winH / 2 - centerDiv.height() / 2);
        centerDiv.css('left', winW / 2 - centerDiv.width() / 2);        
    }
    else
    {
        centerDiv.css('top', e.pageY + 40);
        centerDiv.css('left', e.pageX);
    }
    $(".cinematek_add_title").html(title + " - " + date);    
    $(".cinematek_add_text").html(text);
    $(".cinematek_add_box").fadeTo(600, 1.00).fadeOut(600);
}

(function($) {
    $.fn.showtimetip = function(options) {

        var defaults = {};
        var options = $.extend({}, defaults, options);
        var $this = this;
        var tip = null;
        var activeShowtime = null;

        this.initialize = function() {
            $(this).click(function(e) {                
                var booking = GetBooking($(this).parent().attr('id').substring(3, $(this).parent().attr('id').length));
                
                //Add booking
                if (jQuery(this).hasClass('addToMyProgramme')) {
                    jQuery(this).removeClass('addToMyProgramme');
                    jQuery(this).addClass('removeFromMyProgramme');
                    jQuery(this).attr('title', "Fjern fra 'Mit Program'");
                    jQuery(this).children('img').attr('src', '/layouts/images/icon_star_rem.gif');
                    Cinemateket.AddBookingToSession(booking.id);
                    ShowAddedToMitProgramBox(e, booking.title, booking.text, booking.date); //"Tilføjet til 'Mit program'"

                    //Update favorites counter
                    var count = parseInt($('.cinematekFavorites').children('.cineText').children('a').children('#favoritesCount').html()) + 1;
                    $('.cinematekFavorites').children('.cineText').children('a').children('#favoritesCount').html(count);

                    //Update favorites list
                    if($('.cinematekFavorites').children('.topmargin3').children('div').size() < 3)
                    {
                        var bktitle = booking.title;
                        if(bktitle.length > 22) { bktitle = booking.title.substring(0,20) + '...'; }
                        $('.cinematekFavorites').children('.topmargin3').append('<div id="' + booking.id + '" class="cineSmallText">' +
                            '<a href="/Filmhuset/Cinemateket/Film.aspx?filmID=' + booking.linkid + '">' + bktitle + ' ' + booking.date.substring(0,5) + ' ' + booking.date.substring(11,16) + '</a></div>');
                    }
                }
                //Remove booking
                else {
                    jQuery(this).removeClass('removeFromMyProgramme');
                    jQuery(this).addClass('addToMyProgramme');
                    jQuery(this).attr('title', "Tilføj til 'Mit Program'");
                    jQuery(this).children('img').attr('src', '/layouts/images/icon_star.gif');
                    Cinemateket.RemoveBookingFromSession(booking.id);
                    ShowAddedToMitProgramBox(e, booking.title, booking.rem, booking.date); //"Fjernet fra 'Mit program'"

                    //Update favorites counter
                    var count = parseInt($('.cinematekFavorites').children('.cineText').children('a').children('#favoritesCount').html()) - 1;
                    $('.cinematekFavorites').children('.cineText').children('a').children('#favoritesCount').html(count);

                    //Update favorites list
                    $('.cinematekFavorites').children('.topmargin3').children('#' + booking.id).remove();
                }
                return false;
            });
        };

        return this.initialize();

    };

})(jQuery);

//(function($) {
//    $.fn.showtimeremovetip = function(options) {

//        var defaults = {};

//        var options = $.extend({}, defaults, options);
//        var $this = this;
//        var tip = null;
//        var activeShowtime = null;

//        this.initialize = function() {
//            $(this).click(function(e) {
//                var booking = GetBooking($(this).parent().attr('id').substring(3, $(this).parent().attr('id').length));                
//                ShowAddedToMitProgramBox(e, booking.title, booking.rem, booking.date);
//                Cinemateket.RemoveBookingFromSession(booking.id);
//                return true;
//            });
//        };

//        return this.initialize();

//    };

//})(jQuery);
