

if(typeof(benchmark) == "undefined") { var benchmark = new Object(); }
if(typeof(benchmark.framework) == "undefined") { benchmark.framework = new Object(); }
if(typeof(benchmark.framework.ihm) == "undefined") { benchmark.framework.ihm = new Object(); }
if(typeof(benchmark.framework.ihm.form) == "undefined") { benchmark.framework.ihm.form = new Object(); }


benchmark.framework.ihm.form.ObjItemScore = function()
{
	this.classOn 		= 'scoreEtoileOn';
	this.classOff 		= 'scoreEtoileOff';
	this.classHover 		= 'scoreEtoileHover';
	this.classDiv 		= 'scoreEtoileDiv';
	this.classDescription	= 'scoreDescription';
	this.notes			= {};
	this.readonly		= false;
	this.nullPermis		= false;
	this.valeur			= null;
	this.descriptionDefaut 	= 'Choisir';
	this.descriptionActuel 	= this.descriptionDefaut;
	this.actif			= false;
};

benchmark.framework.ihm.form.ObjScore = function()
{
	
	this.tableauItemScore = {};
	
	
	this.selecteurParDefaut = 'input[type=radio][score]';
	this.genereScore = function(item)
	{
		var score = 	 
					
					'<div class="'+benchmark.execution.objScore.tableauItemScore[item].classDiv+'" id="score_'+item+'" >'+
					
					'<input type="hidden" value="'+benchmark.execution.objScore.tableauItemScore[item].valeur+'" '+
					' name="'+item+'" '+
					' id="'+item+'" >';
					
		for (var i in benchmark.execution.objScore.tableauItemScore[item].notes )
		{
			if( benchmark.execution.objScore.tableauItemScore[item].notes[i].valeur > benchmark.execution.objScore.tableauItemScore[item].valeur )
			{
				score += '<div class="score '+benchmark.execution.objScore.tableauItemScore[item].classOff+'" value="'+benchmark.execution.objScore.tableauItemScore[item].notes[i].valeur+'">&nbsp;</div>';
			}
			else
			{
				score += '<div class="score '+benchmark.execution.objScore.tableauItemScore[item].classOn+'" value="'+benchmark.execution.objScore.tableauItemScore[item].notes[i].valeur+'">&nbsp;</div>';
			}
		}
		
		if(!benchmark.execution.objScore.tableauItemScore[item].readonly){
			score += '<div'+ 
				' id = "score_description_'+item+'" '+ 
				' name = "score_description_'+item+'" '+ 
				' class="'+benchmark.execution.objScore.tableauItemScore[item].classDescription+'">'+
				benchmark.execution.objScore.tableauItemScore[item].descriptionActuel+'</div>';
		}
		
		score += '&nbsp;</div>';

		return score;
	};
	
	
	
	this.redessineScore = function(item)
	{
		$('#score_'+item)
			.replaceWith(this.genereScore(item));
		// on rajoute le comportement :
		this.bindComportement(item);
	};
	
	
	this.changeScore = function(item,score)
	{
		
		this.tableauItemScore[item].valeur = score;
		$('#'+item).attr('value',score);

		
		if(score != 0 && score != null && score != 'null' )
		{
			this.tableauItemScore[item].descriptionActuel = this.tableauItemScore[item].notes[score].description;
		}
		else
		{
			this.tableauItemScore[item].descriptionActuel = this.tableauItemScore[item].descriptionDefaut;
		}
		
		$('#score_'+item+' div.score')
			
			.addClass(this.tableauItemScore[item].classOff)
			.removeClass(this.tableauItemScore[item].classOn+' '+this.tableauItemScore[item].classHover)
			
			.each(
				function(i)
				{
					if( 
						( score != 0 && score != null && score != 'null' ) 
						&&  benchmark.execution.objScore.tableauItemScore[item].valeur >= $(this).attr('value') 
						)
					{
						$(this)
							.removeClass(benchmark.execution.objScore.tableauItemScore[item].classOff)
							.addClass(benchmark.execution.objScore.tableauItemScore[item].classOn)
							;
					}
				}
			);
	};
	
	
	this.bindComportement = function(item)
	{
		$('#score_'+item+' div.score')
			
			.bind(
				'mouseover',
					{ 
						item	: benchmark.execution.objScore.tableauItemScore[item],
						nom	: item
					},
				function(e)
				{
					
					if ( !e.data.item.readonly )
					{
						
						var scoreSurvol = $(this).attr('value');
						
						$('#score_'+e.data.nom+' div.score')
							.each(
								function()
								{
									
									if( $(this).attr('value') <= scoreSurvol )
									{
										$(this).addClass(e.data.item.classHover);
									}
								}
							);
						
						$('#score_description_'+e.data.nom).html(e.data.item.notes[scoreSurvol].description)
					}
				}
			)
			
			.bind(
				'mouseout',
					{ 
						item	: benchmark.execution.objScore.tableauItemScore[item],
						nom	: item
					},
				function(e)
				{
					if ( !e.data.item.readonly )
					{
						$('#score_'+e.data.nom+' div.score')
							.removeClass(e.data.item.classHover);
						
						$('#score_description_'+e.data.nom).html(e.data.item.descriptionActuel)
					}
				}
			)
			
			.bind(
				'dblclick',
					{ 
						item	: benchmark.execution.objScore.tableauItemScore[item],
						nom	: item
					},
				function(e)
				{
					if ( !e.data.item.readonly && e.data.item.nullPermis )
					{
						
						benchmark.execution.objScore.changeScore(e.data.nom,'null'); 
					}
					e.stopPropagation();
				}
			)							
			
			.bind(
				'click',
					{ 
						item	: benchmark.execution.objScore.tableauItemScore[item],
						nom	: item
					},
				function(e)
				{
					if ( !e.data.item.readonly )
					{
					
						benchmark.execution.objScore.changeScore(e.data.nom,$(this).attr('value')); 
					}
					
				}
			)
			.each(
				function()
				{
					
					if
					( 
						typeof( benchmark.framework.ihm ) != 'undefined' &&
						typeof( benchmark.framework.ihm.effet ) != 'undefined' &&
						typeof( benchmark.framework.ihm.effet.ObjEffet ) != 'undefined' &&
						typeof( benchmark.framework.ihm.effet.ObjEffet.desactiveSelection ) == 'function' 
					)
					{
						benchmark.framework.ihm.effet.ObjEffet.desactiveSelection(this);
					}
				}
			)
			;

	};
	
	
	this.initialise = function(selecteur)
	{
	
		if(typeof(selecteur) == 'string')
		{
			selecteur = selecteur + ' ' +this.selecteurParDefaut;
		}
		else
		{
			selecteur = this.selecteurParDefaut;
		}

		
		$(selecteur)
			.each(
				//{}, ce modèle propre semble ne pas fonctionner dans notre version de jquery donc accès direct :/
				function(i)
				{
					
					var nom 			= $(this).attr('name');
					var value 			= $(this).attr('value');
					var readonly		= $(this).attr('readonly');
					var nullPermis		= $(this).attr('nullPermis');
					var description 		= $(this).attr('score');
					var classOn			= $(this).attr('classOn');
					var classOff		= $(this).attr('classOff');
					var classHover		= $(this).attr('classHover');
					var classDiv		= $(this).attr('classDiv');
					var classDescription	= $(this).attr('classDescription');
					var descriptionDefaut	= $(this).attr('descriptionDefaut');
					
					
					if (typeof(nom) == 'undefined') { alert('erreur, il faut définir un ID au radio!');return false;}
					
					if(typeof(benchmark.execution.objScore.tableauItemScore[nom]) == 'undefined' )
					{
						benchmark.execution.objScore.tableauItemScore[nom] = new benchmark.framework.ihm.form.ObjItemScore();
					}
					
					
					if ( typeof(value) == 'undefined' ) { alert('erreur, il faut définir un radio pour chaque score possible');return false;}
					else {
						benchmark.execution.objScore.tableauItemScore[nom].notes[value] = {};
						benchmark.execution.objScore.tableauItemScore[nom].notes[value].valeur = value;
					}
					
					if ( typeof(description)!= 'undefined') { benchmark.execution.objScore.tableauItemScore[nom].notes[value].description = description;}
					
					if(typeof(readonly)		== 'boolean'&& readonly ) 			  { benchmark.execution.objScore.tableauItemScore[nom].readonly		= readonly; }
					if(typeof(nullPermis) 		== 'string' && nullPermis 		!= '' ) { benchmark.execution.objScore.tableauItemScore[nom].nullPermis		= ( nullPermis == 'true' ? true : false ); }
					if(typeof(classOn) 		== 'string' && classOn 			!= '' ) { benchmark.execution.objScore.tableauItemScore[nom].classOn		= classOn; }
					if(typeof(classOff)		== 'string' && classOff			!= '' ) { benchmark.execution.objScore.tableauItemScore[nom].classOff 		= classOff; }
					if(typeof(classHover)		== 'string' && classHover		!= '' ) { benchmark.execution.objScore.tableauItemScore[nom].classHover		= classHover; }
					if(typeof(classDiv)		== 'string' && classDiv			!= '' ) { benchmark.execution.objScore.tableauItemScore[nom].classDiv 		= classDiv; }
					if(typeof(classDescription)	== 'string' && classDescription	!= '' ) { benchmark.execution.objScore.tableauItemScore[nom].classDescription = classDescription; }
					if(typeof(descriptionDefaut)	== 'string' && descriptionDefaut	!= '' ) { benchmark.execution.objScore.tableauItemScore[nom].descriptionDefaut= descriptionDefaut; benchmark.execution.objScore.tableauItemScore[nom].descriptionActuel = descriptionDefaut; }
				}
			)
			;
		
		$(selecteur+':checked')
			.each(
				function(i)
				{
					var id = $(this).attr('id');
					var valeur = $(this).attr('value');
					
					benchmark.execution.objScore.tableauItemScore[id].valeur = valeur;
					
					benchmark.execution.objScore.tableauItemScore[id].descriptionActuel = benchmark.execution.objScore.tableauItemScore[id].notes[valeur].description;
				}
			);
		
		for ( var item in benchmark.execution.objScore.tableauItemScore)
		{
		
			if( $(this.selecteurParDefaut+'[name='+item+'][type=radio]').length > 0 )
			{
				benchmark.execution.objScore.tableauItemScore[item].actif = true;
			
				if ( $('#score_'+item).attr('id') != 'score_'+item )
				{
					
					$('input[name='+item+']:first')
						.before(
							this.genereScore(item)
						)
					
					this.bindComportement(item);
					
					
					$(this.selecteurParDefaut+'[name='+item+']').remove();
				}
			}
			
		}
	};
	
};



$(document).ready(
	function()
	{
		
		if(typeof(benchmark.execution) == "undefined") { benchmark.execution = new Object(); }
		if(typeof(benchmark.execution.objScore) == "undefined") 
		{ 
			benchmark.execution.objScore = new benchmark.framework.ihm.form.ObjScore();
		}
		benchmark.execution.objScore.initialise();
	}
);

