$(document).ready(function(){
   
	var varWaitTime = 7000;  	//How long it waits before moving to the next item. 1000 = 1 sec
	var varSpeed = 400;			//The speed of the animated movement.
	var varResetSpeed = 600;	//The speed of the animated movement as it slides back to the beginning.
	
	var varUserControl = "NO";
	
	var varItemWidth = 179;
	var varContainerPadding = 20;
	var varContainerWidth = 517;
	
	var varMaxPosition = (0 - (((varItemWidth * varItems) - varContainerPadding) - varContainerWidth)) + "px";
	var varPosition = 0;
	
	animateItems();
	
	function checkControl(){
	
		if (varUserControl == "NO"){
			
			moveItems();
		}
	
	}
	
	function animateItems(){
		
		setTimeout(function(){checkControl();},varWaitTime);
		
	}
	
	function moveItems(){
		var position = $("#home_featured_items").css("left");
		
		
		if (varPosition == varItems - 3){
			
			$("#home_featured_items")
				.animate({ left: "0px"}, varResetSpeed, "swing", function(){varPosition = 0; animateItems();} );
		
		}else{
		
			var pxPosition = (0 - (varItemWidth * (varPosition + 1))) + "px";
			
			$("#home_featured_items").animate({ left: pxPosition}, varSpeed, "swing", function(){++varPosition; animateItems();} );
			
		}
	}
	
	function moveRight() {
		var position = $("#home_featured_items").css("left");
		
	   	var pxPosition = (0 - (varItemWidth * (varPosition - 1))) + "px";
		if(position != "0px"){
		   
		   $("#home_featured_items").animate({left: pxPosition}, varSpeed, "swing", function(){ --varPosition;} );
		   
		}
	}
	
	function moveLeft() {
		var position = $("#home_featured_items").css("left");
		
		var pxPosition = (0 - (varItemWidth * (varPosition + 1))) + "px";
		if(position != varMaxPosition){
		   
		   $("#home_featured_items").animate({left: pxPosition}, varSpeed, "swing", function(){ ++varPosition;} );
		   
		}
	}
		
	
	$("#move_left").click(function(event){
	   	event.preventDefault();
		varUserControl = "Yes";
		$("#home_featured_items").stop();
		moveRight();
		
	   	
	 });
	 
	 $("#move_right").click(function(event){
	   	event.preventDefault();
	   	varUserControl = "Yes";
		$("#home_featured_items").stop();
		moveLeft();
	 });
 
 });