// JavaScript Document	
jQuery(document).ready(function(){

var buttons = '<div class="slider-buttons"><a href="#"><img height="20" width="31" alt="Image Précédente" src="/images/prev.png"></a><a href="#"><img height="20" width="31" alt="Image Suivante" src="/images/next.png"></a></div>';

jQuery('.slider .slides-container').append(buttons);

var totWidth=0;
var positions = new Array();
var pos = 0;
var count = 0;
jQuery(window).load(function() {

jQuery('.slider .slides-container li img').each(function(i){

		/* Traverse through all the slides and store their accumulative widths in totWidth */
		
		positions[i]= totWidth;
		totWidth += jQuery(this).width();
		count++;
		
	});
	
	});
	
	jQuery(".slider-buttons a").eq(0).click(function(e,keepScroll){
	if(pos < count-1)
			pos++;
		else
			pos=0;
		
		// alert(jQuery('#slider #sub-slider').find('a:eq('+pos+')').attr('href'));
		
		jQuery('.box.slider ul').stop().animate({marginLeft:-positions[pos]+'px'},450);
		/* Start the sliding animation */
			
		e.preventDefault();
		/* Prevent the default action of the link */
		
		
		if(!keepScroll) clearInterval(itvl);
		
	});
	
	jQuery(".slider-buttons a").eq(1).click(function(e,keepScroll){
	if(pos != 0)
			pos--;
		else
			pos=count-1;
	
		
		jQuery('.box.slider ul').stop().animate({marginLeft:-positions[pos]+'px'},450);
		/* Start the sliding animation */
			
		e.preventDefault();
		/* Prevent the default action of the link */
		
		
		if(!keepScroll) clearInterval(itvl);
		
	});
	
	function autoAdvance()
	{	
		jQuery(".slider-buttons a").eq(0).trigger('click',[true]);
	}

	// The number of seconds that the slider will auto-advance in:
	
	var changeEvery = 5;

	var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);
	
	itvl;
	
});

jQuery(document.documentElement).keyup(function (event) {

    // handle cursor keys
    if (event.keyCode == 37) {
      // go left
      jQuery(".slider-buttons a").eq(1).trigger('click',[true]);
    } else if (event.keyCode == 39) {
      // go right
     jQuery(".slider-buttons a").eq(0).trigger('click',[true]);
    }

  });
