var sliderHome = undefined;

/* product slider */
var productWindowSize = 3; // number of products visible at one time
var animationDuration = 400; // ms
var totalProducts = 5; //$(".featured-product").length;
var productWidth = 213; //$('.featured-product').css("width");
// productWidth = productWidth.substring(0, productWidth.lastIndexOf("px"));
maxWidth = 560;
minWidth = 74;
pos = 0;

$(document).ready(function(){
	$('.promo').each(function(){
		$(this).hover(
			function () {$(this).children('.promo .wrapper2').stop().animate({"top":"176px"}, 200);}, 
			function () {$(this).children('.promo .wrapper2').stop().animate({"top":"206px"}, 200);}
		);
	});

	$('.featured-products').css("width",(productWindowSize*productWidth)+"px");
	$('.products-wrapper').css("width",(totalProducts*productWidth)+"px");
	$('.featured-left-arrow').click(function () {
		var position = $(".products-wrapper").position();
		if (position.left < 0) {
			$('.products-wrapper').stop().animate({"left":"+="+productWidth+"px"}, animationDuration);
		} else {
			$('.products-wrapper').stop().animate({"left":"-" + ((totalProducts-productWindowSize)*productWidth) + "px"}, animationDuration);
		}
	});
	$('.featured-right-arrow').click(moveSlider);

	if (location.pathname == "/") {
		resetTimer();
	}
});

function moveSlider() {	
	var position = $(".products-wrapper").position();
	if (position.left > -((totalProducts-productWindowSize)*productWidth)) {
		$('.products-wrapper').stop().animate({"left":"-="+productWidth+"px"}, animationDuration);
	} else {
		$('.products-wrapper').stop().animate({"left":"0px"}, animationDuration);
	}
	oldpos = pos;
	pos++;
	if (pos > 3) {pos = 0};
	$("#promo"+oldpos+" .wrapper2").stop().animate({"top":"206px"}, {queue:false, duration:400});
	$("#promo"+pos+" .wrapper2").stop().animate({"top":"176px"}, {queue:false, duration:400});
}
function resetTimer() {
	clearInterval(sliderHome);
	sliderHome = setInterval(moveSlider, 6000);
}