var IQTest = {

  _q_num: 1,
  _q_total: 10,
  _choice: [],
  _quiz_cookie: 'IQTest_completed',

  init: function() {
    if (typeof sc_sentinel == 'function') {
      if (! sc_sentinel()) {
        return;
      }
    }
    else if (Get_Cookie(this._quiz_cookie) == 'true') {
      $(Owlery).trigger('repopulate');
      return;
    }

    // Prepare extra elements
    var d = new Date();
    var today = $('<span id="today"></span>').text(d.getDate() + '-' + (d.getMonth()+1) + '-' + d.getFullYear() + ' ');
    $('#highest_score_msg').prepend(today);

    // Register events
//    $('#question_zone .answers a').click(function(e) {e.preventDefault(); IQTest.next();});
    $('#question_zone .answers').each(function(answer_index, answer_div) {
      $(answer_div).children('a').each(function(anchor_index, anchor) {
        $(anchor).click(function(e) {
          e.preventDefault();
          IQTest._choice.push(anchor_index);
          IQTest.next();
        });
      });
    });

    var $body = $('body');
    if ($body.hasClass('de_DE')) { // DE only 5 questions
      IQTest._q_total = 5;
    }
    else if ($body.hasClass('nl_NL')) {
      $('#subtitle1').show();
    }

    $('#inputzone').hide();
    $('#question_index').show();
    $('#container').removeClass('imagestep1').addClass('imagestep0 imagestep_pre1');
  },

  startTest: function() {
    /* hide all question divs */
    for (var i = 1; i <= 10; i++) {
      $('.q' + i).hide();
    }

    $('.q' + this._q_num).show();

    $('#question_index').hide();
    $('#question_zone').fadeIn('slow');

    $('#container').removeClass('imagestep_pre1').addClass('imagestep_pre2');
  },

  next: function() {
    this._q_num++;

    /* hide all question divs */
    for (var i = 1; i <= this._q_total; i++)
      $('.q' + i).hide();

    $('#question_zone').show();
    $('.q' + this._q_num).show();

    if (this._q_num > this._q_total) {
      this.endTest();
    }
    else {
      $('#question_zone').attr('class', 'q'+this._q_num);
    }
  },

  endTest: function() {
    $('#question_zone').hide();
    if ($('body').hasClass('en_NZ')) {
      this.end();
      return;
    }

    $('#container').removeClass('imagestep_pre2').addClass('imagestep_pre3');

    var question_gender = $('#question_gender');
    if (question_gender.length) {
      $('#question_gender').fadeIn('slow');
    }
    else {
      this.end();
    }
  },

  end: function() {
    $('#operatorlist').val($('#question_operatorlist').val());
    $('#question_gender').hide();

    $('#container').removeClass('imagestep0 imagestep_pre1 imagestep_pre2 imagestep_pre3').addClass('imagestep1');
    Set_Cookie(this._quiz_cookie, 'true');
    $('#inputzone').fadeIn('slow');
    $(Owlery).trigger('showMin');

    if (typeof operator_selector == 'object') {
      operator_selector.handle();
    }
  },

  checkTerms: function(message) {
    if (! $('#terms').length) return true;
    if ($('#terms').attr('type') == 'hidden') return true;
    if (! $('#terms').attr('checked')) {
      // highlight the checkbox
      $('#term_warn_box').css('visibility', 'visible');
      $('#term_cb_wrap').addClass('term_cb_highlight');
      alert(message);
      return false;
    }
    return true;
  }
};

landingControlHolder['precontroller'] = IQTest;

top.window.moveTo(0, 0);
if (document.all) {
  top.window.resizeTo(screen.availWidth, screen.availHeight);
}
else if (document.layers || document.getElementById) {
  if (top.window.outerHeight < screen.availHeight || top.window.outerWidth < screen.availWidth) {
    top.window.outerHeight = screen.availHeight;
    top.window.outerWidth = screen.availWidth;
  }
};

