var sliderStep = 3;
$(document).ready(function() {
    /* fix png with gifs */
    $(document).pngFix();
    /* make all esternal links open in new window */
    $('a[href^="http://"]').attr("target", "_blank");
    /* setup animation duration in millisecconds */
    var animationDuration = 150;
    /* shows slider bubble effect */
    $("#slider-programa div.logos ul li a")
        .removeClass("showLink")
        .mouseover(function() {
            $(this).css("background-position", "center -47px");
            $(this).stop().animate({ height: "86px" }, { duration: animationDuration });
        })
        .mouseout(function() {
            $(this).stop().animate({ height: "47px" }, animationDuration, "linear", function() { $(this).css("background-position", "center top"); });
        })
        .click(function(eventArgs) {
            //eventArgs.preventDefault();
        });
    /* shows slider control functions */
    var currentSliderStep = 0;
    var sliderItemCount = $("#slider-programa .logos ul li").length;
    $("#slider-programa #previous")
        .click(function(eventArgs) {
            eventArgs.preventDefault();

            var nextSliderStep = currentSliderStep - sliderStep;

            if (currentSliderStep == 0) {
                nextSliderStep = sliderItemCount - sliderStep;
            }
            if (nextSliderStep < 0) {
                nextSliderStep = 0;
            }
            $("#slider-programa .logos").scrollTo($("#slider-programa .logos ul li")[nextSliderStep], 300);
            currentSliderStep = nextSliderStep;
        });
    $("#slider-programa #next")
        .click(function(eventArgs) {
            eventArgs.preventDefault();
            var nextSliderStep = currentSliderStep + sliderStep;

            if ((currentSliderStep == sliderItemCount) || (currentSliderStep >= sliderItemCount - sliderStep)) {
                nextSliderStep = 0;
            }
            if (nextSliderStep >= sliderItemCount - sliderStep) {
                nextSliderStep = sliderItemCount - sliderStep;
            }
            $("#slider-programa .logos").scrollTo($("#slider-programa .logos ul li")[nextSliderStep], 300);
            currentSliderStep = nextSliderStep;
        });
    /* categories menu control functions */
    var selectedCategory;
    var categoriesAnimation = false;
    $("#menu-categorias ul#swf-menu li a")
        .click(function(eventArgs) {
            eventArgs.preventDefault();
            if (categoriesAnimation) { return; }

            if (selectedCategory == undefined) {
                selectedCategory = $("#menu-categorias ul#swf-menu li a.selected");
            }

            selectedCategory.removeClass("selected");
            $(this).addClass("selected");
            var selectedItem = $(this).attr("rel");

            categoriesAnimation = true;
            $("#menu-categorias div.content-categorias ." + $(selectedCategory).attr("rel")).fadeOut(animationDuration, function() {
                $("#menu-categorias div.content-categorias ." + selectedItem).fadeIn(animationDuration, function() {
                    categoriesAnimation = false;
                });
            });
            selectedCategory = $(this);
        });
    $("#menu-categorias div.content-categorias ul#contolador-categorias li a.avanzar")
        .click(function(eventArgs) {
            eventArgs.preventDefault();
            if (categoriesAnimation) { return; }

            if (selectedCategory == undefined) {
                selectedCategory = $("#menu-categorias ul#swf-menu li a.selected");
            }
            selectedCategory.removeClass("selected");

            var nextItemIndex = 0;
            $.each($("#menu-categorias ul#swf-menu li a"), function(index, item) {
                if ($(item).attr("rel") == selectedCategory.attr("rel")) {
                    nextItemIndex = index + 1;
                }
            });
            if (nextItemIndex > $("#menu-categorias ul#swf-menu li a").length - 1) { nextItemIndex = 0; }

            var nextItem = $("#menu-categorias ul#swf-menu li a")[nextItemIndex];
            $(nextItem).addClass("selected");

            categoriesAnimation = true;
            $("#menu-categorias div.content-categorias ." + $(selectedCategory).attr("rel")).fadeOut(animationDuration, function() {
                $("#menu-categorias div.content-categorias ." + $(nextItem).attr("rel")).fadeIn(animationDuration, function() {
                    categoriesAnimation = false;
                });
            });
            selectedCategory = $(nextItem);
        });
    $("#menu-categorias div.content-categorias ul#contolador-categorias li a.retroceder")
        .click(function(eventArgs) {
            eventArgs.preventDefault();
            if (categoriesAnimation) { return; }

            if (selectedCategory == undefined) {
                selectedCategory = $("#menu-categorias ul#swf-menu li a.selected");
            }
            selectedCategory.removeClass("selected");

            var previousItemIndex = 0;
            $.each($("#menu-categorias ul#swf-menu li a"), function(index, item) {
                if ($(item).attr("rel") == selectedCategory.attr("rel")) {
                    previousItemIndex = index - 1;
                }
            });
            if (previousItemIndex < 0) { previousItemIndex = $("#menu-categorias ul#swf-menu li a").length - 1; }

            var previousItem = $("#menu-categorias ul#swf-menu li a")[previousItemIndex];
            $(previousItem).addClass("selected");

            categoriesAnimation = true;
            $("#menu-categorias div.content-categorias ." + $(selectedCategory).attr("rel")).fadeOut(animationDuration, function() {
                $("#menu-categorias div.content-categorias ." + $(previousItem).attr("rel")).fadeIn(animationDuration, function() {
                    categoriesAnimation = false;
                });
            });
            selectedCategory = $(previousItem);
        });
    /* home page media player control */
    $("#contolador-player li a")
        .click(function(eventArgs) {
            loadMedia($(this).attr("rel"));
            //$("#container .content .aprendamos").fadeOut(animationDuration, function() {
            //$("#container .content .aprendamos").fadeIn(animationDuration);
            //});
            eventArgs.preventDefault();
        });
    /* prograaming tooltip control */
    $("#programacion a")
        .click(function(eventArgs) {
            eventArgs.preventDefault();
            if ($("#aprendamos-desplegable").css("display") == "none") {
                $("#aprendamos-desplegable").fadeIn("slow");
            }
            else {
                $("#aprendamos-desplegable").fadeOut("slow");
            }
        });
    $("#aprendamos-desplegable .close-pop a")
        .click(function(eventArgs) {
            eventArgs.preventDefault();
            $("#aprendamos-desplegable").fadeOut("slow");
        });
    /* parents tips control */
    $("#tips .tips-header a")
        .click(function(eventArgs) {
            eventArgs.preventDefault();
            if ($("#tips").css("height") == "213px") {
                $("#tips").stop().animate({ height: "48px" }, { duration: animationDuration * 2 });
            }
            else {
                $("#tips").stop().animate({ height: "213px" }, { duration: animationDuration * 2 });
            }
        });
    /* contact popup control */
    $("a.contacto")
        .click(function(eventArgs) {
            eventArgs.preventDefault();
            openMyModal($(this).attr("href"), 500, 378);
        });
    /* suggestions popup control */
    $("a.sugerencia")
        .click(function(eventArgs) {
            eventArgs.preventDefault();
            openMyModal($(this).attr("href"), 500, 378);
        });
    /* homepage recommend popup control */
    $("div.recomendar a")
        .click(function(eventArgs) {
            eventArgs.preventDefault();
            openMyModal($(this).attr("href"), 500, 449);
        });
    /* internal recommend popup control */
    $("div.categoria-recomendar a")
        .click(function(eventArgs) {
            eventArgs.preventDefault();
            openMyModal($(this).attr("href"), 500, 449);
        });
    /* shows thumb over control */
    $(".content-programas .list-programas li a")
        .mouseover(function() {
            //$(this).children("img").css("top", "-52px");
        })
        .mouseout(function() {
            //$(this).children("img").css("top", "0");
        })
        .click(function(eventArgs) {
            eventArgs.preventDefault();
            loadMedia($(this).attr("rel"));
        });
    /* default behaviour for programming tooltip */
    if ($("#aprendamos-desplegable .close-pop a").length > 0) {
        if ($.cookie("programmingToolTip") == null) {
            $.cookie("programmingToolTip", 'true');
            $("#aprendamos-desplegable").fadeIn("slow");
        }
    }
    //$.preloadCssImages({ simultaneousCacheLoading: 1 });
});
function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    } else {
        return document[movieName];
    }
}
function loadMedia(str) {
    thisMovie("video").load(str);
}

var modalWindow = {
    parent: "body",
    windowId: null,
    content: null,
    width: null,
    height: null,
    source: null,
    close: function() {
        $(".modal-window").remove();
        $(".modal-overlay").remove();
    },
    open: function() {

        var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
        var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

        if (jQuery.browser.msie && (ie55 || ie6)) {
            window.open(this.source, "", "width=" + this.width + ", height=" + this.height);
        }
        else {
            var modal = "";
            modal += "<div class=\"modal-overlay\"></div>";
            modal += "<div id=\"" + this.windowId + "\" class=\"modal-window\" style=\"width:" + this.width + "px; height:" + this.height + "px; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px;\">";
            modal += this.content;
            modal += "</div>";

            $(this.parent).append(modal);

            $(".modal-window").append("<a class=\"close-window\"></a>");
            $(".close-window").click(function() { modalWindow.close(); });
            $(".modal-overlay").click(function() { modalWindow.close(); });
        }
    }
};
var openMyModal = function(source, width, height) {
    modalWindow.windowId = "myModal";
    modalWindow.width = width;
    modalWindow.height = height;
    modalWindow.source = source;
    modalWindow.content = "<iframe width='" + width + "' height='" + height + "' frameborder='0' scrolling='no' allowtransparency='true' src='" + source + "'></iframe>";
    modalWindow.open();
};
