var difference = 0

function Quiz(questions){
  this.questions = questions;
  this.showQuestions = showQuestions;
  this.showExplanation = showExplanation;
  this.scoreBoard = new ScoreBoard();
  this.showScoreBoard = this.scoreBoard.show;
//  this.explanationText = document.createTextNode("");
  this.explanationHolder = null;
  this.updateExplanation = updateExplanation;

  // Function for questions.  
  function showQuestions(questionsHolder){
    for(var i=0; i < this.questions.length; i++){
      currentQuestion = this.questions[i];
      currentQuestion.display();
      collection = currentQuestion.choicesCollection;
      for(var a=0; a < collection.length; a++){
        addAnswerHandler(this, currentQuestion, collection[a]);
      }
    }
  }

  // Function for handling button events.
  function addAnswerHandler(quiz, question, answer){
    // Handles button activity upon answer selected.
    if(navigator.userAgent.search("MSIE")==-1){
      answer.addEventListener("click", answerAction, false);
    }
    else{
      answer.attachEvent("onclick", answerAction);
    }    

    function answerAction(){
      var choices = question.choicesCollection;
      var answered = false;
      var pressed = "quizButtonPressed";

      // Disable the answers to this question.
      for(var i=0; i< choices.length; i++){ 
        disableButton(choices[i]);
        // Because the removeEventListener is not working properly
        // in the disableButton function, have used this alternate
        // method of finding if a button has been pressed to 
        // allow whether the answerAction does anything.
        answered = (choices[i].className == pressed); 
        if(answered)
          break;
      }
      
      if(!answered){
        answer.className = pressed;
        userAnswer = question.validateAnswer(answer)
        if(userAnswer)
          question.mark.className = "correct";
        else 
          question.mark.className = "incorrect";

        quiz.scoreBoard.update(userAnswer);
        quiz.updateExplanation(question.explanation);

        // This is a special line that calls the page positioning
        // function for site. Reason for this is because any text
        // added to the page (eg. in explanation area), causes the
        // content to be pushed down. On a small screen this can 
        // allow the main content be become hidden behind the footer.
        var increase = getHeightIncrease();
        if(increase > 0){
          // setLayout is defined in w_control_javascript_ext_e.js
          setLayout();
        }
      }
    }

    function disableButton(el){
      // NOTE: This is not working correctly as only removing for first call.
      if(navigator.userAgent.search("MSIE")==-1){
        el.removeEventListener("click", answerAction, false);
      }
      else{
        el.detachEvent("onclick", answerAction);
      }
    }

  }



  // Functions for scoreboard.
  function ScoreBoard(){
    score = new Array();
    this.show = show;
    this.update = update;

    function show(scoreBoardHolder){
      // Create quiz display.
      scoreBoard = document.createElement("div");
      scoreSpan = document.createElement("span");
      scoreText = document.createTextNode("Score:");
      imgCorrect = document.createElement("img");
      imgCorrect.id = "score-board-tick";
      imgCorrect.src = "/images/spacer.gif";
      imgCorrect.height = "35";
      imgCorrect.width = "36";
      correctSpan = document.createElement("span");
      correctSpan.className = "score";
      correctText = document.createTextNode("0");
      score.push(correctText);
      imgWrong = document.createElement("img");
      imgWrong.id = "score-board-cross";
      imgWrong.src = "/images/spacer.gif";
      imgWrong.height = "32";
      imgWrong.width = "34";
      wrongSpan = document.createElement("span");
      wrongSpan.className = "score";
      wrongText = document.createTextNode("0");
      score.push(wrongText);

      // Assign created objects to parents.
      scoreBoardHolder.appendChild(scoreBoard);
      scoreBoard.appendChild(scoreSpan);
      scoreSpan.appendChild(scoreText);
      scoreBoard.appendChild(imgCorrect);
      scoreBoard.appendChild(correctSpan);
      correctSpan.appendChild(correctText);
      scoreBoard.appendChild(imgWrong);
      scoreBoard.appendChild(wrongSpan);
      wrongSpan.appendChild(wrongText);
    }
  
    function update(checkedAnswer){
      if(checkedAnswer)
        score[0].nodeValue = String(Number(score[0].nodeValue) + 1);
      else
        score[1].nodeValue = String(Number(score[1].nodeValue) + 1);
    }

  }

  // Functions for the answer explanation.
  function showExplanation(explanationHolder){
    this.explanationHolder = explanationHolder;
  }

  function updateExplanation(explanation){
    this.explanationHolder.innerHTML = unescape(explanation);
  }

}



function QuizQuestion(q,c,a,e){
  this.question = q;
  this.choices = c;
  this.answer = a;
  this.explanation = e;
  this.display = display;
  this.mark = document.createElement("img");
  this.validateAnswer = validateAnswer;
  this.choicesCollection = new Array();

  function display(){
    // Create elements.
    questionHolder = document.createElement("div");
    questionHolder.className = "question";
    questionTitle = document.createElement("h2");  
    questionTitle.className = "title";
    questionText = document.createTextNode(this.question);
    choicesHolder = document.createElement("div");
    choicesHolder.className = "choicesHolder";
    for(var i=0; i < this.choices.length; i++){
      this.choicesCollection.push(document.createElement("input"));
      this.choicesCollection[i].type = "button";
      this.choicesCollection[i].name = "answer";
      this.choicesCollection[i].className = "quizButton";
      this.choicesCollection[i].value = this.choices[i];
    }
    markHolder = document.createElement("div");
    markHolder.className = "markHolder";
    this.mark.src = "/efc_images/quiz_empty.gif";
    this.mark.height = "32";
    this.mark.width = "34";
   
    // Assign elements to parents.
    questionsArea = document.getElementById("questions");
    questionsArea.appendChild(questionHolder);
    questionHolder.appendChild(questionTitle);
    questionTitle.appendChild(questionText);
    questionHolder.appendChild(choicesHolder);
    for(i=0; i < this.choicesCollection.length; i++){
      choicesHolder.appendChild(this.choicesCollection[i]);
    }
    questionHolder.appendChild(markHolder);
    markHolder.appendChild(this.mark);
  }
  
  function validateAnswer(answer){
    var correct = true;
    var wrong = false;

    if(answer.value == this.answer)
      return correct;
    else
      return wrong;

  }
}


function getHeightIncrease(){
  // original value for explanationHeight set in body.
  // original value for mainContentHeight set in w_control_javascript_ext_e.js
  // value for footerPositionX set in w_control_javascript_ext_e.js
  var heightIncrease = 0;
  var newExplanationHeight = document.getElementById("answer-explanation").offsetHeight;

  if(newExplanationHeight > explanationHeight){
    var difference = newExplanationHeight - explanationHeight;
    newMainContentHeight = mainContentHeight + difference;
    if(newMainContentHeight > footerPositionX){
      heightIncrease = newMainContentHeight - footerPositionX;
    }
  }  

  return heightIncrease;
}

