var initList = initList || [];
initList.push(function() {
    //SLIDER_V2 TAB1
    $(".content.tab1 .buttons").click(function() {
        if ($(this).is(".prev")) {
            var current = $('.content_nav.tab1 .time_line .item.active');
            $('.content_nav.tab1 .time_line .item').removeClass('active');
            $('.content_nav.tab1 .time_line .item img').removeClass('zoomIn');
            current.prev().addClass('active').find('img').addClass('zoomIn');            
            //$('.content_nav.tab1 .time_line div:last').prependTo().addClass('active')
            $('.content_nav.tab1 .time_line div:last')
                .prependTo('.content_nav.tab1 .time_line');
        } else if ($(this).is(".next")) {
            var current = $('.content_nav.tab1 .time_line .item.active');
            $('.content_nav.tab1 .time_line .item').removeClass('active');
            $('.content_nav.tab1 .time_line .item img').removeClass('zoomIn');
            current.next().addClass('active').find('img').addClass('zoomIn');
            //$('.content_nav.tab1 .time_line div:first').next().next().addClass('active');
            $('.content_nav.tab1 .time_line div:first')
                .next()
                .end()
                .appendTo('.content_nav.tab1 .time_line');
        }
    });

    //SLIDER TAB2
    $(".slider div:gt(0)").hide();

    $(".content.tab2 .control").click(function() {
        if ($(this).is(".left")) {
            $('.slider  div:first').hide();
            $('.slider  div:last')
                .fadeIn(10)
                .prependTo('.slider');
        } else if ($(this).is(".right")) {
            $('.slider  div:first')
                .hide()
                .next()
                .fadeIn(10)
                .end()
                .appendTo('.slider');
        }
    });
    setInterval(function() {
        $('.slider  div:first')
            .hide()
            .next()
            .fadeIn(1000)
            .end()
            .appendTo('.slider');
    }, 4000);

    //DRAG AND DROP TAB2
    $('.drags').find('.item').each(function(index, value) {
        var dropSelector = $(value).attr('data-sg-drop-selector');
        sg.setDraggable($(value), {
            revert: true,
            dropSelector: dropSelector,
            success: function() {

                var $this = $(this);
                var drop = $this.data("hit").eq(0);
                
                $this.attr("data-sg-selected", ("#" + drop.attr("id")));
                drop.attr("id", (drop.attr("id") + "_fill"));

                var boton = $(".btnCheck");
                var attempts = parseInt(boton.attr("data-sg-count")) + 1;
                boton.attr("data-sg-count", attempts);
                if (attempts == 4) {
                    boton.bind("click", revisar);
                    boton.addClass("ready shake btn-hover");
                    boton.css('cursor', 'pointer');
                }
                sg.sound('success');
            },
            fail: function() {}
        });
    });


    function revisar() {
        var boton = $(".btnCheck");
        var num_corrects = 0;

        $('.ui-draggable-dragging').each(function() {
            $this = $(this);
            var correct = $this.attr("data-sg-correct").split(',');
            var selected = $this.attr("data-sg-selected");

            if (correct.indexOf(selected) != -1) {
                $this.addClass("correct");
                num_corrects++;
            } else {
                var elem_selected = $(selected + "_fill");
                var new_id = selected.substr(1, 5);
                elem_selected.text("").attr("id", new_id);

                var id_drag = "#" + $this.attr("id");
                $(id_drag).css("visibility", "visible");
                $this.css("display", "none").remove();
                var attempts = parseInt(boton.attr("data-sg-count")) - 1;
                boton.attr("data-sg-count", attempts).removeClass("ready shake btn-hover").unbind("click", revisar);
                boton.css('cursor', 'default');
            }
        });

        if (num_corrects == 4) {
            boton.unbind("click", revisar);
            boton.css('cursor', 'default');
            boton.removeClass('btn-hover').addClass('completed');
            sg.sound('claps');
            $('.btn_popup_tab2').addClass('ready shake btn-hover');
            $('.block_popup').remove();
        }else{
            sg.sound('error');
        }

    }

    $('[data-sg-id=elem-tab]')
        .bind("easytabs:before", function(e, $targetPanel, settings) {
            /*Delete all _fill to allow restart the draggable element*/
            $("div[id$='_fill']").each(function(index, value) {
                var $this = $(this);
                var attrId = $this.attr('id').substr(0, 5);
                $this.attr("id", attrId);
            });
            $('.btnCheck').attr('data-sg-count', 0).removeClass('completed').removeClass('ready shake');
            if($('.btn_popup_tab2').prev().hasClass('block_popup')){
                return;
            }else{
                var block_div=$('<div>').addClass('block_popup');
                $('.btn_popup_tab2').removeClass('ready shake').before(block_div);
            }
        }).easytabs();

    //TAB3 TIMELINE
    var buttons1 = $(".content_nav.tab3 .buttons");
    var sections1 = $(".content_nav.tab3 .time_line");
    var stage1 = 1;
    var num_sections1 = 3; //sections.children().length;
    var width_sections1 = 1245; //1014;//parseInt (sections.children().css("width")) + parseInt(sections.children().css("marginRight") )  + parseInt(sections.children().css("marginLeft") );
    var time_factor1 = 1.5 * width_sections1;

    buttons1.click(function() {
        $(".content_nav.tab3 > .controls > .btn").removeClass("active");
        var variation;

        if ($(this).hasClass("prev")) {
            variation = "+=";
            stage1--;
        } else {
            variation = "-=";
            stage1++;
        }
        
        $(this).fadeOut(50,
            function() {
                if ($(this).hasClass("prev")){
                    $('.content_nav.tab3 .buttons.next').fadeOut();
                }else{
                    $('.content_nav.tab3 .buttons.prev').fadeOut();
                }
                sections1.animate({
                    marginLeft: variation + width_sections1
                }, time_factor1, function() {
                    if (stage1 < 3) buttons1.last().fadeIn();
                    if (stage1 < 3 && stage1 > 1) buttons1.first().fadeIn();
                    if (stage1 == 3) buttons1.first().fadeIn();
                    $(".content_nav.tab3 > .controls > span:nth-child(" + stage1 + ")").addClass("active");
                });
            });
    });

    $(".content_nav.tab3 .controls .btn").click(function() {
        if ($(this).hasClass('active')) return;
        var index = $(this).index();
        var variation = index * width_sections1 * -1;

        stage1 = index + 1;
        $(".content_nav.tab3 > .controls > .btn").removeClass("active");
        buttons1.fadeOut(50,
            function() {
                sections1.animate({
                    marginLeft: variation
                }, time_factor1, function() {
                    if (stage1 < 3) buttons1.last().fadeIn();
                    if (stage1 < 3 && stage1 > 1) buttons1.first().fadeIn();
                    if (stage1 == 3) buttons1.first().fadeIn();
                    $(".content_nav.tab3 > .controls > span:nth-child(" + stage1 + ")").addClass("active");
                });
            });
    });

});