// copyright 2008-2009 LC Média inc.

dynlead = function() {
	this.items   = new Array;
	this.i       = 0;
	this.n       = 0;
	this.speed   = 1000;
	this.running = 0;
	return this;
}

dynlead.prototype.wrapper = function(id) {
	node = document.getElementById(id);
	if (node) this.wrapper = node;
	return this;
}

dynlead.prototype.add = function(id) {
	node = document.getElementById(id);
	if (node) this.items[this.n++] = node;
	return this;
}

dynlead.prototype.incr = function() {
	if (++this.i == this.n) this.i = 0;
	return this;
}

dynlead.prototype.decr = function() {
	if (--this.i < 0) this.i += this.n;
	return this;
}

dynlead.prototype.init = function() {
	for (i = 1; i < this.n; i++) {
		node = this.items[i];
		if (i) jQuery(node).hide();
		else   jQuery(node).show();
	}
	this.start();
	return this;
}

dynlead.prototype.start = function() {
	var dynlead_this = this;
	this.timer = setInterval(function() { dynlead_this.forward() }, 5000);
	this.running = 1;
	return this;
}

dynlead.prototype.stop = function() {
	clearInterval(this.timer);
	this.running = 0;
	return this;
}

dynlead.prototype.forward = function() {
	this.incr();
	if (this.i)
		jQuery(this.items[this.i]).fadeIn(this.speed);
	else {
		jQuery(this.items[0]).show();
		last = this.n - 1;
		for (i = 1; i < last; i++) {
			jQuery(this.items[i]).hide();
		}
		jQuery(this.items[last]).fadeOut(this.speed);
	}
}

dynlead.prototype.rewind = function() {
	this.decr();
	last = this.n - 1;
	if (this.i < last) {
		jQuery(this.items[this.i  ]).show();
		jQuery(this.items[this.i+1]).fadeOut(this.speed);
	}
	else
		jQuery(this.items[this.i]).fadeIn(this.speed);
}

dynlead.prototype.addControls = function() {
	if (this.wrapper) {
		jQuery(this.wrapper).append('\
				<ul class="dynlead-controls">\
					<li class="dynlead-prev" ></li>\
					<li class="dynlead-pause"></li>\
					<li class="dynlead-next" ></li>\
				</ul>\
		');
		
		var lead = this;
		jQuery(this.wrapper).find('.dynlead-prev' ).click(function() {
			lead.stop();
			jQuery(this).parent().find('.dynlead-pause').addClass('dynlead-paused');
			lead.rewind();
		});
		jQuery(this.wrapper).find('.dynlead-next' ).click(function() {
			lead.stop();
			jQuery(this).parent().find('.dynlead-pause').addClass('dynlead-paused');
			lead.forward();
		});
		jQuery(this.wrapper).find('.dynlead-pause').click(function() {
			if (lead.running) {
				lead.stop(); 
				jQuery(this).addClass('dynlead-paused');
			}
			else {
				lead.start();
				jQuery(this).removeClass('dynlead-paused');
			}
		});
	}
	return this;
}

dynlead.prototype.addStroke = function() {
	if (this.wrapper) {
		jQuery(this.wrapper).find('.lead-text').each(function() {
			t = jQuery(this);
			t.before(t.clone().addClass('lead-stroke lead-stroke-tl'));
			t.before(t.clone().addClass('lead-stroke lead-stroke-tr'));
			t.before(t.clone().addClass('lead-stroke lead-stroke-bl'));
			t.before(t.clone().addClass('lead-stroke lead-stroke-br'));
		});
		jQuery(this.wrapper).find('.lead').each(function() {
			t = jQuery(this);
			t.find('.lead-text:last').wrapInner('<a href="'+ t.find('a').attr('href') +'"></a>');
		})
	}
	return this;
}
jQuery(document).ready(function() {
	home_lead =
		new dynlead()
		.wrapper('dynlead');
	
	jQuery('#dynlead .lead').each(function() {
		if (this.id) home_lead.add(this.id);
	});
	
	home_lead
		.addControls()
		.addStroke()
		.init();
		
	jQuery('#videos .header').html("Le Guide de l'auto <em>TV</em>");
	
	jQuery('#news .tabs a').click(function() {
		jQuery('#news .tabs a').removeClass('selected');
		jQuery(this).addClass('selected');
		jQuery('#nouvelles, #shows').hide();
		jQuery('#'+ this.rel).fadeIn();
		clearInterval(tab_timer);
		return false;
	});
	
	populate_select(jQuery('#f_vehiculezone_make'), makes);
	jQuery('#f_vehiculezone_make').change(function() {
		o = jQuery('#f_vehiculezone_model')
		o.find(':not(:first)').remove();
		populate_select(o, makes[this.value]);
	});
	jQuery('#f_vehiculezone .go').bind('click', function() {
		make  = jQuery('#f_vehiculezone_make' ).find('option:selected').attr('value');
		model = jQuery('#f_vehiculezone_model').find('option:selected').attr('value');
		if (make && model) {
			window.location = site_w_path +'constructeurs/'+ make +'/'+ model +'/';
			jQuery('#f_vehiculezone_make, #f_vehiculezone_model').attr('disabled', 'disabled');
		}
	});
	jQuery('#f_vehiculezone_make, #f_vehiculezone_model').removeAttr('disabled');
	
	populate_select(jQuery('#f_newcars_make'), makes);
	jQuery('#f_newcars_make').change(function() {
		o = jQuery('#f_newcars_model')
		o.find(':not(:first)').remove();
		populate_select(o, makes[this.value]);
	});
	jQuery('#f_newcars .go').bind('click', function() {
		make  = jQuery('#f_newcars_make' ).find('option:selected').attr('value');
		model = jQuery('#f_newcars_model').find('option:selected').attr('value');
		if (make && model) {
			window.location = site_w_path +'specifications/'+ make +'/'+ model +'/';
			jQuery('#f_newcars_make, #f_newcars_model').attr('disabled', 'disabled');
		}
	});
	jQuery('#f_vehiculezone_make, #f_vehiculezone_model').removeAttr('disabled');
	
	jQuery('#news .first:has(.thumb)').css({minHeight:'75px'}).find('p:first').css({display:'block'});
	

/*
	var tabs = ['nouvelles', 'shows'];
	var tab_i = 0;
	var tab_timer = setInterval(
		function() {
			jQuery('#news .tabs a').removeClass('selected');
			jQuery('#tab_'+ tabs[++tab_i%tabs.length]).addClass('selected');
			jQuery('#nouvelles, #shows').hide();
			jQuery('#'+ tabs[tab_i%tabs.length]).fadeIn(1500);
		},
		10000
	);
*/
});

