$(document).ready(function () {
     
  carousel();
  
});

function carousel() {   
    // CONSTANT - how many products can be displayed in one screen?
	var display = 4; 
	
	// Get carousel & count products
	var carousel = $('.featured-products ul');
	var productCount = $('.featured-products ul li').length; // How many products are there?
	
    // Add next and previous buttons (if required)
	if (productCount > display) {
		var buttons = '<a class="featured-next">Next product</a><a class="featured-previous">Previous product</a>';
		$('#featured-buttons').replaceWith(buttons);
	} else {
		return false;	
	}
	
	// Get buttons to add click events
    var next = $('.featured-next');
	var previous = $('.featured-previous');
	
	// CONSTANT - How many pixels to shift the products left?
	var leftValue = 160; 
	
	var leftCount = 0; // How many times have we clicked the left button?
	var leftMax = productCount - display; // How many times CAN we click the left button?
    
	// "Next" click event
    $(next).click( function() {
		if (leftCount < leftMax) {
			leftCount++;
			
			$(carousel).animate({"left": "-="+leftValue+"px"}, "slow" );
		}
    });
    
	// "Previous" click event
	$(previous).click( function() {
		if (leftCount > 0) {
			leftCount--;
			
			$(carousel).animate({"left": "+="+leftValue+"px"}, "slow" );

		}		
    });
  
}


