var QuizController = {
  AnswersHolder: [],

  CheckInput: function(e, question_id, answer, action) {
    QuizController.CheckAnswer(question_id, answer, action);
  },

  CheckAnswer: function(question_id, answer, action) {
      for(var i = 0; i < QuizController.AnswersHolder[question_id].length; ++i) {
        if(QuizController.AnswersHolder[question_id][i] === hex_sha1(answer)) {
          $('tick_' + question_id).style.display = "block";
          $('answer_holder_' + question_id).style.display = "";

          var a = document.createElement('a');

          new Ajax.Request(
            action,
            {
              asynchronous:false,
              parameters: { "answer" : answer, "question_id": question_id },
              onSuccess: function(data){ a.href = data.responseJSON.answer_url; }
            });

          a.target = "_blank";
          a.innerHTML = "View full image";

          while($('answer_' + question_id).firstChild)
            $('answer_' + question_id).removeChild($('answer_' + question_id).firstChild);

          $('answer_' + question_id).appendChild(a);
          return;
        }
      }

      $('tick_' + question_id).style.display = "none";
      $('answer_holder_' + question_id).style.display = "none";
  },

  SaveAnswer: function(input, question_id, action) {
    if(input.value.length > 0)
      new Ajax.Request(
            action,
            {
              asynchronous:true,
              parameters: { "answer" : input.value, "question_id": question_id },
              onSuccess: function(){}
            });
  }
};
