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

    $('.angles').find('.angle').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 == 14) {
                boton.bind("click", revisar).addClass("ready btn-hover");
                boton.css('cursor','pointer');
            }
            sg.sound('success-low');
        },
        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");
            var selected = $this.attr("data-sg-selected");
            console.log(correct + '--' + selected);
            
            if (correct == selected){
                console.log('if_equal');
                $this.addClass("correct").removeClass('btn-hover');
                sg.sound('success');
                num_corrects++;
                if(selected=="#drop2"){
                    var idCorrect = $(this).attr("id");
                    if(idCorrect=="drag6"){
                        $('#drag17').remove();
                    }else{
                        $('#drag6').remove();
                    }
                }
                if(selected=="#drop3"){
                    var idCorrect = $(this).attr("id");
                    if(idCorrect=="drag4"){
                        $('#drag14').remove();
                    }else{
                        $('#drag4').remove();
                    }
                }
                if(selected=="#drop5"){
                    var idCorrect = $(this).attr("id");
                    if(idCorrect=="drag5"){
                        $('#drag8').remove();
                    }else{
                        $('#drag5').remove();
                    }
                }  
                console.log($this); 
                console.log(num_corrects);
            }else{
                console.log('else_different');
                var elem_selected = $(selected+"_fill");
                var new_id = selected.replace(/_fill/g,'').replace(/#/g,'');
                console.log(new_id);
                elem_selected.text("").attr("id",new_id);

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

        if (num_corrects == 14)
        {
            boton.html("¡Correcto!").addClass('finished').removeClass('btn-hover');
            boton.unbind("click", revisar);
            boton.css('cursor','default');
            sg.sound('claps');
        }   
    }

});