LibTest = {


  showQuestion : function (questionNumber){
    if(!testFirstPage){ //first page is atomatically tracked
      if(testFinished){
        pageTracker._trackPageview(testRelativeUrl + 'result/?nr=' + (questionNumber + 1));
      } else {
        pageTracker._trackPageview(testRelativeUrl + '?nr=' + (questionNumber + 1));
      }
    }

    var question = testQuestions[questionNumber];
    var i = 0;


    document.getElementById('testQuestionNumber').innerHTML = '';
    document.getElementById('testQuestionNumber').appendChild(document.createTextNode('Testo klausimas: ' + (questionNumber + 1) + '/' + testQuestions.length));


    document.getElementById('testQuestionTitle').innerHTML = '';
    document.getElementById('testQuestionTitle').appendChild(document.createTextNode(question['klausimas']));


    document.getElementById('testPhoto').style.display = 'none';

    if(question['iliustracija'] != ''){
      document.getElementById('testPhoto').src = testBaseUrl + 'testas/' + question['iliustracija'];
      document.getElementById('testPhoto').style.display = 'block';
      
    }

    document.getElementById('testRules').innerHTML = '';
    if(testFinished){
      document.getElementById('testRules').innerHTML = question['rulesHtml'];
    }


    document.getElementById('testAnswers').innerHTML = ''; //remove all answers

    i = 0;
    while(i < question['answers'].length){ //add answers
      var answerDiv = document.createElement('div');

      var checkbox = document.createElement('div');  //answer checkbox
      checkbox.setAttribute('id', 'mod_auto_test_' + questionNumber + '_' + i);
      if(testQuestions[questionNumber]['answers'][i]['checked'] != undefined && testQuestions[questionNumber]['answers'][i]['checked'] == 1){
        checkbox.className = 'check1 kaire'; //checked
      } else {
        checkbox.className = 'check kaire'; //unchecked
      }
      checkbox.questionNumber = questionNumber;
      checkbox.answerNumber = i;
      answerDiv.appendChild(checkbox);
      checkbox.answerId = question['answers'][i]['id'];
      if(!testFinished){
        checkbox.onclick = LibTest.checkboxClick;
      }
      var answerText = document.createElement('div'); //answer text
      
      if(testFinished){
        if(testQuestions[questionNumber]['answers'][i]['teisingas'] == 1){
          answerText.className = 'zalias desine';
        } else {
          answerText.className = 'pilkas2 desine';
        }
      } else {
        answerText.className = 'pilkas2 desine';
      }
      answerText.appendChild(document.createTextNode(question['answers'][i]['atsakymas']));
      answerDiv.appendChild(answerText);

      document.getElementById('testAnswers').appendChild(answerDiv);

      var clear = document.createElement('div'); //clear div
      clear.className = 'clear';
      document.getElementById('testAnswers').appendChild(clear);
      i++;



    }


    //place question nubmers (links)
    var answeredCount = 0;
    document.getElementById('testNumbers').innerHTML = ''; //remove all answers
    //quesstion numbers
    i = 0;
    while(i < testQuestions.length){ //add answers
      var numberDiv = document.createElement('div'); //clear div


      ii = 0;
      var answered = false;
      while(ii < testQuestions[i]['answers'].length){ //check if the question is answered
        if(testQuestions[i]['answers'][ii]['checked'] != undefined && testQuestions[i]['answers'][ii]['checked'] == 1){
          answered = true;
        }
        ii++;
      }

      if(answered){
        answeredCount++;
      }

      var className;
      if(testFinished){
        if(testQuestions[i]['correct']){
          className = 'kaire vertz';
        } else {
          className = 'kaire vertr';
        }
      } else {
        if(answered){
          className = 'kaire vertzydras';
        } else {
          className = 'kaire vertpilkas';
        }
      }

      if(questionNumber == i){
        className = className + ' current';
      }

      numberDiv.className = className;


      var linkSpan = document.createElement('span');
      linkSpan.className = 'tamsus';
      linkSpan.questionNumber = i;
      linkSpan.onclick = LibTest.setQuestion;
      linkSpan.appendChild(document.createTextNode('' + (i + 1)));
      numberDiv.appendChild(linkSpan);


      document.getElementById('testNumbers').appendChild(numberDiv);
      i++;
    }


    document.getElementById('testAnsweredQuestions').innerHTML = '';

    if(testFinished){
      var incorrectCount = 0;
      i = 0;
      while(i < testQuestions.length){
        if(!testQuestions[i]['correct']){
          incorrectCount++;
        }
        i++;
      }

      document.getElementById('testAnsweredQuestions').appendChild(document.createTextNode('Klaidos: ' + incorrectCount + '/' + testQuestions.length));

    } else {
      document.getElementById('testAnsweredQuestions').appendChild(document.createTextNode('Atsakyta: ' + answeredCount + '/' + testQuestions.length));
    }

    if(testFinished && testQuestions.length == 30){
      document.getElementById('testResult').innerHTML = '';
      if(incorrectCount > 6){
        document.getElementById('testResult').innerHTML = '<p>Jūs neišlaikėte egzamino. Per egzaminą galima padaryti 6 klaidas. Jūsų padarytos klaidos: ' + incorrectCount + '.</p>';
      } else {
        document.getElementById('testResult').innerHTML = '<p>Jūs išlaikėte egzaminą. Per egzaminą galima padaryti 6 klaidas. Jūsų padarytos klaidos: ' + incorrectCount + '.</p>';
      }
    }

    if(testFinished){
      document.getElementById('testBntResults').style.display = 'none';
      document.getElementById('testBntRenew').style.display = 'inline';
    } else {
      document.getElementById('testBntResults').style.display = 'inline';
      document.getElementById('testBntRenew').style.display = 'none';
    }

    testFirstPage = false;

  },

  previousQuestion : function (){
    testQuestionNumber--;
    if(testQuestionNumber < 0){
      testQuestionNumber = testQuestions.length - 1;
    }
    LibTest.showQuestion(testQuestionNumber);
  },

  nextQuestion : function (){
    testQuestionNumber++;
    if(testQuestionNumber > testQuestions.length - 1){
      testQuestionNumber = 0;
    }
    LibTest.showQuestion(testQuestionNumber);
  },


  setQuestion : function (){
    testQuestionNumber = this.questionNumber;
    LibTest.showQuestion(testQuestionNumber);
  },

  showResults : function (){
    testFinished = true;
    testQuestionNumber = 0;

    i = 0;
    while(i < testQuestions.length){ //loop through questions
      var ii;
      ii = 0;
      var correct;
      correct = true;
      while(ii < testQuestions[i]['answers'].length){ //check answers
        //alert(testQuestions[i]['answers'][ii]['teisingas'] + ' ' + testQuestions[i]['answers'][ii]['checked']);
        if(testQuestions[i]['answers'][ii]['checked'] == undefined){
          testQuestions[i]['answers'][ii]['checked'] = 0;
        }
        if(testQuestions[i]['answers'][ii]['teisingas'] != testQuestions[i]['answers'][ii]['checked']){
          correct = false;
        }
        ii++;
      }
      testQuestions[i]['correct'] = correct;
      i++
    }
    LibTest.showQuestion(0);
  },

  checkboxClick : function () {
    if(this.className == 'check kaire'){
      this.className = 'check1 kaire';
      testQuestions[this.questionNumber]['answers'][this.answerNumber]['checked'] = 1;
    } else {
      this.className = 'check kaire';
      testQuestions[this.questionNumber]['answers'][this.answerNumber]['checked'] = 0;
    }
  },

  renewTest : function () {
    document.location = document.location;
  },

  checkKey : function (e){

    var evt = e || window.event;
    if(evt.keyCode == 37){
      LibTest.previousQuestion();
    }
    if(evt.keyCode == 39){
      LibTest.nextQuestion();
    }
    if(evt.keyCode >= 48 && evt.keyCode <= 57){
      if(testFinished){
        return;
      }
      var key = evt.keyCode - 48;
      var checkbox = document.getElementById('mod_auto_test_' + testQuestionNumber + '_' + (key-1));
      if(checkbox){
        if(checkbox.className == 'check kaire'){
          checkbox.className = 'check1 kaire';
          testQuestions[checkbox.questionNumber]['answers'][checkbox.answerNumber]['checked'] = 1;
        } else {
          checkbox.className = 'check kaire';
          testQuestions[checkbox.questionNumber]['answers'][checkbox.answerNumber]['checked'] = 0;
        }

      }
      
    }

    if(evt.keyCode >= 96 && evt.keyCode <= 105){
      if(testFinished){
        return;
      }
      var key = evt.keyCode - 96;
      var checkbox = document.getElementById('mod_auto_test_' + testQuestionNumber + '_' + (key-1));
      if(checkbox){
        if(checkbox.className == 'check kaire'){
          checkbox.className = 'check1 kaire';
          testQuestions[checkbox.questionNumber]['answers'][checkbox.answerNumber]['checked'] = 1;
        } else {
          checkbox.className = 'check kaire';
          testQuestions[checkbox.questionNumber]['answers'][checkbox.answerNumber]['checked'] = 0;
        }

      }
    }
  }


}
