define(function(require) {

    (function() {

        var l = new Linguas();
        l.verificaLinguaPadrao();

        var form                = $('#players');
        var girar               = $('.opcoes #girar');
        var voltarJogo          = $('.opcoes #voltarJogo');
        var config              = $('.opcoes #configuracoes');
        var container           = $('.container');
        var counters            = $('.counters');
        var conteudoAtualizar   = $('.conteudoAtualizar');
        var divTempoJogo        = $('.tempo');
        var tempoJogo           = $('#tempoJogo');
        var turno               = $('.turno');
        var contadorTurno       = $('#contadorTurno');
        var turnoAdd            = $('.turno .add');
        var turnoSub            = $('.turno .sub');
        var labelPlayer1        = $('#labelPlayer1');
        var labelPlayer2        = $('#labelPlayer2');

        form.submit(function(){

            var player1 = $('#player1').val();
            var player2 = $('#player2').val();

            if (player1.length <= 0)
            {
                player1 = l.texto('jogador1Vazio');
            }

            if (player2.length <= 0)
            {
                player2 = l.texto('jogador2Vazio');
            }

            labelPlayer1.html(player1);
            labelPlayer2.html(player2);

            counters.show('slow');
            divTempoJogo.show('slow');
            turno.show('slow');
            container.hide('slow');
            conteudoAtualizar.hide('slow');
            girar.show();
            voltarJogo.show();
            config.hide();

            tempoJogo.stopwatch().stopwatch('start').click(function(){
                $(this).stopwatch('toggle');
            });

            return false;

        });

        turnoAdd.click(function(){
            var turnoAtual  = contadorTurno.html();
            var novoTurno   = eval(turnoAtual) + 1;
            contadorTurno.html(novoTurno);
        });

        turnoSub.click(function(){
            var turnoAtual = contadorTurno.html();
            var novoTurno   = eval(turnoAtual) - 1;
            if (novoTurno < 0)
            {
                return false;
            }
            contadorTurno.html(novoTurno);
        });

        $('.controls .add').click(function(){

            var type            = $(this).hasClass('life') ? 'life' : 'poison';
            var colorHistory    = type === 'life' ? 'green' : 'red';
            var turnoAtual      = contadorTurno.html();
            var counter         = $(this).parent().parent().parent().find('.counter');
            var counterPoison   = $(this).parent().parent().parent().find('.counterPoison');
            var history         = $(this).parent().parent().parent().find('.history');
            var historyList     = history.find('ul');
            var numberIndex     = $(this).parent().find('input.numberIndex').val();

            var tmpCounter  = counter.html();
            var newCounter  = eval(tmpCounter) + eval(numberIndex);

            var tmpCounterPoison    = counterPoison.html();
            var newCounterPoison    = eval(tmpCounterPoison) + eval(numberIndex);

            if (numberIndex.length > 0)
            {

                historyList.append('<li class="' + colorHistory + ' ' + type + '">+ ' + numberIndex + l.texto(type) + l.texto('noTurno') + turnoAtual + ' (' + tempoJogo.html() + ')</li>'); 
                historyList.find('li:last').fadeIn(200).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
                history.animate({ scrollTop : historyList.height() }, 400);
                
                if ($(this).hasClass('life'))
                {

                    if (newCounter >= 10)
                    {

                        counter.css('color', '#008500');

                    }

                    if (newCounter >= 5 && newCounter < 10)
                    {

                        counter.css('color', '#FF3500');

                    }

                    counter.html(newCounter).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);

                }
                else
                {

                    if (newCounterPoison >= 10)
                    {

                        if('vibrate' in navigator) {
                            navigator.vibrate(500);
                        }

                        counterPoison.css('color', '#F30021');

                    }

                    if (newCounterPoison >= 5 && newCounterPoison < 10)
                    {

                        counterPoison.css('color', '#FF3500');

                    }

                    counterPoison.html(newCounterPoison).fadeOut(100).fadeIn(200).fadeOut(100).fadeIn(100);

                }

            }

        });


        $('.controls .sub').click(function(){

            var type            = $(this).hasClass('life') ? 'life' : 'poison';
            var colorHistory    = type === 'life' ? 'red' : 'green';
            var turnoAtual      = contadorTurno.html();
            var counter         = $(this).parent().parent().parent().find('.counter');
            var counterPoison   = $(this).parent().parent().parent().find('.counterPoison');
            var history         = $(this).parent().parent().parent().find('.history');
            var historyList     = history.find('ul');
            var numberIndex     = $(this).parent().find('input.numberIndex').val();

            var tmpCounter  = counter.html();
            var newCounter  = eval(tmpCounter) - eval(numberIndex);

            var tmpCounterPoison    = counterPoison.html();
            var newCounterPoison    = eval(tmpCounterPoison) - eval(numberIndex);

            if (numberIndex.length > 0)
            {
                
                historyList.append('<li class="' + colorHistory + ' ' + type + '">- ' + numberIndex + l.texto(type) + l.texto('noTurno') + turnoAtual + ' (' + tempoJogo.html() + ')</li>'); 
                historyList.find('li:last').fadeIn(200).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
                history.animate({ scrollTop : historyList.height() }, 400);
                
                if ($(this).hasClass('life'))
                {

                    if (newCounter <= 10)
                    {

                        counter.css('color', '#FF3500');

                    }

                    if (newCounter <= 5)
                    {

                        counter.css('color', '#F30021');

                    }

                    if (newCounter <= 0)
                    {

                        if('vibrate' in navigator) {
                            navigator.vibrate(500);
                        }

                    }

                    counter.html(newCounter).fadeOut(100).fadeIn(200).fadeOut(100).fadeIn(100);

                }
                else
                {

                    if (newCounterPoison <= 10)
                    {

                        counterPoison.css('color', '#F30021');

                    }

                    if (newCounterPoison <= 5)
                    {

                        counterPoison.css('color', '#FF3500');

                    }

                    if (newCounterPoison <= 2)
                    {

                        counterPoison.css('color', '#008500');

                    }

                    if (newCounterPoison <= 0)
                    {

                        if('vibrate' in navigator) {
                            navigator.vibrate(500);
                        }

                    }

                    counterPoison.html(newCounterPoison).fadeOut(100).fadeIn(200).fadeOut(100).fadeIn(100);

                }

            }

        });

        voltarJogo.click(function(){

            $('.counter').each(function(){
                $(this).html('20').css('color', '#008500');
            });

            $('.counterPoison').each(function(){
                $(this).html('0').css('color', '#008500');
            });

            $('.history').each(function(){
                $(this).html('');
            });

            counters.hide('slow');
            girar.hide();
            voltarJogo.hide();

            container.show('slow');
            config.show();

        });

        $('#girar').click(function(){

            var dois = $('.dois');

            if (!dois.hasClass('rotate')) {

                dois.addClass('rotate');
                $('.dois').css('animation', 'rotate 2s');
                $('.dois').css('transform', 'rotate(180deg)');

            } else {

                dois.removeClass('rotate');
                $('.dois').css('animation', 'rotateBack 2s');
                $('.dois').css('transform', 'rotate(0deg)');

            }

        });

    })();

});