$(function () {
                $(".button").unbind("click").click(function () {
                                hideInterface();
                                showImage($(this));
                                return false;
                        });
                $("#mostrar_creditos").mouseover(function () {
                                $("#creditos").fadeIn()
                        }).mouseout(function () {
                                $("#creditos").fadeOut()
                        });
                $("#mas_info").attr("href", "javascript:;").click(function () {
                                if ($(".texto").hasClass("showText")) {
                                        $(".texto").fadeOut("normal", function () {
                                                        $(this).switchClass("showText", "hideText");
                                                });
                                        $(this).find("img").attr("src", "img/mas.png");
                                } else {
                                        $(".texto").fadeIn("normal", function () {
                                                        $(this).switchClass("hideText", "showText");
                                                });
                                        $(this).find("img").attr("src", "img/menos.png");
                                }
                        });
                $(".texto").addClass("hideText")
                $("#tScroll").jScrollPane({
                                autoReinitialise : true,
                                verticalGutter : 18
                        });
                $("#menu_action").click(function () {
                                showMenu();
                        });
                $("#menu_principal li a").addClass("showItem").click(function () {
                                var uri = $(this).attr("href");
                                hideMenu(uri);
                                return false;
                        });
                if ($("body").hasClass("home")) {
                        $(".cabecera").switchClass("cabecera", "cabecera_preview");
                        $(".logo").switchClass("logo", "logo_preview");
                        $("#menu_principal").addClass("hideMenu");
                        $(window).load(function () {
                                        showMenu();
										/***** NAVIDAD *****/
										$(document).trigger("load_nadal");
										console.log = "hello";
                                        $(".lateral_iz .destacado_menu").effect("slide", {}, 500);

                                });
                } else
                        $("#menu_principal").addClass("hideMenu");
                $("#menu_content").css("position", "relative").css("overflow", "hidden");
                function hideMenu(uri) {
                        var h = $("#menu_principal");
                        if (h.hasClass("showMenu")) {
                                h.switchClass("showMenu", "hideMenu", 1000);
                                setTimeout(function () {
                                                showMenuWord(uri);
                                        }, 1000);
                        } else {
                                $("#menu_action").fadeOut();
                                h.switchClass("hideMenu", "showMenu", 1000);
                        }
                }
                function showMenuWord(uri) {
                        if ($("body").hasClass("home")) {
                                $(".cabecera_preview").switchClass("cabecera_preview", "cabecera", 1000);
                                $(".logo_preview").switchClass("logo_preview", "logo", 1000, function () {
                                                $("#menu_action").fadeIn(function () {
                                                                document.location = uri;
                                                        });
                                        });
                        } else {
                                $("#menu_action").fadeIn(function () {
                                                document.location = uri;
                                        });
                        }
                }
                function showMenu() {
                        hideMenu("");
                }
                function showImage(ele) {
                        $(".button").each(function (ind) {
                                        $(this).find("img").removeClass("activo");
                                });
                        ele.find("img").addClass("activo");
                        var src = ele.find("img").attr("src").replace("pic/", "");
                        var nImage = new Image();
                        nImage.src = src;
                        $(nImage).load(function () {
                                        var ancho_p = $(window).width();
                                        var alto_p = $(window).height();
                                        var alto_V = nImage.height;
                                        var ancho_V = nImage.width;
                                        $("#img_fondo").height("auto").width("auto").attr("style", "");
                                        if (ancho_V > ancho_p) {
                                                var newAncho = ancho_V;
                                                var newAlto = Math.round((ancho_V * alto_p) / ancho_p);
                                                if (newAlto > alto_V)
                                                        $("#img_fondo").height(alto_p).width("auto");
                                                else
                                                        $("#img_fondo").width(ancho_p).height("auto");
                                        } else {
                                                $("#img_fondo").width(ancho_p);
                                        }
                                        $("#img_fondo").attr("src", src);
                                });
                }
                function refreshSize() {
                        var ancho_p = $(window).width();
                        var alto_p = $(window).height();
                        var alto_V = $("#img_fondo").height();
                        var ancho_V = $("#img_fondo").width();
                        $("#img_fondo").height("auto").width("auto").attr("style", "");
                        if (ancho_V > ancho_p) {
                                var newAncho = ancho_V;
                                var newAlto = Math.round((ancho_V * alto_p) / ancho_p);
                                if (newAlto > alto_V)
                                        $("#img_fondo").height(alto_p).width("auto");
                                else
                                        $("#img_fondo").width(ancho_p).height("auto");
                        } else {
                                $("#img_fondo").width(ancho_p);
                        }
                }
                $(window).resize(function () {
                                refreshSize();
                        });
                $(window).load(function () {
                                refreshSize();
                        });
                function hideInterface() {
						$(".social").fadeOut(500);
                        $("#img_fondo").data("saveImage", $("#img_fondo").attr("src"));
                        $(".lateral_iz .destacado_menu").effect("drop", {
                                        "direction" : "left"
                                }, 500, function () {
                                        if ($("#menu_principal").hasClass("showMenu")) {
                                                $("#menu_principal").switchClass("showMenu", "hideMenu", 1000);
                                        }
                                        if ($(".texto").length > 0) {
                                                $(".texto").fadeOut('normal', function () {
                                                                hideInterfaceFX();
                                                                $(this).dequeue();
                                                        });
                                        } else {
                                                hideInterfaceFX();
                                        }
                                });
                        $("#img_fondo").click(function () {
                                        showInterface();
                                })
                        $(".button").unbind("click").bind("click", function () {
                                        var ele = $(this);
                                        showImage(ele)
                                });
                }
                function hideInterfaceFX() {
                        $(".idioma").fadeOut();
                        $(".mas_info").fadeOut();
                        $(".titulo_sec").fadeOut();
                        $(".cabecera").switchClass("cabecera", "cabecera_preview", 1000, callback);
                        $(".presentacion").switchClass("presentacion", "presentacion_preview", 1000)
                        $(".pics").switchClass("pics", "pics_preview", 1000); ;
                        $(".logo").switchClass("logo", "logo_preview", 1000, function () {
                                        $(".presentacion *").fadeOut();
                                        $(this).dequeue();
                                });
                        $(".desplega_menu").fadeOut();
                }
                function showInterface() {
                        $("#bg").css("zIndex", -1);
                        $(".button").unbind("click").bind("click", function () {
                                        hideInterface();
                                        showImage($(this));
                                });
                        $(".cabecera_preview").switchClass("cabecera_preview", "cabecera", 1000);
                        $(".logo_preview").switchClass("logo_preview", "logo", 1000);
                        $(".pics_preview").switchClass("pics_preview", "pics", 1000);
                        $(".desplega_menu").fadeIn();
                        $(".presentacion_preview").switchClass("presentacion_preview", "presentacion",
1000, function () {
                                        $(".presentacion *").fadeIn(function () {
                                                        $(".mas_info").fadeIn(function () {
                                                                        if (!$(".texto").hasClass("hideText")) $(".texto").fadeIn();
                                                                        $(".idioma").fadeIn();
                                                                        $(".lateral_iz .destacado_menu").effect("slide", {}, 500);
                                                                        $("#img_fondo").unbind("click").attr("src",
$("#img_fondo").data("saveImage"));
                                                                });
                                                });
                                        $(".titulo_sec").fadeIn();
                                        $(".social").delay(1000).fadeIn(1000);
                                });
                }
                function callback() {
                        $(".cabecera_preview").css("position", "relative").css("zIndex", 200);
                        $("#bg").css("zIndex", 99);
                };
        });
