$(function ()
{
    $('#btnPrev').hide();
    $('.jas').hide();
    $('#btnNext').hide();
    $('.ada').hide();
    var currentQuestion = 1;
    var currentPoints = 0;
    var emailSent = false;
    var totalQuestions = $('main').data("totalquestions");
    var totalPoints = $('main').data("totalpoints");
    var checking = $('main').data("checking");
    window.gameOn = true;
    $('#image' + currentQuestion).show();
    $('#question' + currentQuestion).show();

    var email = $('main').data('email');

    $('#btnNext').click(function () {
        $('#question' + currentQuestion).hide();
        $('#image' + currentQuestion).hide();
        $('#answers' + currentQuestion).hide();
        currentQuestion++;
        $('.currentQuestion').text('Pytanie: '+currentQuestion);
        if (currentQuestion == 2) {
            $('#btnPrev').show();
            $('.jas').show();
        }
        if (currentQuestion <= totalQuestions) {
            $('#question' + currentQuestion).show();
            $('#answers' + currentQuestion).show();
            $('#image' + currentQuestion).show();
        } else {
            $('.left').hide();
            $('.right').hide();
            $('#btnNext').hide();
            $('#btnPrev').hide();
            $('.ada').hide();
            $('.jas').hide();

            if (window.gameOn) {
                for (var i = 1; i <= totalQuestions; i++) {
                    var answerType = $('#answers' + i).data("answertype");

                    if (answerType == 1) {
                        var input = $('#answer' + i + '_1').val();
                        $('#answer' + i + '_1').prop('disabled', true);
                        if (input.toString().toLowerCase() == $('#question' + i).data("rightanswer").toString().toLowerCase()) {
                            currentPoints += $('#question' + i).data("points");
                            $('#answer' + i + '_1').css('border-color', 'rgb(53, 177, 5)');
                        } else {
                            $('#answer' + i + '_1').css('border-color', 'red');
                        }
                    } else {
                        $('input[name=answer' + i + ']').prop('disabled', true);
                        if ($('input[name=answer' + i + ']:checked').val() == $('#question' + i).data("rightanswer")) {
                            currentPoints += $('#question' + i).data("points");
                            $('input[name=answer' + i + ']:checked').parent().css("color", "rgb(53, 177, 5)").css("font-weight", "strong").append('<div class="mark_ok"></div>');
                            
                        } else {
                            $('input[name=answer' + i + ']:checked').parent().css("color", "red").css("font-weight", "strong").append('<div class="mark_bad"></div>');
                            $('input[name=answer' + i + '][value=' + $('#question' + i).data("rightanswer") + ']').parent().css("color", "rgb(53, 177, 5)").css("font-weight", "strong").append('<div class="mark_ok"></div>');
                        }
                    }

                }
            }
            window.gameOn = false;
            if (email.length > 0 && !emailSent) {
                var finalmsg = '<div class="finalmsg">Test ukończony !<br /><br />' +
                        '<span style="font-size: 36px; color:#0D7AFC">Twój wynik to: ' + currentPoints + ' punktów!</span><br /><br />' +
                        'Maksymalna liczba punktów do uzyskania to ' + totalPoints + '.<br /><br />' +
                        'Wpisz swoje imię i nazwisko: <br /><br />' +
                        '<form id="emailForm" action="email.php" method="post">' +
                        '<input id="studentName" style="width: 25%; position: absolute; left: 299px;" type="text" name="studentName" placeholder="Np. Jan Kowalski" autocomplete="off"><br />' +
                        '<input type="hidden" value="' + email + '" name="teacherEmail">' +
                        '<input type="hidden" value="' + $(document).find("title").text() + '" name="gameName">' +
                        '<input type="hidden" value="' + currentPoints + '" name="studentPoints">' +
                        '<button name="submit" type="submit" style="margin-left: 0px;float: none;" id="btnSubmitResults" class="btn">Wyślij wynik<div class="arrow-next"></div></button>' +
                        '</form><br />' +
                        '</div>';
                $('main').append(finalmsg);
                var form = $('#emailForm');
                var submit = $('#btnSubmitResults');

                form.on('submit', function (e) {
                    e.preventDefault();

                    $.ajax({
                        url: 'email.php',
                        type: 'POST',
                        dataType: 'html',
                        data: form.serialize(),
                        beforeSend: function () {
                            submit.html('Wysyłanie....');
                        },
                        success: function (data) {
                            form.trigger('reset');
                            submit.html('Wyślij wynik');
                            submit.hide();
                            emailSent = true;
                        },
                        error: function (e) {
                            console.log(e)
                        }
                    });
                });
            } else {
                $('.description').hide();
                $('main').append('<div class="finalmsg">Test ukończony !<br /><br /><span style="font-size: 36px; color:#0D7AFC">Twój wynik to: ' + currentPoints + ' punktów!</span><br /><br />Maksymalna liczba punktów do uzyskania to ' + totalPoints + '.<br /> Pozostały czas: '+$('#OneHourCountDown').text()+'</div>');
            }
            if (checking == 1) {
                $('#btnCheck').show();
                
            }
        }
    });

    $('#btnCheck').click(function () {
        $('#btnCheck').hide();
        $('.left').show();
        $('.right').show();
        $('#btnNext').show();
        $('.ada').show();
        $('.finalmsg').hide();
        $('#question' + currentQuestion).hide();
        $('#image' + currentQuestion).hide();
        $('#answers' + currentQuestion).hide();
        currentQuestion = 1;
        $('.currentQuestion').text('Pytanie: '+currentQuestion);
        $('#question' + currentQuestion).fadeIn();
        $('#answers' + currentQuestion).fadeIn();
        $('#image' + currentQuestion).fadeIn();
    });

    $('#btnPrev').click(function (e) {
        $('#question' + currentQuestion).hide();
        $('#image' + currentQuestion).hide();
        $('#answers' + currentQuestion).hide();
        currentQuestion--;
        $('.currentQuestion').text('Pytanie: '+currentQuestion);
        if (currentQuestion == 1){
            $(this).hide();
         $('.jas').hide();
         }
        $('#question' + currentQuestion).show();
        $('#answers' + currentQuestion).show();
        $('#image' + currentQuestion).show();
    });

    $('a.hint').click(function (e) {
        e.preventDefault();
        $('div.hint').fadeToggle();

    });

    $('#btnReset').click(function () {
        var res = confirm("Czy na pewno chcesz zresetować test i zacząć od nowa?");
        if (res == true) {
            location.reload();
        }
    });

    $('#btnInfo').click(function () {
        var infoMsg = 'Pomoc multimedialna zrealizowana w projekcie "Ad@ i J@ś na matematycznej wyspie"\nNumer projektu WND-POKL.03.03.04-00-020/1\n\nLider projektu:\n' +
                'Agencja Wydawnicza Aga Press Sp. z o.o.\nul. Kamionkowska 45\n03-812 Warszawa\nwww.aga-press.pl\n\nPartner projektu:\n' +
                'Logonet Sp. z o.o.\nul. ks. M. Piotrowskiego 7-9\n85-098 Bydgoszcz\ntel.: (52) 326 00 56\nfax (52) 326 00 55\nwww.lo.pl';
        alert(infoMsg);
    });

    $("input").each(function () {
        if ((this.getAttribute('checked') == null) == this.checked)
            this.checked = !this.checked;
        else
            this.value = this.getAttribute('value') || '';
    });

    $("select").each(function () {
        var opts = $("option", this), selected = 0;
        for (var i = 0; i < opts.length; i++)
            if (opts[i].getAttribute('selected') !== null)
                selected = i;

        this.selectedIndex = selected || 0;
    });

    $('.start').click(function () {
        OneHourCountDown("OneHourCountDown", $('main').data("time") * 60); //60sec x 60min = 1hour or 3600 sec
        $('#btnNext').show();
        $('.ada').show();
    $('#answers' + currentQuestion).show();
    $(".left p[id*='question']").css('visibility','visible');
                $('.start').hide();
                $('.pre').hide();
                $('.currentQuestion').text('Pytanie: '+currentQuestion);
                $('.right .header').text('Odpowiedź:');
                $('.description').show();
    });
});
