var LoveTest = {

  _q_num: 1,
  _q_total: 8,
  _please_answer_the_question: 'Please answer the question',

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

    $('#inputzone').hide();
    $('#welcome_page').show();
    $('#container').removeClass('imagestep1').addClass('imagestep0');
    $('#question_zone .answers a').click(function(e) {e.preventDefault(); LoveTest.next();});
    $('#question_zone .open_answers a').click(function(e) {e.preventDefault(); LoveTest.next();});
    return true;
  },

  showPreface: function() {
    $('#welcome_page').hide();
    $('#preface_page').fadeIn('slow');
  },

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

    $('#welcome_page').hide();
    $('#preface_page').hide();
    $('.q' + this._q_num).show();
    $('#question_zone').fadeIn('slow');
  },

  next: function() {
    if (1 <= this._q_num == 1 && this._q_num <= 3) {
      var text_fields = $('.q'+this._q_num + ' .open_answer_field');
      for (var i=0, n=text_fields.length; i<n; ++i) {
        text_fields[i].value = jQuery.trim (text_fields[i].value);
        if (text_fields[i].value == '') {
          alert (this._please_answer_the_question);
          return;
        }
      }
    }

    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', 'answering_q'+this._q_num); // can use this class to track which question the user is answering
    }
  },

  endTest: function() {
    $('#question_zone').hide();

//    $('#question_gender').fadeIn('slow');
    this.end();
  },

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

    $('#container').removeClass('imagestep0').addClass('imagestep1');
    Set_Cookie('LoveTestWithPreface_completed', 'true');
    $('#inputzone').fadeIn('slow');
    $(Owlery).trigger('showMin');
  },

  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'] = LoveTest;

