//(c) vogelvisie.nl/birds-of-europe.com

PHOTOS_DIR = 'new_quiz/photos/quiz/';
AUDIO_DIR  = 'new_quiz/audio/quiz/';

function quiz($quizId){
  this.quizId = $quizId;
  this.question = -1;
  this.questions = new Array();
  this.timer = 0;
  this.hintVisible = 0;
  this.secondTry = 0;
  this.totalScore = 0;
  this.scoreSuffix = '';
  this.checkSum = 1234567;
  this.totalScoreLabel = '';
  this.answerLabel = '';

  this.start = function(){
    this.totalScore = 0;
    document.getElementById('start').style.visibility = 'hidden';
    this.showQuestion(0);
    this.showTotalScore();
  }

  this.showQuestion = function($nr){

    if($nr >= this.questions.length){
      this.submitForm();
      return;
    }

    this.secondTry = (this.question == $nr);

    if(this.secondTry){
      // Try again, set score to half Maximum;
      var $multiplier = .5;
      this.showHint();
    }else{
      var $multiplier = 1;
      this.hintVisible = 0;
      this.hideHint();
    }

    this.question = $nr;

    switch(this.questions[this.question].difficulty){
      case 1: this.baseScore = 300; break;
      case 2: this.baseScore = 400; break;
      case 3: this.baseScore = 500; break;
      case 4: this.baseScore = 600; break;
    }

    this.baseScore *= $multiplier;
    this.score = this.baseScore;

    document.getElementById('correct').style.visibility = 'hidden';
    document.getElementById('incorrect').style.visibility = 'hidden';
    document.getElementById('correct_answer').style.visibility = 'hidden';

    if(this.quizId == 1 || this.quizId == 2){
      document.getElementById('loading').style.visibility = 'visible';
      document.getElementById('quizPhoto').src = PHOTOS_DIR + this.questions[$nr].filename;
      if(this.secondTry){
        this.imageLoaded();
      }
    }else{
      document.getElementById('loading').style.visibility = 'visible';
      $audioPlayer.file = AUDIO_DIR + this.questions[$nr].filename;
      $audioPlayer.downloadFile();
    }

    for(var $i = 0; $i < this.questions[$nr].answers.length ; $i++){
      var $buttonDiv = document.getElementById('button_' + $i);
      $buttonDiv.innerHTML = this.questions[$nr].answers[$i][0];
    }
  }

  this.addQuestion = function($idx, $type, $difficulty, $filename, $answers, $hint){
    var $index = this.questions.length;
    this.questions[$index] = new Object();
    this.questions[$index].idx        = $idx;
    this.questions[$index].type       = $type;
    this.questions[$index].difficulty = $difficulty;
    this.questions[$index].filename   = $filename;
    this.questions[$index].answers    = $answers;
    this.questions[$index].hint       = $hint;
    this.questions[$index].correct    = 0;
  }

  this.setScoreSuffix = function($suffix){
    this.scoreSuffix = $suffix;
  }

  this.setTotalScoreLabel = function($label){
    this.totalScoreLabel = $label;
  }

  this.setAnswerLabel = function($label){
    this.answerLabel = $label;
  }

  this.selectAnswer = function($answer){
    document.getElementById('question').style.visibility    = 'hidden';
    document.getElementById('hint_text').style.visibility   = 'hidden';
    document.getElementById('hint_button').style.visibility = 'hidden';
    clearTimeout(this.timer);

    if(this.quizId == 3){
      $audioPlayer.stopPlay();
    }

    if(this.questions[this.question].answers[$answer][1] % 17 == 0){
      if(!this.secondTry)
        this.questions[this.question].correct = 1;
      document.getElementById('correct').style.visibility = 'visible';
      this.totalScore += this.score;
      this.checkSum -= 2 * this.score;
      this.showTotalScore();
      this.timer = setTimeout('$quiz.showQuestion(' + (this.question + 1) + ')', 2000);
    }else{
      document.getElementById('incorrect').style.visibility = 'visible';
      if(this.secondTry){
        this.showAnswer();
        this.timer = setTimeout('$quiz.showQuestion(' + (this.question + 1) + ')', 4000);
      }else{
        this.timer = setTimeout('$quiz.showQuestion(' + (this.question) + ')', 2000);
      }
    }
  }

  this.imageLoaded = function(){
    if($quiz.question >= 0){
      document.getElementById('loading').style.visibility  = 'hidden';
      document.getElementById('question').style.visibility = 'visible';
      clearTimeout($quiz.timer);
      $quiz.timer = setTimeout('$quiz.tooLong()', this.quizId < 3 ? 5000 : 10000);
      $quiz.showScore();
    }
  }

  this.showHint = function(){
    if(!this.hintVisible && this.quizId < 3){
      $hintSpan = document.getElementById('hint_span');
      $hintSpan.innerHTML = this.questions[this.question].hint;

      document.getElementById('hint_button').style.visibility = 'hidden';
      document.getElementById('hint_text').style.visibility   = 'visible';
      this.hintVisible = 1;
      clearTimeout(this.timer);
      this.timer = setTimeout('$quiz.tooLong()', 5000);
      this.score = Math.max(this.baseScore / 2, this.score - .25 * this.baseScore);
      this.showScore();
    }
  }

  this.hideHint = function(){
    this.hintVisible = 0;
    document.getElementById('hint_text').style.visibility   = 'hidden';
    if(this.question >= 0)
      if(this.questions[this.question].hint != '')
        document.getElementById('hint_button').style.visibility = 'visible';
    else
      document.getElementById('hint_button').style.visibility = 'hidden';

  }

  this.tooLong = function(){
    this.score = Math.max(this.baseScore / 2, this.score - 10);
    this.showScore();
    if(this.score > this.baseScore / 2)
      this.timer = setTimeout('$quiz.tooLong()', 2000);
  }

  this.showScore = function(){
    $scoreDiv = document.getElementById('score_box');
    var $textNode = document.createTextNode(Math.round(this.score) + ' ' + this.scoreSuffix);

    if($scoreDiv.firstChild)
      $scoreDiv.removeChild($scoreDiv.firstChild);

    $scoreDiv.appendChild($textNode);
  }

  this.showTotalScore = function(){
    $totalScoreDiv = document.getElementById('total_box');
    var $textNode = document.createTextNode(this.totalScoreLabel + ': ' + Math.round(this.totalScore));

    if($totalScoreDiv.firstChild)
      $totalScoreDiv.removeChild($totalScoreDiv.firstChild);

    $totalScoreDiv.appendChild($textNode);
  }

  this.showAnswer = function(){
    var $answerDiv = document.getElementById('correct_answer');
    for(var $i = 0; $i < this.questions[this.question].answers.length; $i++)
      if(this.questions[this.question].answers[$i][1] % 17 == 0)
        var $textNode = document.createTextNode(this.answerLabel + ': ' + this.questions[this.question].answers[$i][0]);

    if($answerDiv.firstChild)
      $answerDiv.removeChild($answerDiv.firstChild);

    $answerDiv.appendChild($textNode);
    document.getElementById('correct_answer').style.visibility = 'visible';
  }

  this.submitForm = function(){
    document.quiz_form.score.value    = this.totalScore;
    document.quiz_form.checksum.value = this.checkSum;
    for(var $i = 0; $i < this.questions.length; $i++){
      document.quiz_form.questions.value += this.questions[$i].idx + ':' + this.questions[$i].correct + ',';
    }
    document.quiz_form.submit();

  }
}

