$(document).ready(function() {        
    function GaleriaFotos(prefijo) {
        if(GaleriaFotos.numGalerias >= 0)
            GaleriaFotos.numGalerias++;
        else
            GaleriaFotos.numGalerias = 0;
        var numGaleria = GaleriaFotos.numGalerias;
        GaleriaFotos.galeriaSeleccionada = numGaleria;

        //Fotos
        var numFoto = 1;
        var fotoWidth = $("#" + prefijo + "fotos_ext").width();
        var numFotos = $("#" + prefijo + "fotos_int img").size();
        var fotosIntWidth = numFotos * fotoWidth;
        $("#" + prefijo + "fotos_int").css('width', fotosIntWidth);

        this.nextFoto = function() {
            if(numFoto < numFotos)
                (numFoto)++;
            else
                numFoto = 1;
            moveFoto();
        }

        this.previousFoto = function() {
            if(numFoto > 1)
                (numFoto)--;
            else
                numFoto = numFotos;
            moveFoto();
        }

        this.popupNextFoto = function() {
            this.nextFoto();
            popupMoveFoto();
        }

        this.popupPreviousFoto = function() {
            this.previousFoto();
            popupMoveFoto();
        }

        var moveFoto = function() {
            var pos = (numFoto - 1) * fotoWidth;
            $("#" + prefijo + "fotos_int").stop().animate({ 
                left: -pos
            }, 500);
        }

        var popupMoveFoto = function() {
            $("#popup_img").attr("src", "./img/portada/800/" + prefijo + numFoto + ".jpg");
        }

        //Popup
        $("#" + prefijo + "fotos_int a").click(function() {
            GaleriaFotos.galeriaSeleccionada = numGaleria;
            PlayTimer.stop();
            posicionarPopup();

            $("#popup_img").attr("src", "./img/portada/800/" + prefijo + numFoto + ".jpg");
            //$("#hide_content").stop().fadeIn(0, function() {
                $("#popup").fadeIn(300);
            //});
        });
    }

    function PlayTimer(galerias, interval) {
        PlayTimer.interval = interval ? interval : 1000;
        PlayTimer.playTimer = null;

        PlayTimer.playOnce = function() {
            for(var i = 0; i < galerias.length; i++)
                galerias[i].nextFoto();
        }
        
        PlayTimer.play = function() {
            if(!PlayTimer.playTimer) {
                PlayTimer.playTimer = setInterval(function() {
                    for(var i = 0; i < galerias.length; i++)
                        galerias[i].nextFoto();
                }, PlayTimer.interval);
            }
        }

        PlayTimer.stop = function() {
            if(PlayTimer.playTimer) {
                clearInterval(PlayTimer.playTimer);
                PlayTimer.playTimer = null;
            }
        }

        PlayTimer.playOrStop = function() {
            PlayTimer.playTimer ? PlayTimer.stop() : PlayTimer.play();
        }
    }

    $("#close_popup").click(function() {
        PlayTimer.play();
        $("#popup").stop().fadeOut(300, function() {
            //$("#hide_content").fadeOut(300);
        });
    });

    //Arrows
    $("#li_arrow_left_low").click(function() {
        PlayTimer.stop();
        galerias[0].previousFoto();
        PlayTimer.play();
    });
    $("#li_arrow_right_low").click(function() {
        PlayTimer.stop();
        galerias[0].nextFoto();
        PlayTimer.play();
    });
    $("#l_arrow_left_low").click(function() {
        PlayTimer.stop();
        galerias[1].previousFoto();
        PlayTimer.play();
    });
    $("#l_arrow_right_low").click(function() {
        PlayTimer.stop();
        galerias[1].nextFoto();
        PlayTimer.play();
    });
    $("#arrow_left_mid").click(function() {
        galerias[GaleriaFotos.galeriaSeleccionada].popupPreviousFoto();
    });
    $("#arrow_right_mid").click(function() {
        galerias[GaleriaFotos.galeriaSeleccionada].popupNextFoto();
    });
    
    posicionarPopup = function() {
        var hideContentWidth = $(document).width();
        var hideContentHeight = $(document).height();
        $("#hide_content").css({
            'width': hideContentWidth,
            'height': hideContentHeight
        });
        $("#popup").css({
            'width': $("#popup_img").width(),
            'height': $("#popup_img").height()
        });
        /*$("#popup").css({
            'left': (hideContentWidth - $("#popup").width()) / 2,
            'top': (hideContentHeight - $("#popup").height()) / 2
        });*/
        $("#popup").css({
            'left': $("#div_portada_int").offset().left + ($("#div_portada_int").width() - $("#popup").width()) / 2,
            'top': $("#div_portada_int").offset().top + ($("#div_portada_int").height() - $("#popup").height() - 40)
        });
    }

    /*$(window).resize(function() {
        posicionarPopup();
    });
    
    var galerias = new Array(new GaleriaFotos("li_"), new GaleriaFotos("l_"))
    var playTimer = new PlayTimer(galerias, 3500);
    
    //Pasar la primera imágen más rápido y luego continuar con la velocidad normal
    var timeout = setTimeout(function() {
      PlayTimer.playOnce();
      PlayTimer.play();
    }, 1200);*/
});
