// 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() {
	jQuery('#archives div.archives_year').hide();
	y = jQuery('#f_archives_ym_years option:selected').attr('value');
	jQuery('#archives div.archives_year').filter('#archives_ym_'+ y).show();

	populate_select(jQuery('#f_search_mm_make'), makes);
	jQuery('#f_search_mm_make').change(function() {
		o = jQuery('#f_search_mm_model').empty().append('<option value="" class="first">All models</option>');
		populate_select(o, makes[this.value]);
	});
	jQuery('#f_search_mm .submit').click(function() {
		try {
			make  = jQuery('#f_search_mm_make' ).find('option:selected').attr('value') || '' ;
			model = jQuery('#f_search_mm_model').find('option:selected').attr('value') || '' ;
			window.location = site_w_path +'search/?make='+ make +'&model='+ model;
		}
		catch(e) {}
	});
	
	jQuery('#f_archives_ym_years').change(function() {
		jQuery('.archives_year')
			.hide()
			.filter('#archives_ym_'+ jQuery(this).find('option:selected').attr('value'))
			.fadeIn()
			.focus();
	});

	jQuery('#f_archives_ym_cat .submit').click(function() {
		ym  = jQuery('#f_archives_ym_cat').find('select[name=ym]  option:selected').attr('value');
		cat = jQuery('#f_archives_ym_cat').find('select[name=cat] option:selected').attr('value');
		window.location = site_w_path +'tests_and_features/'+ (cat ? cat +'/' : '') + (ym ? '?ym='+ ym : '');
	});
	
	tests_lead = 
		new dynlead()
		.wrapper('dynlead')
		.add('lead1')
		.addStroke();
});

