$(document).ready(function(){
	
	var f,currImage = $('#features li:last-child').prev().find('img').attr('src');
	
	$('#features li').each(function() {
		$(this).prependTo($(this).parent());
		$(this).clone().appendTo('#features_thumbs');
	});
	
	$('#features_thumbs li').each(function() {
		$(this).css('opacity', .5);
		$(this).find('a').attr('href','javascript:void(0);').click(function(){
			var src = $(this).find('img').attr('src'),ti;
			$('#features li').each(function() {
				ti = $(this).find('img').attr('src');
				if(src==ti && currImage!==ti)
				{
					resetFeature();
					$('#features li:last-child').before(this);
					setFeatureItem();
				}
			})
		});
	});
	
	$('#features_thumbs li:first-child').css('opacity', 1);
	
	function startFeature()
	{
		f = setInterval(function(){
			setFeatureItem();
		}, 10000);
	}
	
	function resetFeature()
	{
		clearInterval(f);
		startFeature();
	}
	
	function setFeatureItem()
	{
		currImage = $('#features li:last-child').prev().find('img').attr('src');
		$('#features li:last-child').fadeTo(500, 0, function(){
			$(this).prependTo($(this).parent()).css('opacity', 1);
		});
		$('#features_thumbs li').each(function(){
			var thumb = $(this);
			$(this).css('opacity', .5);
			if($(this).find('img').attr('src')==currImage){
				$(this).css('opacity', 1);
			}
		});
	}
	
	startFeature();
});