var initList = initList || [];
initList.push(function() {

    var ini_directions = 0;
    $('.box2').each(function(index, value) {
      var dropSelector= $(value).attr('data-sg-drop-selector');
      sg.setDraggable($(value), {
        revert: true,
        dropSelector:dropSelector,
        success: function() {
            
            var $this = $(this);
            $this.appendTo("#tabs3");
            $this.css({"top": "-=185" , "left": "-=60" });   

            var drop = $this.data("hit").eq(0);

            if (!ini_directions){
                ini_directions++;
                $("#forces_game2 .directions").addClass("active");
                $("#forces_game2 .directions span").bind("click", change_directions);
                $("#tres3_2").click();
            }

            $(".box2").removeClass("active");
            $this.attr( "data-sg-selected", ("#" + drop.attr("id")) ).addClass("active");
            drop.attr("id", (drop.attr("id")+"_fill") );

            var boton = $(".start");
            var attempts = parseInt(boton.attr("data-sg-count")) + 1;
            boton.attr("data-sg-count", attempts);
            if (attempts == $("#forces_game2 .box2").length ){
                boton.bind("click", revisar).addClass("ready");
            }
            sg.sound('success');
            $(".box2.ui-draggable-dragging").bind("click", click_to_dragging);

        },
        fail: function() {
        }
      });
    });


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

        $('.ui-draggable-dragging').each(function() {
            $this = $(this);
            var correct = $this.attr("data-sg-correct").split(' ')[0];
            var correct2 = $this.attr("data-sg-correct").split(' ')[1];
            var selected = $this.attr("data-sg-selected");
            var complete = selected +  "_" + $this.attr("data-sg-direction");

            if ( (correct == complete) || (correct2 == complete) ) {
                $this.addClass("correct").unbind("click");
                num_corrects++;    
            }else{
                var elem_selected = $(selected+"_fill");

                var new_id = selected.substr(1);
                elem_selected.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").unbind("click", revisar);
            }
        });
        if (num_corrects == $("#forces_game2 .box2").length ){
            $("div[data-sg-id='btn-popup'][data-sg-key='tres3_4']").click();
            $("#forces_game2 .start").attr("data-sg-count", "0").removeClass("ready").unbind("click", revisar).addClass("finish");
            $(".box2").unbind("click").removeClass("active");
        }else
            $("div[data-sg-id='btn-popup'][data-sg-key='tres3_3']").click();

    //    $("buttom").last().bind("click", corregir).addClass("ready");
    }

    var click_to_dragging = function(){ 
        $(".box2.ui-draggable-dragging").removeClass("active");
        $(this).addClass("active");
        $("#forces_game2 .directions").addClass("active");
    }; 

    var change_directions = function(){        
        var degree = $(this).attr("id");
        $(".box2.active:not(.correct)").attr("data-sg-direction", degree);
    };
//    $("#forces_game2 .directions span").bind("click", change_directions);


    $("#forces_game2 .verify").click(function(){
        $(".box2").css("visibility", "visible");
        $(".box2.ui-draggable-dragging").remove();
        $(".drop").each(function( index ) {
            var id = $(this).attr("id");
            id = id.replace("_fill",'');
            $(this).attr("id", id);
        });
        ini_directions = 0;
        $("#forces_game2 .directions").removeClass("active");
        $("#forces_game2 .directions span").unbind("click", change_directions);
        $("#forces_game2 .start").attr("data-sg-count", "0").removeClass("ready finish").unbind("click", revisar);
    });

    $(".tab:nth-child(3)").click(function(){
        console.log(ini_directions);
        if (!ini_directions){
            $("div[data-sg-id='btn-popup'][data-sg-key='tres3_0']").click();
        }
    });
    $("div[data-sg-id='item-popup'][data-sg-key='tres3_0'] .icon_close_button").click(function(){
        $("#tres3_1").click();
    });


});