// JavaScript Document
Quiz = {
	afficher_score: true,
	legende_avant_score: '',
	legende_apres_score: '',
	interpolation_separateur: '',
	interpolation: {},

	couts: function(colonne)
	{
		return colonne;
	},

	configuration: {
		classeON:		'quiz_actif',
		classeVALIDE:	'quiz_vraie'
	},

	obt_td: function(e)
	{
		var elm = {};
		if(e)
		{
			if(e.target)
				elm = e.target;
			else if(e.srcElement)
				elm = e.srcElement;

			while(elm.tagName.toLowerCase() != 'td' && elm.tagName.toLowerCase() != 'th' && elm.tagName.toLowerCase() != 'table' && elm.parentNode)
				elm = elm.parentNode;
		}
		return elm;
	},
	choisir: function(e)
	{
		if(e)
		{
			var elm = this.obt_td(e);
			if(elm.tagName.toLowerCase() == 'td')
			{
				var n,p;
				for(n=0;n<elm.parentNode.childNodes.length;n++)
				{
					if(!isNaN(n))
					{
						try
						{
							elm.parentNode.childNodes[n].getElementsByTagName('div')[0].className = this.declasser(elm.parentNode.childNodes[n].getElementsByTagName('div')[0]);
							elm.parentNode.childNodes[n].getElementsByTagName('div')[0].parentNode.className = this.declasser(elm.parentNode.childNodes[n].getElementsByTagName('div')[0].parentNode);
						}
						catch(e) {
							elm.parentNode.childNodes[n].className = this.declasser(elm.parentNode.childNodes[n]);
							elm.parentNode.childNodes[n].parentNode.className = this.declasser(elm.parentNode.childNodes[n].parentNode);
						}
					}
				}
				elm.getElementsByTagName('div')[0].className += ' '+this.configuration.classeON;
				elm.getElementsByTagName('div')[0].parentNode.className += ' '+this.configuration.classeON;
			}

		}
	},
	corriger: function(e, out)
	{
		var score = 0;
		var nb = 0;
		var tableau = this.obt_td(e).parentNode.parentNode;
		var graphique = {};
		if(tableau.tagName.toLowerCase() == 'tbody' || tableau.tagName.toLowerCase() == 'table')
		{
			var n,p,a=0,b=0;
			for(n=0;n<tableau.childNodes.length;n++)
			{
				var tableau_n = tableau.childNodes[n];
				if(!isNaN(n) && typeof(tableau_n.tagName) != 'undefined' && tableau_n.tagName.toLowerCase() == 'tr')
				{
					a++;b=0;
					for(p=0;p<tableau_n.childNodes.length;p++)
					{
						var tableau_n_p = tableau.childNodes[n].childNodes[p];
						if(!isNaN(p) && typeof(tableau_n_p.tagName) != 'undefined' && tableau_n_p.tagName.toLowerCase() == 'td')
						{
							b++;var case_encours = tableau_n_p;
							var case_p = case_encours.getElementsByTagName('div')[0];
							// Question existante
							if(case_p.className.toLowerCase().indexOf(this.configuration.classeON.toLowerCase()) != -1
							|| case_p.className.toLowerCase().indexOf(this.configuration.classeVALIDE.toLowerCase()) != -1)
							{
								// Réponse validée
								case_p.className = this.declasser(case_p)+' '+this.configuration.classeVALIDE;
								case_p.parentNode.className = this.declasser(case_p.parentNode)+' '+this.configuration.classeVALIDE;
//								this.setopacity(case_p,0.9);
								graphique[a] = b;
								score += this.couts(b);
								nb++;
							}
						}
					}
				}
			}
		}
		if(nb < 6)
			score = 0;
		if(!out)
			return score;
		if(typeof(out) == 'string')
			out = document.getElementById(out);
		return this.populer(out, score, nb, graphique);
	},

	populer: function(obj, score, nb, graphique)
	{
		var html = '';
		if(typeof(graphique[3]) == 'undefined' && typeof(graphique[5]) == 'undefined'
		&& typeof(graphique[7]) == 'undefined' && typeof(graphique[9]) == 'undefined'
		&& typeof(graphique[11]) == 'undefined' && typeof(graphique[13]) == 'undefined')
			graphique = [graphique[2],graphique[4],graphique[6],graphique[8],graphique[10],graphique[12]];
		if(typeof(graphique[2]) == 'undefined' && typeof(graphique[4]) == 'undefined'
		&& typeof(graphique[6]) == 'undefined' && typeof(graphique[8]) == 'undefined'
		&& typeof(graphique[10]) == 'undefined' && typeof(graphique[12]) == 'undefined')
			graphique = [graphique[3],graphique[5],graphique[7],graphique[9],graphique[11],graphique[13]];
		html += '<ul class="conteneur_graph">';
		html += '<li>&nbsp;</li>';
		var i = 0;
		for(i=0;i<graphique.length;i++)
		{
			if(typeof(graphique[i]) == 'undefined')
				graphique[i] = 0;
			html += '<li class="element_graph_'+graphique[i]+'">&nbsp;</li>';
		}
		html += '</ul>';
		if(this.afficher_score && score)
			html += this.legende_avant_score + score + this.legende_apres_score;
		for(cfg in this.interpolation)
		{
			config = this.interpolation[cfg];
			if(config.min <= score && score <= config.max)
				html += config.texte + this.interpolation_separateur;
		}
		obj.innerHTML = html;
		return score;
	},

	declasser: function(obj)
	{
		if(typeof(obj.className) == 'undefined')
			return '';

		var aparser = obj.className.split(' ');
		for(p=0;p<aparser.length;p++)
		{
			if(!isNaN(p)
			&&(aparser[p] == this.configuration.classeON
			|| aparser[p] == this.configuration.classeVALIDE))
				aparser[p] = '';
		}
		return obj.className = aparser.join(' ');
	},
	
	setopacity: function(obj,value)
	{
		if(value < 0.0001)
			value = 0;
		try
		{
			obj.style.filter = 'alpha(opacity=' + (value * 100) + ')';
		}
		catch(e) {}
		try
		{
			obj.style.opacity = value;
		}
		catch(e) {}
	}
}