Wijzer = {
  history: []
  ,
  choices: []
  ,
  path: []
  ,
  loading: false
  ,
  install: function() {
    this.currentChoice = 1;
    this.retrieveChoices();
  }
  ,
  retrieveChoices: function() {
    this.loading = true;
    var me = this;
    $('content').addClass('content-loading');
    $$('#left_choice img')[0].src = '/images/empty.png';
    $$('#right_choice img')[0].src = '/images/empty.png';
    $('left_title').innerHTML = '';
    $('right_title').innerHTML = '';
    new Json.Remote("ajax", {onComplete: function(json){
      $('content').removeClass('content-loading');
      me.loading = false;
      if (json.result_fetish)
      {
        //--POST result id--
        document.to_result.action = json.result_fetish;
        document.to_result.result_id.value = json.result_id;
        document.to_result.submit()
        return
      }
      if (json.escape)
      {
          location = json.escape
          return
      }
      me.path = json.path;
      //delete the path property so it isn't pushed to the history
      delete json.path;
      me.history.push(json);
      me.setChoices(json);
    }}).send({
      'choices': this.choices,
      'history': this.history,
      'path'   : this.path
    });
  }
  ,
  setChoices: function(json) {
    $('left_choice').title = json.left.title.replace('_',' ');
    $('left_title').innerHTML = json.left.title.replace('_',' ');
    $$('#left_choice img')[0].alt = json.left.title.replace('_',' ');
    $$('#left_choice img')[0].src = json.left.image;
    $('right_choice').title = json.right.title.replace('_',' ');
    $('right_title').innerHTML = json.right.title.replace('_',' ');
    $$('#right_choice img')[0].alt = json.right.title.replace('_',' ');
    $$('#right_choice img')[0].src = json.right.image;
    //Set progress bar
    $('progress').style.width = (300 * ((this.currentChoice - 1) / config_wijzer_length)) + 'px';
  }
  ,
  choose: function(choice) {
    if(!this.loading) {
      this.choices.push(choice);
      this.retrieveChoices();
      this.currentChoice++;
    }
  }
}
window.addEvent('load', function() {
  Wijzer.install();
});
