jQuery(function($){
	
	// jQuery Content Slider
	var currentPosition = 0;
	var slideWidth = 480;
	var slideTimer = 3000;
	var slides = $('.itemslider .product');
	var numberOfSlides = slides.length;
	
	// Remove scrollbar in JS
	//$('#slidesContainer').css('overflow', 'hidden');
	
	// Wrap all .slides with #slideInner div
	slides
	.wrapAll('<div id="slideInner"></div>')
	// Float left to display horizontally, readjust .slides width
	.css({
	'float' : 'left',
	'width' : slideWidth
	});
	
	// Set #slideInner width equal to total width of all slides
	$('#slideInner').css('width', slideWidth * numberOfSlides);
	
	// Insert controls in the DOM
	$('#slides')
	.prepend('<span class="control" id="leftControl">Clicking moves left</span>')
	.append('<span class="control" id="rightControl">Clicking moves right</span>');
	
	//animate controls fader
	$(".control").fadeTo("slow", 0.1);
	$(".control").hover(function(){
	  $(this).stop().fadeTo("slow", 1.0);
	},function(){
	  $(this).stop().fadeTo("slow", 0.1);
	});
	
	// Create event listeners for .controls clicks
	$('.control')
	.bind('click', function(){
	  animateSlider(true, this);
	});
	
	//Animate slide after timeout
	interval = window.setInterval(function(){
	  animateSlider(false, null);
	}, slideTimer);
	
	$('#slides').hover(function () {
		  clearInterval(interval);
		}, function () {
		  interval = setInterval(function(){
		animateSlider(false, null);
	  }, slideTimer);
		});
	  
	function animateSlider(clicked, object){
	  if(clicked){
	    // Determine new position
	    currentPosition = ($(object).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
	    
	    //reset position if position > amount of divs
	    if(currentPosition >= (numberOfSlides)){
	      currentPosition = 0;
	    }
	    else if(currentPosition < 0){
	      currentPosition = (numberOfSlides - 1);
	    }
	  }
	  else{
	    //reset position if position > amount of divs
	    if(currentPosition == (numberOfSlides -1)){
	      currentPosition = 0;
	    }
	    //increment the position after timeout
	    else{
	      currentPosition++;
	    }
	  }
	  // Move slideInner using margin-left
	  $('#slideInner').animate({
	  'marginLeft' : slideWidth*(-currentPosition)
	  });
	}
});
