var ghtest = new Class
({
  canvas: null,
  questions: new Array(),
  answers: new Array(),
  goodAnswers: new Array(),
  currentQuestion: -1,
  goodCount: 0,

  initialize: function( canvasId, questionsArray )
  {
    this.canvas = $(canvasId);
    this.questions = questionsArray;
    this.goodAnswers = [2,1,1,2,2,1,1,1,1,1];
    this.overrideQuestions = [6,9];
    window.addEvent( 'domready', this.boot.bind( this ) );
  },
  
  clear: function ()
  {
    this.canvas.removeChild( this.canvas.getElement( 'h1' ) );
    this.canvas.removeChild( this.canvas.getElement( 'ul.answers' ) );
  },
  
  handleAnswer: function ( index, link )
  {
    this.saveAnswer( index, link );
    var counterId = 'questionTag['+ this.currentQuestion +']';
    $(counterId).set( 'class', 'done' );
    this.next();
    return false;
  },
  
  setCurrentCounter: function ()
  {
    var previous = this.canvas.getElement('li.current');
    if ( previous ) { 
      previous.removeClass( 'current' );
    }
    var counterId = 'questionTag['+ this.currentQuestion +']';
    $(counterId).addClass( 'current' );
  },
  
  saveAnswer: function ( index, link )
  {
    this.answers[this.currentQuestion] = index;
    if ( this.goodAnswers[this.currentQuestion] == index) {
      if ( this.overrideQuestions.contains( this.currentQuestion ) ) {
        // Override
        this.goodCount = 10;
      } else {
    	  this.goodCount++;
      }
    }
  },
  
  next: function ()
  {
    this.clear();
    if( this.isLastQuestion( this.currentQuestion ) ) {
      if(this.goodCount < 5) {
        this.paintForm('negative');
      } else {
        this.paintForm('positive');
      }
    } else {
      this.currentQuestion++;
      this.paintQuestion();
      this.setCurrentCounter();
    }
  },
  
  paintQuestion: function()
  {
    var question = this.questions[this.currentQuestion];
    var title = new Element( 'h1' ).set( 'text', question.question );
    var ul = new Element( 'ul' );
    ul.addClass( 'answers' );
    var myInstance = this;
    question.options.each( function( option, key ) {
      ul.appendChild( myInstance.paintOption( key, option ) );
    } );
    this.canvas.appendChild( title );
    this.canvas.appendChild( ul );
  },
  
  paintOption: function ( key, option )
  {
    var li = new Element( 'li' );
    var a = new Element( 'a' );
    var myInstance = this;
    key += 1;
    a.addEvent( 'click', function ( e ) { new Event(e).stop(); myInstance.handleAnswer( key, this ); } );
    a.href = '#';
    a.set( 'text', option );
    switch( key ) {
      case 1: li.addClass( 'one' ); break;
      case 2: li.addClass( 'two' ); break;
      case 3: li.addClass( 'three' ); break;
    }
    li.appendChild( a );
    return li;
  },
  
  paintCounter: function ()
  {
    var container = new Element( 'ul' );
    container.addClass( 'counterContainer' );
    var i = 0;    
    this.questions.each( function (question) {
      if( ! question ) return;
      var q = new Element( 'li' );
      q.set( 'text', i+1 );
      q.addClass( question['class'] );
      q.setProperty( 'id', 'questionTag['+ i +']' );
      container.appendChild( q );
      i++;
    });
    this.canvas.appendChild( container );
    this.setCurrentCounter();
  },

  paintIntro: function ()
  {
    var intro = new Element( 'div' );

    var next_button = new Element( 'button' );
    next_button.set( 'text', 'Start de test' );
    var self = this;
    next_button.addEvent('click', function(){
      this.canvas.removeChild( intro );
      self.currentQuestion++;
      self.paintCounter();
      self.paintQuestion();
    }.bind(this));
    intro.appendChild( next_button );
    this.canvas.appendChild( intro );
  },

  isLastQuestion: function ( counter )
  {
    if( counter >= this.questions.length-1 || !this.questions[counter+1]) {
      return true;
    } else {
      return false;
    }
  },
  
  isFirstQuestion: function ( counter )
  {
    if( counter == 0 ) {
      return true;
    } else {
      return false;
    }
  },
  
  paintForm: function ( type )
  {
		var title, form, action;
    if (type == 'positive') {
      title = new Element( 'p', {'class':'resultText'} ).set( 'text', 'Uw antwoorden geven aan dat een Stamrecht BV in uw situatie een geschikte oplossing kan zijn. Wilt u meer informatie over de stamrecht BV vul dan uw gegevens in. Wij sturen het boekje "Gouden Handdruk 2009" en nemen vrijblijvend contact met u op om u te informeren over de stamrecht BV.' );
			form = "stamrecht-bv-test";
			action = "/gouden-handdruk/informatie/stamrecht-bv-test/reactie.html";
    } else {
      title = new Element( 'p', {'class':'resultText'} ).set( 'text', 'Helaas, antwoorden geven aan dat een Stamrecht BV in uw situatie waarschijnlijk niet de beste oplossing is... Wij informeren u graag verder over de andere mogelijkheden voor uw gouden handdruk. Vul uw gegevens in en we nemen contact met u op.' );
			form = "contact";
			action = "/over-ons/contact.html";
    }
    title.inject( this.canvas );
		
    new Request.JSON({url: "/service/json/Form/view/" + form + ".html", onComplete: function( result ) {
      if ( result.success ) {
				var element = new Element( 'div', {'class':'clear'} );
        var html = result.message.stripScripts( true );
				element.set( 'html', html );
				if ( action ) element.getElement( 'form' ).set( 'action', action );
				element.inject( this.canvas );
      }
    }.bind( this ) } ).get();
  },
    
  boot: function ()
  {
    if( this.currentQuestion == -1 ) {
      this.paintIntro();
    } else {
      this.currentQuestion++;
      this.paintCounter();
      this.paintQuestion();
    }
  }
});
