﻿
var PrincipalScrollConfig = {
    isItemHover: false,
    itemHoverIndex: 0
};

$(document).ready(function () {

    var principalItemSeleccionadoIndex = 0;

    principalInicializar();



    function principalInicializar() {

        var bloque1Config = {
            url: '/es/bloque/1-4.json',
            templateSelector: '#tmplBloque1',
            destinoSelector: '.desneaScroll_Lista22 table tbody tr',
            cargadoFuncion: 'scrollInicializar();'
        };

        DESNEA.Util.templateArmar(bloque1Config);

        //Insertar en contenedor 

        //Formateando tamaño listado
        //        $("#bloque1 img").each(function () {
        //            //                imagenUrl = $(this).attr("src");
        //            //                imagenUrl = imagenUrl.toString().replace('130x130', '130x130');
        //            //                $(this).attr("src", imagenUrl);
        //        });

        //        //Formateando tamaño visualizar dentro del contenedor
        //        $("#bloque1 .enlace").each(function () {
        //            enlaceUrl = $(this).attr("href");
        //            enlaceUrl = enlaceUrl.toString().replace('130x130', '640x640');
        //            $(this).attr("href", enlaceUrl);
        //        });





        //        var contenidoHtml = $("#bloque1").html();

        //        $(".desneaScroll_Lista ul").html(contenidoHtml);

        //            $(".desneaScroll_Lista ul li .t").each(function () {
        //                var titulo = $(this).text();

        //                $(this).parents(".d").attr("title") = titulo;
        //            });




        //            var i = 0;
        //            $("#bloque1 .t").each(function (indice, valor) {
        //                i = indice + 1;
        //                $(this).addClass(String.format("principalTitulo{0}", i.toString()));
        //                $(this).find("a").attr("href", "#")
        //            });



        //            //aleatorio
        //            setInterval(function () {
        //                principalReiniciar();
        //                principalRandom();
        //            }, 5000);

    }

    function principalRandom() {

        var tamano = $(".desneaScroll_Lista22 table tbody tr td").size();
        var random = Math.floor(Math.random() * tamano);
        principalItemSeleccionadoIndex = random;

        principalSeleccionar(principalItemSeleccionadoIndex);

    }

    function principalSeleccionar(index) {
        var item = $(".desneaScroll_Lista22 table tbody tr td").get(index);
        $(item).click();
    }

    function principalSiguiente() {

        if (PrincipalScrollConfig.isItemHover) { 
            return
        }

        var tamano = $(".desneaScroll_Lista22 table tbody tr td").size();

        if (principalItemSeleccionadoIndex == (tamano - 1)) {
            principalItemSeleccionadoIndex = 0;
        } else {
            principalItemSeleccionadoIndex++;
        }

        principalSeleccionar(principalItemSeleccionadoIndex);

    }

    principalRandom();

    setInterval(function () {
        principalSiguiente();
    }, 7000);


});                            //Close Function


function scrollInicializar() {

    //Visualizar descripcion
    $(".desneaScroll_Contenedor .desc").show(); //Visualizar
    $(".desneaScroll_Contenedor .d").animate({ opacity: 0.75 }, 1); //Opacity

    //Eventos para lista de articulos
    //$(".desneaScroll_Lista ul li:first").addClass('active');

    $(".desneaScroll_Lista22 table tbody tr td").click(function () {
        //Contenido
        var imgAlt = $(this).find('img').attr("alt");
        var imgTitle = $(this).find('a').attr("href");
        var imgDesc = $(this).find('.d').html();
        var imgDescHeight = $(".desneaScroll_Contenedor").find('.d').height(); //calcular el alto de la descripcion	

        if ($(this).is(".active")) {
            return false;
        } else {
            //Asignando valores				
            $(".desneaScroll_Contenedor .d").animate({ opacity: 0, marginBottom: -imgDescHeight }, 250, function () {
                $(".desneaScroll_Contenedor .d").html(imgDesc).animate({ opacity: 0.75, marginBottom: "0" }, 250);
                $(".desneaScroll_Contenedor img").attr({ src: imgTitle, alt: imgAlt });
            });
        }

        $(".desneaScroll_Lista22 table tbody tr td").removeClass('active'); //Remove class of 'active' on all lists
        $(this).addClass('active');  //add class of 'active' on this list only
        return false;

    }).hover(function () {

        PrincipalScrollConfig.isItemHover = true;
        $(this).addClass('hover');
        
    }, function () {
    
        PrincipalScrollConfig.isItemHover = false;
        $(this).removeClass('hover');        

    });

    //Toggle Teaser
    $("a.collapse").click(function () {
        $(".desneaScroll_Contenedor .d").slideToggle();
        $("a.collapse").toggleClass("show");
    });
}


//TABS
$(document).ready(function () {

    
    // Cuando el enlace sea clickeado
    $("a.tab").click(function () {

        // Apaga el Activo
        $(".activo").removeClass("activo");

        // Cambia Activo  
        $(this).addClass("activo");

        // Despliega, escondiendo
        $(".contenidoTab").hide('slow');

        // Muestra el contenido, obteniendo del atributo title del enlace (p.e. contenido_1), y muestra con show el contenido
        var muestra = $(this).attr("title");
        $("#" + muestra).show('slow');

    });

});


function ctesaldiaImagenDimensiones(enlace, dimensionAnterior, dimensionNueva) {
    return enlace.replace(dimensionAnterior, dimensionNueva);
}
