var ConfiguratorCarsEffects = new Class({
	inCar: false,
	fx: {},
	stopDelay: '',
	last: null,
	cfg: {
		minOpacity: 0.3,
		maxOpacity: 0.99,
		duration: 500,
		delay: 1500
	},
	
	info: {
		famille: {},
		silhouette: {}
	},
	
	rangeTop: -1,

	/**
	 * Constructor
	 */
	initialize: function()
	{	
		query = new Ajax('/defaultSites/peugeot/php/criteres.php', {
			method:		'get',
			onSuccess:	this.loadXml.bind(this),
			onFailure:	function()
			{
				alert(LANGS.configurator.error);
			}
		});
		query.request(); 
		this.loadXml.bind(this);
	},
	
	/**
	 * Called when cars informations are loaded
	 */
	loadXml: function(data, xml)
	{
		// Get famille info
		/* var f = xml.getElementsByTagName('famille');
		for (var i = 0; i < f.length; i++)
		{
			var code = f.item(i).getAttribute('code');			
			var value = f.item(i).firstChild.nodeValue;

			this.info.famille[code] = value;		
		}
		
		// Get silhouette info
		var s = xml.getElementsByTagName('silhouette');
		for (var i = 0; i < s.length; i++)
		{
			var code = s.item(i).getAttribute('code');			
			var value = s.item(i).firstChild.nodeValue;

			this.info.silhouette[code] = value;		
		} */
		
		// Simulate position fixed for IE6
		if (window.ie6)
		{
			$('range_info_bloc').setStyle('position', 'absolute');
			this.rangePosition.periodical(1);
		}

		// Add events on cars
		$$('.range_item').each(function(el)
		{
			el.setOpacity(0.99);
			el.opts = {};
			el.addEvents({
				'mouseenter':	this.carEnter.bind(el),
				'mouseleave':	this.carLeave.bind(el),
				'click':		this.carClick.bind(el)
			});
		}.bind(this));
	},
	
	/**
	 * Called when mouse is over the car
	 */
	carEnter: function()
	{
		$clear(cfgEffect.stopDelay);
		
		// Add opacity effect on current car
		if (!this.hasClass('selected'))
		{
			this.setOpacity(cfgEffect.cfg.minOpacity);
			
			if ($type(cfgEffect.fx[this.id]) != false)
			{
				cfgEffect.fx[this.id].stop();
			}

			cfgEffect.fx[this.id] = new Fx.Style(this, 'opacity', {
				duration: cfgEffect.cfg.duration,
				fps: 70
			});
			cfgEffect.fx[this.id].start(cfgEffect.cfg.minOpacity, cfgEffect.cfg.maxOpacity);
		}
		
		// Low others cars opacity
		$$('.range_item').each(function(r)
		{
			if (r != this && r != cfgEffect.last && !r.hasClass('selected'))
			{
				if ($type(cfgEffect.fx[r.id]) != false)
				{
					cfgEffect.fx[r.id].stop();
				}
				r.setOpacity(cfgEffect.cfg.minOpacity);
			}
		}.bind(this));
		
		// Drop opacity effect on last car
		if (cfgEffect.last && cfgEffect.last != this && !cfgEffect.last.hasClass('selected'))
		{
			if ($type(cfgEffect.fx[cfgEffect.last.id]) != false)
			{
				cfgEffect.fx[cfgEffect.last.id].stop();
			}

			cfgEffect.fx[cfgEffect.last.id] = new Fx.Style(cfgEffect.last, 'opacity', {
				duration: cfgEffect.cfg.duration,
				fps: 70
			});
			cfgEffect.fx[cfgEffect.last.id].start(cfgEffect.last.getStyle('opacity'), cfgEffect.cfg.minOpacity);
		}
		cfgEffect.last = this;
	},
	
	/**
	 * Called when mouse leave the car
	 */
	carLeave: function()
	{
		cfgEffect.inCar = false;
		cfgEffect.stopDelay = cfgEffect.stopEffects.delay(cfgEffect.cfg.delay);
	},

	/**
	 * Called when we click on the car
	 */
	carClick: function()
	{
		if (this.hasClass('selected'))
		{
			return ;
		}
		
		// Refresh right menu informations
		var s = this.id.split('_');
		$('range_info_content').setHTML('');
		
		img = new Element('img');
		img.src = this.opts.bigTitle;
		img.width = 180;
		img.height = 40;
		$('range_info_content').appendChild(img);
		
		img = new Element('img');
		img.src = this.opts.bigImg;
		img.width = 200;
		img.height = 120;
		$('range_info_content').appendChild(img);
		
		/* var p_name = new Element('p');
			p_name.className = 'range_info_VU_name';
			p_name.appendText(this.opts.car_title);
			$('range_info_content').appendChild(p_name); */
		
		if(this.opts.car_price!='') {
			var p_price = new Element('p');
				p_price.className = 'range_info_VU_price';
				p_price.innerHTML = LANGS.configurator.beforeprice+' '+LANGS.configurator.beforepricespecial+this.opts.car_price+' '+LANGS.configurator.afterprice;
				$('range_info_content').appendChild(p_price);
		}
		
		var div = new Element('div');
		div.setAttribute('id', 'range_info_links');
			var span = new Element('span');span.addClass('b1');div.appendChild(span);
			var span = new Element('span');span.addClass('b2');div.appendChild(span);
			var span = new Element('span');span.addClass('b3');div.appendChild(span);
			var span = new Element('span');span.addClass('b4');div.appendChild(span);
			var divContent = new Element('div');
			divContent.addClass('bcontent');
				var ul = new Element('ul');
					if(this.opts.url!=null && this.opts.url!='') {
						var li = new Element('li');
							var a = new Element('a');
							a.setAttribute('href', this.opts.url);
							a.appendText(LANGS.configurator.models.discover_car + ' ' + this.opts.car_title);
							li.appendChild(a);
						ul.appendChild(li);
					}
					if(this.opts.url_conf!=null && this.opts.url_conf!='') {
						var li = new Element('li');
							var a = new Element('a');
							a.setAttribute('href', this.opts.url_conf);
							a.appendText(LANGS.configurator.models.configure_car + ' ' + this.opts.car_title);
							li.appendChild(a);
						ul.appendChild(li);
					}
					/* var li = new Element('li');
						var a = new Element('a');
						a.setAttribute('href', '#');
						a.appendText(LANGS.configurator.models.book_a_try);
						li.appendChild(a); 
					ul.appendChild(li);*/
				divContent.appendChild(ul);
			div.appendChild(divContent);
			var span = new Element('span');span.addClass('b4');div.appendChild(span);
			var span = new Element('span');span.addClass('b3');div.appendChild(span);
			var span = new Element('span');span.addClass('b2');div.appendChild(span);
			var span = new Element('span');span.addClass('b1');div.appendChild(span);
		$('range_info_content').appendChild(div);
		
		// Change select effect on cars
		$$('.selected').each(function(el)
		{
			if ($type(cfgEffect.fx[el.id]) != false)
			{
				cfgEffect.fx[el.id].stop();
			}

			cfgEffect.fx[el.id] = new Fx.Style(el, 'opacity', {
				duration: cfgEffect.cfg.duration,
				fps: 70
			});
			cfgEffect.fx[el.id].start(el.getStyle('opacity'), cfgEffect.cfg.minOpacity);
			el.removeClass('selected');
		});
		
		if (!this.hasClass('selected'))
		{
			this.addClass('selected');
		}
		if(typeof(pngfix)=='function') {
			pngfix();
		}
	},
	
	/**
	 * Called with a delay when mouse is out of a car, cancel all effects
	 */
	stopEffects: function()
	{
		if (!cfgEffect.inCar)
		{
			$$('.range_item').each(function(el)
			{
				if ($type(cfgEffect.fx[el.id]) != false)
				{
					cfgEffect.fx[el.id].stop();
				}
				el.setOpacity(cfgEffect.cfg.maxOpacity);
			});
		}
	},
	
	/**
	 * Called for IE6, in order to simulate position fixed
	 */
	rangePosition: function()
	{
		if (cfgEffect.rangeTop == -1)
		{
			cfgEffect.rangeTop = $('range_info_bloc').getTop();
		}
		$('range_info_bloc').setStyle('top', (cfgEffect.rangeTop + getScrollTop()) + 'px');

	}
});

window.addEvent('domready', function() {
	cfgEffect = new ConfiguratorCarsEffects();
});