var ctrPagina1 = 0;

function init() {
    var pagina = 0;
    $('.btnAzul').mousedown(function(event) {
        $(this).css('top', '+=3px');
    });

    $('.btnAzul').mouseup(function(event) {
        $(this).css('top', '-=3px');
    });

    $('.btnAzul, .principal').click(function(event) {
        $(this).removeAttr('onclick');
        var idbtn = $(this).attr('id');
        switch (idbtn) {
            case 'btnPrin':
                sg.abrirPop(1);
                break;
            case 'btnA1':
                sg.abrirPop(2);
                break;
            case 'btnA2':
                sg.abrirPop(3);
                break;
            case 'btnA3':
                sg.abrirPop(4);
                break;
            case 'btnA4':
                sg.abrirPop(5);
                break;
            case 'btnA5':
                sg.abrirPop(6);
                break;
            case 'btnA6':
                sg.abrirPop(7);
                break;
            case 'btnA7':
                sg.abrirPop(8);
                break;
        }
        if ($(this).attr('suma') == 'true') {
            ctrPagina1++;
            $(this).attr('suma', 'false');
        }
    });

    $('.lyClose').click(function(event) {
        //ctrPagina1 = 8; // debo quitar esta linea
        if (ctrPagina1 == 8) {
            sg.abrirPop(9);
        }
    });

    $('.homePa').click(function(event) {
        /* Act on the event */
        ctrPagina1 = 0;
        $('.btnAzul').attr('suma', 'true');
        $('.principal').attr('suma', 'true');
    });
    $('.cerrar9').click(function(event) {
        sg.cerrarPop(9);
        pagina++;
        arrgalabotonera();
        actividadDrad1();
    });

    $('.nextPa').click(function() {
        if (pagina < 3) {
            pagina++;
            arrgalabotonera();
        }
        if (pagina == 1) {
            actividadDrad1();
        } else if (pagina == 2) {
            actividadDrad2();
        } else if (pagina == 3) {
            actividadDrad3();
        }
    });

    $('.prevPa').click(function() {
        if (pagina > 0) {
            pagina--;
            arrgalabotonera();
        }
        if (pagina == 1) {
            actividadDrad1();
        } else if (pagina == 2) {
            actividadDrad2();
        } else if (pagina == 3) {
            actividadDrad3();
        }
    });

    function arrgalabotonera() {
        if (pagina == 1) {
            $('.prevPa').css('visibility', 'hidden');
        } else {
            $('.prevPa').css('visibility', 'visible');
        }
    }

    function actividadDrad1() {
        sg.setDraggable(
            ".drag1", {
                dropSelector: ".drop1",
                success: function() {
                    var hit = $(this).data("hit");
                    var iddrop = hit.attr('id');
                    var clases = $(this).attr('class');
                    clases = clases.split(' ');
                    switch (iddrop) {
                        case 'drop11':
                            if (clases[2] == "drag11") {
                                sg.sound("success");
                                $(this).css('visibility', 'hidden');
                                hit.css('visibility', 'hidden');
                            } else {
                                sg.sound("error");
                                sg.resetDraggable('.' + clases[2]);
                            }
                            break;
                        case 'drop12':
                            if (clases[2] == "drag17") {
                                sg.sound("success");
                                $(this).css('visibility', 'hidden');
                                hit.css('visibility', 'hidden');
                            } else {
                                sg.sound("error");
                                sg.resetDraggable('.' + clases[2]);
                            }
                            break;
                        case 'drop13':
                            if (clases[2] == "drag16") {
                                sg.sound("success");
                                $(this).css('visibility', 'hidden');
                                hit.css('visibility', 'hidden');
                            } else {
                                sg.sound("error");
                                sg.resetDraggable('.' + clases[2]);
                            }
                            break;
                        case 'drop14':
                            if (clases[2] == "drag12") {
                                sg.sound("success");
                                $(this).css('visibility', 'hidden');
                                hit.css('visibility', 'hidden');
                            } else {
                                sg.sound("error");
                                sg.resetDraggable('.' + clases[2]);
                            }
                            break;
                        case 'drop15':
                            if (clases[2] == "drag13") {
                                sg.sound("success");
                                $(this).css('visibility', 'hidden');
                                hit.css('visibility', 'hidden');
                            } else {
                                sg.sound("error");
                                sg.resetDraggable('.' + clases[2]);
                            }
                            break;
                        case 'drop16':
                            if (clases[2] == "drag14") {
                                sg.sound("success");
                                $(this).css('visibility', 'hidden');
                                hit.css('visibility', 'hidden');
                            } else {
                                sg.sound("error");
                                sg.resetDraggable('.' + clases[2]);
                            }
                            break;
                        case 'drop17':
                            if (clases[2] == "drag15") {
                                sg.sound("success");
                                $(this).css('visibility', 'hidden');
                                hit.css('visibility', 'hidden');
                            } else {
                                sg.sound("error");
                                sg.resetDraggable('.' + clases[2]);
                            }
                            break;
                        default:
                            sg.resetDraggable('.' + clases[2]);
                            break;
                    }
                },
                fail: "sg.sound('error')",
                revert: true
            });
    }

    function actividadDrad2() {
        sg.setDraggable(
            ".drag2", {
                dropSelector: ".drop2",
                success: function() {
                    var hit = $(this).data("hit");
                    var iddrop = hit.attr('id');
                    var clases = $(this).attr('class');
                    clases = clases.split(' ');
                    switch (iddrop) {
                        case 'drop21':
                            if (clases[2] == "drag23") {
                                sg.sound("success");
                                $(this).css('visibility', 'hidden');
                                hit.css('visibility', 'hidden');
                            } else {
                                sg.sound("error");
                                sg.resetDraggable('.' + clases[2]);
                            }
                            break;
                        case 'drop22':
                            if (clases[2] == "drag26") {
                                sg.sound("success");
                                $(this).css('visibility', 'hidden');
                                hit.css('visibility', 'hidden');
                            } else {
                                sg.sound("error");
                                sg.resetDraggable('.' + clases[2]);
                            }
                            break;
                        case 'drop23':
                            if (clases[2] == "drag25") {
                                sg.sound("success");
                                $(this).css('visibility', 'hidden');
                                hit.css('visibility', 'hidden');
                            } else {
                                sg.sound("error");
                                sg.resetDraggable('.' + clases[2]);
                            }
                            break;
                        case 'drop24':
                            if (clases[2] == "drag22") {
                                sg.sound("success");
                                $(this).css('visibility', 'hidden');
                                hit.css('visibility', 'hidden');
                            } else {
                                sg.sound("error");
                                sg.resetDraggable('.' + clases[2]);
                            }
                            break;
                        case 'drop25':
                            if (clases[2] == "drag27") {
                                sg.sound("success");
                                $(this).css('visibility', 'hidden');
                                hit.css('visibility', 'hidden');
                            } else {
                                sg.sound("error");
                                sg.resetDraggable('.' + clases[2]);
                            }
                            break;
                        case 'drop26':
                            if (clases[2] == "drag21") {
                                sg.sound("success");
                                $(this).css('visibility', 'hidden');
                                hit.css('visibility', 'hidden');
                            } else {
                                sg.sound("error");
                                sg.resetDraggable('.' + clases[2]);
                            }
                            break;
                        case 'drop27':
                            if (clases[2] == "drag24") {
                                sg.sound("success");
                                $(this).css('visibility', 'hidden');
                                hit.css('visibility', 'hidden');
                            } else {
                                sg.sound("error");
                                sg.resetDraggable('.' + clases[2]);
                            }
                            break;
                        default:
                            sg.resetDraggable('.' + clases[2]);
                            break;
                    }
                },
                fail: "sg.sound('error')",
                revert: true
            });
    }

    function actividadDrad3() {
        sg.setDraggable(
            ".drag3", {
                dropSelector: ".drop3",
                success: function() {
                    var hit = $(this).data("hit");
                    var iddrop = hit.attr('id');
                    var clases = $(this).attr('class');
                    clases = clases.split(' ');
                    switch (iddrop) {
                        case 'drop31':
                            if (clases[2] == "drag37") {
                                sg.sound("success");
                                $(this).css('visibility', 'hidden');
                                hit.css('visibility', 'hidden');
                            } else {
                                sg.sound("error");
                                sg.resetDraggable('.' + clases[2]);
                            }
                            break;
                        case 'drop32':
                            if (clases[2] == "drag36") {
                                sg.sound("success");
                                $(this).css('visibility', 'hidden');
                                hit.css('visibility', 'hidden');
                            } else {
                                sg.sound("error");
                                sg.resetDraggable('.' + clases[2]);
                            }
                            break;
                        case 'drop33':
                            if (clases[2] == "drag33") {
                                sg.sound("success");
                                $(this).css('visibility', 'hidden');
                                hit.css('visibility', 'hidden');
                            } else {
                                sg.sound("error");
                                sg.resetDraggable('.' + clases[2]);
                            }
                            break;
                        case 'drop34':
                            if (clases[2] == "drag31") {
                                sg.sound("success");
                                $(this).css('visibility', 'hidden');
                                hit.css('visibility', 'hidden');
                            } else {
                                sg.sound("error");
                                sg.resetDraggable('.' + clases[2]);
                            }
                            break;
                        case 'drop35':
                            if (clases[2] == "drag32") {
                                sg.sound("success");
                                $(this).css('visibility', 'hidden');
                                hit.css('visibility', 'hidden');
                            } else {
                                sg.sound("error");
                                sg.resetDraggable('.' + clases[2]);
                            }
                            break;
                        case 'drop36':
                            if (clases[2] == "drag35") {
                                sg.sound("success");
                                $(this).css('visibility', 'hidden');
                                hit.css('visibility', 'hidden');
                            } else {
                                sg.sound("error");
                                sg.resetDraggable('.' + clases[2]);
                            }
                            break;
                        case 'drop37':
                            if (clases[2] == "drag34") {
                                sg.sound("success");
                                $(this).css('visibility', 'hidden');
                                hit.css('visibility', 'hidden');
                            } else {
                                sg.sound("error");
                                sg.resetDraggable('.' + clases[2]);
                            }
                            break;
                        default:
                            sg.resetDraggable('.' + clases[2]);
                            break;
                    }
                },
                fail: "sg.sound('error')",
                revert: true
            });
    }
}