var Quiz = new Class(
{

	/**
	 * PROPERTIES
	 */
	aFields: [],
	aQuestions: [],

	/**
	 * METHODS
	 */
	initialize: function()
	{
		if( $( 'quizform' ) )
		{
			// disable the submit button
			this.disable_form();
			// get all fields
			this.get_fields();
			// get all the question id's
			this.get_questions();
			// add form events
			this.add_events();

			// check if page is being refreshed
			this.check_submit();
		}
		
		if ($('book-list'))
		{
			this.setupBookList();
		}
	},
	
	setupBookList: function()
	{
		$$('#book-list a.book-link').each(function(element)
		{
			element.removeEvents('click');
			element.addEvent('click', this.handleBookLinkClick.create({'bind':this}));
		}, this);
	},
	
	handleBookLinkClick: function(e)
	{
		var evt = new Event(e);
		evt.stop();
		
		var html = '';
		
		switch($(e.target).getProperty('id'))
		{
		case 'reisdagboek':
			break;
		case 'safari':
			break;
		case 'gladijs':
			break;
		default:
			alert($(e.target).getProperty('id') + ' is not implemented');
		}
		
		// Set the html of the book details element
	},

	disable_form: function()
	{
		// disable submit button
		$( 'submit_quiz' ).setProperty( 'disabled', true );
	},

	get_fields: function()
	{
		this.aFields = $$( '#quizform div input[name!=submit_quiz]', '#quizform div select' );
	},

	get_questions: function()
	{
		// get all questions and get names
		$$( '#quizform li' ).each( function( oQuestion )
		{
			this.aQuestions.push( oQuestion.getElement( 'input' ).getProperty( 'name' ) );
		}, this );
	},

	add_events: function()
	{
		// attach click events to all radio buttons, to see if submit button should be enabled
		$$( '#quizform ul input' ).each( function( oElement )
		{
			oElement.addEvent( 'click', this.check_submit.create({ 'bind': this }) );
		}, this );

		// add a submit forme ven handler.
		$( 'quizform' ).addEvent( 'submit', this.validate.create({ 'bind': this }) );
	},

	check_submit: function()
	{
		// every question has three answers
		var abc = [ 'a', 'b', 'c' ];
		// for each available question, check if an answer has been selected
		var checked = [];
		// iterator
		var i = 0;

		// loop the questions
		this.aQuestions.each( function( sQuestion )
		{
			// check each answer for being checked
			abc.each( function( letter )
			{
				// answer has been checked
				if( $( sQuestion + letter ).getProperty( 'checked' ) )
					checked[ i ] = true;

				else
				{
					if( !checked[ i ] )
						checked[ i ] = false;
				}
			}, this );

			// increment
			i++;
		}, this );

		var formValid = true;
		checked.each( function( check )
		{
			if( !check )
				formValid = false;
		}, this );

		if( formValid )
		{
			$( 'submit_quiz' ).setProperty( 'disabled', false );
		}

	},

	validate: function( eEvent )
	{
		// stop the form from submitting
		eEvent = new Event( eEvent );
		eEvent.stop();

		var aErrors = [];

		// loop the fields and check if they are filled out
		this.aFields.each( function( oField )
		{
			var sName	= oField.getProperty( 'name' );
			var sValue	= oField.getProperty( 'value' );

			if( sValue.trim() == '' && sName != 'use_email' )
			{
				aErrors.push( sName );
			}
		}, this );


		// some extra checks
		if( !$( 'email' ).value.trim().test( /^[a-z0-9]+(?:[-._][a-z0-9]+)*@[a-z0-9]+(?:[-.][a-z0-9]+)*(?:[.-][a-z0-9]{2,4})$/i ) && !aErrors.contains( 'email' ) )
			aErrors.push( 'email' );

		// reset label styles
		$$( '#quizform div label' ).setStyle( 'color', '' );

		aErrors.each( function( sError )
		{
			var sLabel = $E( 'label[for=' + sError + ']' );

			if( sLabel )
			{
				sLabel.setStyle( 'color', '#d90000' );
			}

		}, this );

		if( aErrors.length == 0 )
			this.send_data();
	},

	send_data: function()
	{
		$( 'submit_quiz' ).setProperty( 'disabled', true );

		var sRequest	= 'quizes/post?' + $( 'quizform' ).toQueryString();
		var sData	= 'path=' + encodeURIComponent(sRequest);
		var sUrl	= 'quiz_proxy.php';

		new Ajax( sUrl,
		{
			data: sData,
			method: 'get',
			onComplete: function( r )
			{
				var oSpan = new Element( 'span' );
				oSpan.setStyle( 'display', 'block' );
				oSpan.addClass( 'quiz_submitted' );
				oSpan.setHTML( 'Je gegevens zijn verzonden, bedankt!' );
				$( 'quizform' ).replaceWith( oSpan );
			}
		}).request();
	}

});

init_quiz = function()
{
	new Quiz();
};

window.addEvent( 'domready', init_quiz );