var lotteryData = new Array(4, 6, 11, 14, 19);
var answersCorrect = true;
var wrongAnswers = 0;
var questionIndex = 0;
var questionSetIndex = 1;
var displayIntro = true;
var jokerUsed = false;

$(document).ready(function () {
  $('.start-lottery').click(function () {
    $('#lottery-intro').hide();
    $('#play-now-circle').hide();
    
    loadFirstQuestionSet();
    displayIntro = false;
  });

  $(".answers .answer").hover(function () {
    $(this).addClass('hover');
  }, function () {
    $(this).removeClass('hover');
  });

  $(".answers .answer").click(function () {
    // Get the id of the correct answer
    var answerId = "answer-" + lotteryData[questionIndex];

    // Check if user selected the correct answer
    if ($(this).attr('id') != answerId) {
      answersCorrect = false;
      wrongAnswers++;
      //$(this).addClass('click-correct');
      // User selected wrong answer - restart lottery
    } /*else {
      //$(this).addClass('click-wrong');
      setTimeout(function() {loadFirstQuestionSet()}, 800);
    }*/

    questionIndex++;
    setTimeout(function() {loadNextQuestionSet();}, 800);
  });

  $("#lottery-progress-bar #joker").click(function() {
    if (!jokerUsed) {
      questionIndex++;
      setTimeout(function() {loadNextQuestionSet();}, 800);
      jokerUsed = true;
      $("#lottery-progress-bar #joker a").addClass('joker-used');
    }
  });
  
  $('#lottery-number-30, #lottery-number-1').hover(function() {
    $(this).addClass('hover');
  }, function() {
    $(this).removeClass('hover');
  });

  $('#lottery-number-1').click(function() {
    $("#dialog-confirm").dialog({
      width: 500, height: 180, modal: true, resizable: false, draggable: false,
      buttons: {
        "Ja": function() {
          location.href = "/winap/postcard";
        }, "Nein": function() {
          $(this).dialog("close");
        }
      }
    });
  });
});

function loadFirstQuestionSet() {
  // Check if we should load first question
  if (displayIntro || questionSetIndex > 1) {
    questionIndex = 0;
    questionSetIndex = 1;
    $('.question-set').hide('slow');
    $('#question-set-1').show('slow');
  }

  // Reset booleans
  answersCorrect = true;
  wrongAnswers = 0;
  jokerUsed = false;
  $("#lottery-progress-bar #joker a").removeClass('joker-used');

  updateLotteryProgressBar();
}

function loadNextQuestionSet() {
  var maxQuestionSetIndex = lotteryData.length;

  // Check if we should display next question
  if (questionSetIndex < maxQuestionSetIndex) {
    $('#question-set-' + questionSetIndex).hide('slow');
    questionSetIndex++;
    $('#question-set-' + questionSetIndex).show('slow');

    updateLotteryProgressBar();

  // User answered all questions correctly
  } else if (questionSetIndex == maxQuestionSetIndex) {
    if (answersCorrect) {
      $("#congratulation-dialog").dialog({width:500, height: 130, modal: true, resizable: false, draggable: false});
      setTimeout(function() {location.href = '/lottery/number';}, 3000);
    } else {
      $("#try-again-dialog p").text("Leider hast du " + wrongAnswers + " Frage(n) falsch beantwortet! Bitte versuche es erneut!");
      $("#try-again-dialog").dialog({width:500, height: 130, modal: true, resizable: false, draggable: false});
      setTimeout(function() {loadFirstQuestionSet();}, 800);
    }
  }
}

function removeAnswerIcons() {
  $(".answers .answer").removeClass('click-correct');
  $(".answers .answer").removeClass('click-wrong');
}

function updateLotteryProgressBar() {
  $('#lottery-progress-bar').show();
  $('#lottery-progress-bar #question').html('Frage ' + questionSetIndex + ' von ' + lotteryData.length);
}
