$(document).ready(function(){
	
	var total = $(".promo").length;
	var promoWidth = $(".promo").width();
	var	current = $(".promoCurrent").index();
	var	mouseInside = false;
	var animationSpeed = 400;

	
	function scrollNext(){
		if (current < (total - 1)) {
			if($("#promoScroller:animated").length < 1) {
				
				++current;
				$("#promoScroller").animate({left: '-='+promoWidth}, animationSpeed, function() {
					$("#scrollbox ul li a").removeClass("current");
					$("#scrollbox ul li a").eq(current).addClass("current");					
				});
			}
		}		
	}
	
	function scrollPrev(){

		if (current <= 0) {
			current = 0;
		}
		
		else {
			if($("#promoScroller:animated").length < 1) {
				current--;
				$("#promoScroller").animate({left: '+='+promoWidth}, animationSpeed, function(){
					$("#scrollbox ul li a").removeClass("current");
					$("#scrollbox ul li a").eq(current).addClass("current");		
				});
			}
		}		
	}
	
	
	$("#scrollbox ul li").click(function(){	
		
		if(($(this).index() + 1) == total) {
			$("#next").css({"cursor" : "default"});
			$("#next").hide();
		}
		
		if($(this).index() == 0) {
			$("#prev").css({"cursor" : "default"});
			$("#prev").hide();
		}			
		
		
		if($("#promoScroller:animated").length < 1) {
			current = $(this).index();
			goto = -(current * promoWidth);
			
			$("#scrollbox ul li a").removeClass("current");
			$(this).find("a").addClass("current");
					
			$("#promoScroller").animate({left: goto}, animationSpeed);			
		}
	});




	
	$(document).mousemove(function(e){
		
		var windowWidth = $(window).width();
		
		$(window).resize(function() {
			var windowWidth = $(window).width();
		});
		
		
		var windowHalf = Math.round(windowWidth/2);		
		
		if ((e.pageX < windowHalf) && (e.pageY > 165) && (e.pageY < 515) && ($("#products").is(":hidden"))) {
			$("#next").hide();
			showNext = false;
			
			if (current != 0) {
				$("#prev").show();
				showPrev = true;
			}
			else {
				$("#prev").hide();
				showPrev = false;				
			}
		}
		
		else if ((e.pageX > windowHalf) && (e.pageY > 165) && (e.pageY < 515) && ($("#products").is(":hidden"))) {
			
			if (current < (total - 1)) {
				$("#next").show();
				showNext = true;			
			}
			else {
				$("#next").hide();
				showNext = false;
			}

			$("#prev").hide();
			showPrev = false;
		}
		
		else {
			$("#next").hide();
			showNext = false;
			$("#prev").hide();
			showPrev = false;
		}
		
	}); 


	$(".promo").hover(function(){ 
		mouseInside = true;
	}, function(){ 
		mouseInside = false;
	});

	
	$(document).click(function(){
		
		$("#next").css({"cursor" : "pointer"});		
		$("#prev").css({"cursor" : "pointer"});				
				
		if(showNext && !mouseInside) {
			if((current + 2) == total) {
				$("#next").css({"cursor" : "default"});
				$("#next").hide();
			}
			scrollNext();
		}
		else if(showPrev && !mouseInside) {
			if(current == 1) {
				$("#prev").css({"cursor" : "default"});
				$("#prev").hide();
			}
			scrollPrev();
		}
	});
	

});

