var ConfiguratorCarsEffects = new Class(
{
	/*************************************************************************************************************************
	 * Variable
	 *************************************************************************************************************************/
	timer: '',
	last_car: '',
	last_ligne: '',
	preload_image: '',
	preload_name: '',
	
	cfg: {
		delay: 300
	},
	
	carsInfos: {},
	
	
	
	
	/*************************************************************************************************************************
	 * Constructor
	 *************************************************************************************************************************/
	initialize: function()
	{
		/* 	utilisez this.carsInfos.info et carsInfos.opts  pour recuperer les infos des vehicules	*/
		//on charge les evenements
		this.recup_Infos();
		this.load();
	},
	
	
	
	/*************************************************************************************************************************
	 * Evenements
	 *************************************************************************************************************************/
	load: function()
	{
		//	//on cache le panel info
		$('info_panel').setStyle('display','none');
		//evenements lie au panel  des vehicules
		$$('div.range_item').each(function(el)
			{
				el.addEvents({
					'mouseenter':	this.carEnter.pass([el], this),
					'mouseleave':	function(){}
				});
			}.bind(this));
		//evenements lie au changement de ligne
		$$('div.range_items').each(function(ligne)
			{
				ligne.addEvents({
					'mouseenter':	this.listEnter.pass([ligne], this),
					'mouseleave':	function(){}
				});
			}.bind(this));
		//evenement lie a la sortie du cadre des vehicules
		$('range_lists').addEvents({
					'mouseenter':	function(){},
					'mouseleave':	this.cadreLeave.pass([], this)
				});
		//evenements lie au changement de type de vehicule
		$$('h3').each(function(ligne)
			{
				ligne.addEvents({
					'mouseenter':	this.cadreLeave.pass([], this),
					'mouseleave':	function(){}
				});
			}.bind(this));
			
	},
	
	
	
	/*************************************************************************************************************************
	 * Load infos
	 *************************************************************************************************************************/
	recup_Infos: function()
	{
		//on charges les infos des vehicules a partir d'un autre fichier script
		this.carsInfos = new cars_Infos();
	},
	
	/*************************************************************************************************************************
	 * Functions
	 *************************************************************************************************************************/
	 /*************************************************** Fonctions utiles *****************************************/
	//fonction pour formater le prix
	number_format: function(number, decimals, dec_point, thousands_sep)
	{
		var n = number, prec = decimals;
		
		var toFixedFix = function (n,prec) 
		{
			var k = Math.pow(10,prec);
			return (Math.round(n*k)/k).toString();
		};
		
		n = !isFinite(+n) ? 0 : +n;
		prec = !isFinite(+prec) ? 0 : Math.abs(prec);
		var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
		var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;
		
		var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
		
		var abs = toFixedFix(Math.abs(n), prec);
		var _, i;
		
		if (abs >= 1000) 
		{
			_ = abs.split(/\D/);
			i = _[0].length % 3 || 3;
		
			_[0] = s.slice(0,i + (n < 0)) +
				  _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
			s = _.join(dec);
		} 
		else 
		{
			s = s.replace('.', dec);
		}
		
		var decPos = s.indexOf(dec);
		if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) 
		{
			s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
		}
		else if (prec >= 1 && decPos === -1) 
		{
			s += dec+new Array(prec).join(0)+'0';
		}
		
		return s;
	},
	
	//evenement lorsqu'on passe la souris sur un vehicule
	carEnter: function(el)
	{
		this.show_infoPanel_timer(el,this.cfg.delay);
	},
	
	//lorqu'on change de list on cache le panel et le deploy
	listEnter : function(ligne) 
	{
		//this.change_list(ligne);
	},
	
	//pour cacher le panel
	cadreLeave : function()
	{
		this.hide_infoPanel(0);
	},
	
	//declencher le timer pour montrer l'info panel avec le nouvel vehicule
	show_infoPanel_timer : function(el,time) 
	{
		//pour eviter de montrer les voitures survoles sans temps d'arret
		$clear(this.timer);
		this.last_car = el;
		
		var s = el.id.split('_');
		
		//on precharge l'image avant de lancer le timer pour quel apparraissent plus rapidement
		this.preload_image = new Element('img');
			this.preload_image.src = this.carsInfos.opts.url_bigImg[s[1]];
			this.preload_image.width = 200;
			this.preload_image.height = 120;
		//on lance le timer pour montre l'info panel
		this.timer = this.show_infoPane.delay(time, this, [el]);
	},
	
	//affiche le pannel d'info avec les nouvelles infos
	show_infoPane: function(el)
	{
		//$('info_panel').getElement('.topSelector').setStyle('left',el.getPosition($('range_lists')).x+15);
		
		//taille du texte
		var tailleText = parseInt(el.getFirst().getStyle('height'));
		
		$$('.range_name').removeClass('selected');
		el.getElement('.range_name').addClass('selected');
		
		 // recup de l'id
		 var s = el.id.split('_');
		 
		//chargement de la nouvelle l'image sans chargement
		$('info_img').set('html','');
		$('info_img').appendChild(this.preload_image);
		if (Browser.Engine.trident && Browser.Engine.version == 4)//mozilla ff
		{
			if (typeof pngfix=='function')
				pngfix();
		}
		
		//changer  les infos et le prix du vehicule de la recap box
		$('info_desc').set('html','');
				//comments
				var p = new Element('p');
					p.set('html', this.carsInfos.info.comments[s[1]]);
					$('info_desc').appendChild(p);
				//prix 1
				var p = new Element('p');
					p.set('class', 'tarif');
					if ( this.carsInfos.showPrice )
						if ( this.carsInfos.info.prix[s[1]] != '' )
							if ( noShowAfterPrice != true )
								p.set('html',this.carsInfos.trad.begin_price + ' ' + this.carsInfos.trad.before_price + ' ' +this.carsInfos.info.prix[s[1]] + ' ' + this.carsInfos.trad.after_price);
							else p.set('html',this.carsInfos.trad.begin_price + ' ' + this.carsInfos.trad.before_price + ' ' +this.carsInfos.info.prix[s[1]]);
					else
						if ( this.carsInfos.replacePriceLabel != '' )
							p.set('html',this.carsInfos.trad.begin_price + ' ' + this.carsInfos.trad.before_price + ' ' +this.carsInfos.replacePriceLabel + ' ' + this.carsInfos.trad.after_price);
					$('info_desc').appendChild(p);
				//prix 2 si il y a
				if ( useLocalPrice )
				{
					var second_price = parseFloat(this.carsInfos.info.prix[s[1]]) * parseFloat(localPriceConvert) * 1000;// * 1000 car this.carsInfos.info.prix[s[1]] ---> est deja converti ! ex 9000 --> 9.0
					if ( this.carsInfos.trad.after_price != undefined && this.carsInfos.trad.after_price != '' )
					{
						var before_second_price = '';
						var after_second_price = localPriceUnit.toLowerCase();
					}
					else
					{
						var before_second_price = localPriceUnit.toLowerCase();
						var after_second_price = '';
					}
					var p = new Element('p');
						p.set('class', 'tarif2');
						p.set('html', '(&nbsp;' + before_second_price + ' ' + this.number_format(second_price , 0 , "," , ".") + ' ' + after_second_price +'&nbsp;)' );
						$('info_desc').appendChild(p);
				}
		
		//changer le contenu du cadre menu de la recap box
		$('btn').set('html','');
				var ul = new Element('ul');
					//lien decouvrir --> on fais apparaitre le lien que si ce dernier existe
					if((this.carsInfos.opts.url[s[1]]) && (this.carsInfos.opts.url[s[1]]!='#'))
					{
					var li = new Element('li');
						var a = new Element('a');
						a.setAttribute('href', this.carsInfos.opts.url[s[1]]);
						if ( this.carsInfos.reverseName )
							a.set('html',this.carsInfos.info.famille[s[1]] + ' ' + this.carsInfos.info.silhouette[s[1]] + ' ' + this.carsInfos.trad.discover);
						else
							a.set('html',this.carsInfos.trad.discover + ' ' + this.carsInfos.info.famille[s[1]] + ' ' + this.carsInfos.info.silhouette[s[1]]);
						if ( this.carsInfos.showFullName )
							a.set('html',this.carsInfos.trad.discover);
						if ( this.carsInfos.reset_links == 1 )
							a.set('html',this.carsInfos.trad.discover[s[1]]);
						if ( this.carsInfos.trad_alternate.discover[s[1]] ) {
							a.set('html',this.carsInfos.trad_alternate.discover[s[1]]);
						}
						li.appendChild(a);
					ul.appendChild(li);
					}
					//lien configurer
					if((this.carsInfos.opts.url_conf[s[1]]!=null) && (this.carsInfos.opts.url_conf[s[1]]!='#') && (this.carsInfos.opts.url_conf[s[1]]!='')) 
					{
						var li = new Element('li');
							var a = new Element('a');
							a.setAttribute('href', this.carsInfos.opts.url_conf[s[1]]);
							if ( this.carsInfos.reverseName )
								a.set('html',this.carsInfos.info.famille[s[1]] + ' ' + this.carsInfos.info.silhouette[s[1]] + ' ' + this.carsInfos.trad.configure);
							else
								a.set('html',this.carsInfos.trad.configure + ' ' + this.carsInfos.info.famille[s[1]] + ' ' + this.carsInfos.info.silhouette[s[1]]);
							if ( this.carsInfos.showFullName )
								a.set('html',this.carsInfos.trad.configure);
							if ( this.carsInfos.reset_links == 1 )
								a.set('html',this.carsInfos.trad.configure[s[1]]);
							if ( this.carsInfos.trad_alternate.configure[s[1]] ) {
								a.set('html',this.carsInfos.trad_alternate.configure[s[1]]);
							}
							li.appendChild(a);
						ul.appendChild(li);
					}
					//lien pour demander un essai
					if((this.carsInfos.opts.url_tdrive[s[1]]) && (this.carsInfos.opts.url_tdrive[s[1]]!='#')) 
					{
						var li = new Element('li');
							var a = new Element('a');
							if(this.carsInfos.opts.url_tdrive_onclick[s[1]]) {
								a.setAttribute('onClick',this.carsInfos.opts.url_tdrive_onclick[s[1]]);
								a.setAttribute('href',this.carsInfos.opts.url_tdrive[s[1]]);
							}
							else {
								a.setAttribute('href', this.carsInfos.opts.url_tdrive[s[1]]);
							}
							a.set('html',this.carsInfos.trad.test);
							if ( this.carsInfos.reset_links == 1 )
								a.set('html',this.carsInfos.trad.test[s[1]]);
							if ( this.carsInfos.trad_alternate.test[s[1]] ) {
								a.set('html',this.carsInfos.trad_alternate.test[s[1]]);
							}
							li.appendChild(a);
						ul.appendChild(li);
					}
		$('btn').appendChild(ul);
		
		//positionnement du deploy en horizontal et chargement de l'image du deploy
		$('selectorArrowTop').setStyle('left',el.getCoordinates().left+35);
		$('selectorArrowTop').setStyle('top',el.getCoordinates().top+tailleText);
		$('selectorArrowTop').setStyle('display','block');
		
		//positionnement en verticale
		$('info_panel').setStyle('top', el.getPosition().y+10+tailleText);
		
		//on montre le panel avec les nouveaux infos
		$('info_panel').setStyle('display', 'block');
	},
	
	//cacher l'info panel et supprimer le deploy de l'onglet
	hide_infoPanel : function(time) 
	{
		$clear(this.timer);
		(function () {
				$('info_panel').setStyle('display', 'none');
			}.bind(this)
		).delay(time); 
		$('selectorArrowTop').setStyle('display','none');
	},
	
	//lorsqu'on change de ligne on cache l'infopanel et le deploy instantanement 
	change_list : function(ligne) 
	{
		if ( !(this.last_ligne == ligne ) )
		{
			//$('topTabLabel').setStyle('display', 'none');
			$('info_panel').setStyle('display', 'none');
			//on prepare la hauteur a l'avance du panel pour eviter un bug d'affichage (adjust)
			if (Browser.Engine.gecko)//mozilla ff
			{
				$('info_panel').setStyle('top', $(ligne).getPosition().y+42);
			}
			else //i.e 6
				if (Browser.Engine.version == 4) //ie 6 -> version 4
				{
					$('info_panel').setStyle('top', $(ligne).getPosition().y+34);
				}
				else //ie 7 8 et otre
				{
					$('info_panel').setStyle('top', $(ligne).getPosition().y+41);
				}
		}
		this.last_ligne = ligne;
	}
	
	
	
});//fin class
	
	
	
//dom ready
window.addEvent('domready', function() {
	cfgEffect = new ConfiguratorCarsEffects();
	if ( Browser.Engine.trident && Browser.Engine.version == 4 )
	{
		if ( typeof pngfix!='function' )
		{
			var myScript = new Asset.javascript('/defaultSites/peugeot/js/pngfix.js', {
				id: 'myScript', 
				onload: function(){
					;
				}
			});
		}
	}
});
