/* Gestion des questions */

function startTests() {
	courant = 1;
	total = $$('#test fieldset.question').length;
	if (total == 0) return;
	score = 0;
	redraw(courant, total);
	$('score').setProperty('value', appreciations[score]+ ' ('+score + '/'+total+')');
	$$('#test fieldset input[type=radio]').each( function(choix) {
		choix.addEvent('click', function() {
			//Afficher l'erreur
			$(choix).getParent().set('tween', {
				duration: 200,
				onComplete: function () {
						if (courant <= total) {
							if (choix.get('value') == reponse) { score++; }
							$('score').set('value', appreciations[score]+ ' ('+score + '/'+total+')');
							courant++;
							redraw(courant, total);
						}
					}
			});
			reponse = $(choix.get('name').replace('q','r')).get('value');
			if (choix.get('value') != reponse) {
				choix.parentNode.tween('color', '#93161c');
				choix.parentNode.setStyle('background', 'url(images/cancel.png) no-repeat right top');
			} else {
				choix.parentNode.tween('color', '#656d20');
				choix.parentNode.setStyle('background', 'url(images/accept.png) no-repeat right top');
			}
		});
	} );
}

function redraw(courant, total) {
	$$('#test fieldset.question').each( function(f) {
		if ((courant > total) || (f.getProperty('id') == 'question_'+courant)) {
			f.setStyle('display','block');
		} else {
			f.setStyle('display','none');
		}
	} );
	if (courant > total) location.hash = '#question_'+courant;
	
	$$('#test fieldset#resultat').each( function(f) {
	total++;
		if ((courant == total)) {
			f.setStyle('display','block');
		} else {
			f.setStyle('display','none');
		}
	} );
}

window.addEvent('domready', startTests);

