﻿
var _correctAnswer;
var _lastAnswerId = 0;
var _factDetails;
var _unl;

$(document).ready(function() {
    $('#lnkQuiz').click(function(e) {
        e.preventDefault();
        $('#quizPopup').modal({
            overlayCss: {
                backgroundColor: '#000',
                cursor: 'wait'
            },
            containerCss: {
                width: 382,
                height: 500
            }
        });
        $('#loading').show();
        loadRandomQuiz();
    });
});

function loadRandomQuiz() {
    //add randomizer to force ie to call NextQuiz again
    var rand = Math.random();
    // call JSON service to load quiz
    $.getJSON("/Quiz.aspx/NextQuiz?rand=" + rand,
        function(data) {
            //randomize the order of the answers
            var randomized = data.Answers.sort(function() { return Math.random() - 0.5 });
            for (i = 0; i < randomized.length; i++) {
                if (randomized[i].Correct == true) {
                    _correctAnswer = randomized[i];
                }
            }
            _factDetails = data.FactDetails;
            _lastAnswerId = data.Id;
            $("#quizPopupMiddleInner").setTemplateURL('/Quiz.htm');
            $("#quizPopupMiddleInner").processTemplate(data);
            $("#loading").hide();

            $("#nestedPopupNew").hide();
            $("#nestedPopup").hide();
                        $("#nestedPopup1").hide();
                        $("#nestedPopup2").hide();
                        $("#nestedPopup3").hide();
                        $("#nestedPopup4").hide();
                        $("#nestedPopup5").hide();
                        $("#nestedPopup6").hide();
                        $("#nestedPopupVillageGoalReached").hide();
                        $("#nestedPopupWaytogo").hide();
                        $("#nestedPopupExhausted").hide();
        });
}

function answerQuiz(answer) {
    $("#trCorrectLabel").hide();
    $("#trIncorrect").hide();
    $("#trFactDetail").hide();
    $("#quizContinue").hide();
    $("#quizWrongContinue").hide();

    if (answer == null) {
        var answerHtml = "<h4>Please choose an answer</h4>";
        $("#questionHead").html(answerHtml);
    }
    else {
        var expDate = new Date();
        expDate.setDate(expDate.getDate() + 365);
        document.cookie = 'lastQ=' + _lastAnswerId + '; expires=' + expDate.toGMTString() + '; path=/';

        var answerHtml;

        if (answer == _correctAnswer.Id) {
            answerHtml = "<h4>Correct!</h4>";
            $("#quizPopupMiddleInner").html('Your answer was <br />' + _correctAnswer.Answer + '<br /> ' + '<p>' + _factDetails);
            $("#quizContinue").slideDown("slow");
        }
        else {
            answerHtml = "<h4 class='sorry'>Sorry</h4>";
            $("#quizWrongContinue").slideDown("slow");
            $("#trAnswers").hide();
            $("#trSubmit").hide();
            $("#trCorrectLabel").show();
            $("#trIncorrect").show();
            $("#trFactDetail").show();
            $("#trLimit").hide();
        }

        $("#questionHead").html(answerHtml);
        $.post("/Quiz.aspx/PostAnswer", {}, function(data) {
            var result = eval(data);
            var isVillageGoalReached = result.IsVillageGoalReached;
            var userUnlockedAmountToday = result.UserUnlockedAmountToday;
            var isSiteGoalReached = result.IsSiteGoalReached;

            if (isSiteGoalReached) {
                showModalPopup(true, "#nestedPopupNew");
            }
            else 
            {
                if (isVillageGoalReached) {
                    if (userUnlockedAmountToday == 0) {
                        showModalPopup(true, "#nestedPopupVillageGoalReached");
                    }
                }
                else 
                {
                    if (userUnlockedAmountToday == 0) {
                        showModalPopup(true, "#nestedPopup1");
                    }
                    else if (userUnlockedAmountToday == 1) {
                        showModalPopup(true, "#nestedPopup2");
                    }
                    else if (userUnlockedAmountToday == 2) {
                        showModalPopup(true, "#nestedPopup3");
                    }
                    else if (userUnlockedAmountToday == 3) {
                        showModalPopup(true, "#nestedPopup4");
                    }
                    else if (userUnlockedAmountToday == 4) {
                        showModalPopup(true, "#nestedPopup5");
                    }
                    else if (userUnlockedAmountToday >= 5) {
                        showModalPopup(true, "#nestedPopup6");
                    }
                }

            }
        }, "json");

    }
}

function tryAnother() {
    $("#quizContinue").slideUp("slow");
    $("#quizWrongContinue").slideUp("slow");
    $("#questionHead").html("");
    loadRandomQuiz();
}

function enableQuiz() {
    showModalPopup(false, null);
}

function showModalPopup(showPopup, popupName) {

    $("#nestedPopupNew").hide();
        $("#nestedPopup1").hide();
        $("#nestedPopup2").hide();
        $("#nestedPopup3").hide();
        $("#nestedPopup4").hide();
        $("#nestedPopup5").hide();
        $("#nestedPopup6").hide();
        $("#nestedPopupWaytogo").hide();
        $("#nestedPopupExhausted").hide();
        $("#nestedPopupVillageGoalReached").hide();

    if (popupName) {
        if (showPopup)
            $(popupName).show();
        else
            $(popupName).hide();
    }

    jQuery('#btnClose').attr("disabled", showPopup);
    jQuery('#btnQuizWrongContinueTry').attr("disabled", showPopup);
    jQuery('#btnQuizContinueTry').attr("disabled", showPopup);
    jQuery('#btnQuizCorrectContinueMyVillage').attr("disabled", showPopup);
    //$("#btnClose")[0].disabled = showPopup;    
    //$("#btnQuizWrongContinueTry")[0].disabled = showPopup;
    //$("#btnQuizContinueTry")[0].disabled = showPopup;
    //$("#btnQuizCorrectContinueMyVillage")[0].disabled = showPopup;
}