// 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();

	populate_select(jQuery('#f_vehiculezone_make'), makes);
	jQuery('#f_vehiculezone_make').change(function() {
		o = jQuery('#f_vehiculezone_model').empty().append('<option value="" class="first">all models</option>');
		populate_select(o, makes[this.value]);
	});
	jQuery('#f_vehiculezone .submit').click(function() {
		try {
			make  = jQuery('#f_vehiculezone_make' ).find('option:selected').attr('value') || '' ;
			model = jQuery('#f_vehiculezone_model').find('option:selected').attr('value') || '' ;
			window.location = site_w_path +'search/?make='+ make +'&model='+ model;
		}
		catch(e) {}
	});
	
	jQuery('#news .first:has(.thumb)').css({minHeight:'75px'}).find('p:first').css({display:'block'});
});

