var indexCarga=0;
var arrayImagenes=[];
function inicio(){
    mouseoversSeries();

        var j=0;
        $("#bandaInferior li.mostrar").each(function(){
            var yo=$(this);
            var idtabla=yo.attr("idtabla");
            var i=yo.find("a").attr("imagen");
            yo.find("a").click(function(e){e.preventDefault();})
            var im=new Image();
            $(im).load(function(){
                $(this).addClass("relativo");
                yo.append($(this));
                $("#debug").append("<div>"+idtabla+"</div>");
            }).attr("src",i).css("opacity",(j>0)?1:1);
            yo.attr("j",j).click(function(){
               var dest=$("#centro img[idtabla="+idtabla+"]");
               if(dest.length==1){
                   var indexFoto=parseInt($(dest).attr("index"));
                   $("#centro").cycle(indexFoto);
               }
               
            });
            j++;
        });//fin del each
        cargaImagen();////////////
        var pos=0;
        var indexNav=0;
        var arrNav=[];
        nav();       
        function nav(){
            var i=0;
            var fijo=1305;
            $(".btnNext").unbind("click").click(function(){
                i=i+fijo;
                recorreBuena();
                setTimeout(function(){                   
                    var dest=$("li.sePasa").eq(0);
                    if(dest.length==1){
                        mueve($(dest),function(){recorreBuena();});
                    }else{
                        i=0
                        mueve($("li.mostrar").eq(0),function(){recorreBuena();});
                    }
                },100);
            });
            $(".btnPrev").unbind("click").click(function(){
                if(i>0){
                 i=i-fijo;
                }
                recorreBuena();
                setTimeout(function(){
                    var dest=$("li.sePasa").eq(0);
                    if(dest.length==1){
                        mueve($(dest),function(){recorreBuena();});
                    }else{
                        i=0
                        mueve($("li.mostrar").eq(0),function(){recorreBuena();});
                    }
                },100);
            });
            function recorreBuena(){
                $("#debug").empty().html(i);
                $(".sePasa").removeClass("sePasa");
                $("div.aux").remove();
                $("#bandaInferior li.mostrar").each(function(){
                    var yo=$(this);
                    yo.find(".virtual").remove();
                    var off=yo.position();
                    var w=yo.find("img").attr("width");
                    var tot=off.left+w-i;
                    var txt=(tot>0)?"se pasa":off.left;
                    yo.prepend("<div class='aux'>"+txt+"/"+tot+"</div>");
                    if(tot>0){
                        yo.find("div.aux").addClass("sePasa");
                        yo.addClass("sePasa");
                    }else{
                        yo.find("div.aux").removeClass("sePasa");
                    }
                });
            }//fin de recorreBuena
        }//fin de la función nav
        function mueve(dest,callback){
            $("#bandaInferior").scrollTo(dest,800,function(){
                callback($(this));
            });
        }
}
function menuIzquierda(){
        var ancho=$(document).width();
        //var ancho=$(document).width();
        var profiles ={window800:	{height:800,		width:1400,		status:1}};
        //$(".popupwindow").popupwindow(profiles);
        var seleccionados=$("li.thumb").not(".oculto");
        var total=seleccionados.length;
        arrayImagenes=[];
        var anchoCapa=$("#menu .capa-2").width();
        //alert(anchoCapa)
        $("#menu .capa-1").css("width",anchoCapa+"px");
        var altoCapa=$("#menu").height();
        //var altoCapa=300;
        $("#menu .capa-1").css("height",altoCapa+"px"); 
        if(home){
            $("#menu .capa-1").fadeTo('slow',0.4);
        }
        $("#debug").append($("li.mostrar").length);
        $("#bandaInferior").scrollTo($("li.mostrar").eq(0),100,function(){})
        $(".scroll").click(function(){
           $("#bandaInferior").scrollTo($("li.mostrar").eq(10),800,function(){})
        });
}
function mouseoversSeries(){
    //listaSeries
    $(".listaSeries li[id_padre=0]").mouseover(function(){
        $(".listaSeries li ul").hide();
        $(this).find("ul").show();
    })
}
function texto(){
    /*$("#derecha").unbind("click").click(function(){
       $("#centro").animate({width:'500px'});
       $("#centro").find("img").each(function(){
           var x=$(this).attr("width");
           var y=$(this).attr("height");
           var coef=500/800;
           $(this).attr("width",Math.ceil(x*coef));
           $(this).attr("height",Math.ceil(y*coef));
       })
       $(this).css("border","0px solid black").animate({width:"500px"})
    });*/
    $(".botoneraTexto button").click(function(){
        var rel=$(this).attr("rel");
        var signo=(rel=="1")?"-":"+";
        var dist=45;
        var veloc=300;
        $("#wrapper-texto").scrollTo({top:signo+'='+dist+'px',left:'0px'},veloc);
    })
}
function cargaImagen(){
        var li=$("li.thumb").not(".oculto").eq(indexCarga);
        var img=$(li).find("a").attr("href");
        var grande=$(li).attr("grande");
        var idtabla=$(li).attr("idtabla");
        var i=new Image();
        $(i).load(function(){
            if($(li).find("img").length==1){
              $(li).find("img").fadeTo("slow",1);
            }else{
              //$(li).fadeTo("slow",1);
            }
            $(this).click(function(){
                //alert($(this).attr("idtabla"))
            });
                var div=document.createElement('div');
                //$(this).css("display","none");
                $(this).addClass("relativo");
                var span=document.createElement('span');
                //$(span).addClass("absolutoIzquierda").html('fasd'+idtabla).css("z-index",100).css("background","white");
                $(div).append($(this)); 
                if(idtabla!=$("#portadaSerie").attr("rel")){
                   arrayImagenes.push($(div));
                }
                $("#counter").html(indexCarga+"/"+$("li.thumb").not(".oculto").length);
                if(indexCarga<$("li.thumb").not(".oculto").length-1){
                        indexCarga++;
                        cargaImagen();
                }
                if(indexCarga==$("li.thumb").not(".oculto").length-1){
                        $("#counter").html("");
                        inyectaCarrusel();
                        $("#centro").cycle({

                        });
                }
        }).attr("src",grande).attr("idtabla",idtabla).attr("index",indexCarga);//"../phpthumb/phpThumb.php?src="+img+"&w="+$("#centro").width()
}
function inyectaCarrusel(){
    $.each(arrayImagenes,function(n,i){
        $("#centro").append(i);
    });
}


