$(document).ready(function(){
	
	var selectedPromo = 1;
	var homePromoTimer = 0;
	
	function switchHomePromo(n) {
		$('#home-promo-image div:visible').fadeOut(333);
		$('#home-promo-image-'+n).fadeIn(333);
		$('#home-promo-list div a').removeClass('selected');
		$('#home-promo-list div a[rel='+n+']').addClass('selected');
		selectedPromo = n;
	};
	
	function startHomePromo(){
		stopHomePromo();
		homePromoTimer = setInterval(function(){
			selectedPromo++;
			if (selectedPromo > 6) { selectedPromo = 1; }
			switchHomePromo( selectedPromo );
		}, 4000);
	};
	function stopHomePromo(){
		clearInterval(homePromoTimer);		
	};
	
	function resizeHomePromo(){
		var new_width = Math.min( 500, Math.round($('#home-promo').width()*0.6));
		$('#home-promo-image').width( new_width );
		$('#home-promo-image div').width( new_width );
	};
	
	$('#home-promo-list a').click( function(){
		switchHomePromo( $(this).attr('rel') );
		stopHomePromo();
		return false;
	});
	
	resizeHomePromo();
	startHomePromo();

	var homeResizeTimer;
	$(window).resize(function() {
	    clearTimeout(homeResizeTimer);
	    homeResizeTimer = setTimeout(function(){resizeHomePromo();}, 25);
	});
	$(window).focus(startHomePromo);
	$(window).blur(stopHomePromo);
});

