// JavaScript Document

function initCarrousel(carrousel,leftBtn,rightBtn) {
	function animateFn()
	{
		animate(1);
	}
	function animate(posneg)
	{
		if(posneg==null)posneg=1;
		var ul = carrousel.children(":first-child");
		var increment = parseFloat(ul.find("li").css("width"));
		var count = ul.children().length;
		var ulWidth = count*increment;
		ul.css("width", ulWidth+ "px");
		var widthCarrousel = parseFloat(carrousel.css("width"));
	
		ul.stop();
		var left = parseFloat(ul.css("left"));
		if(isNaN(left)) left=0;
		if (posneg>0) {
			var pos = (Math.floor(left/increment)-1)*increment;
			var max = -(ulWidth - widthCarrousel);
			if(pos<max) { pos=max; clearInterval(automatisation);} ; ul.animate({ left: pos }, 1000 );
		} else {
			var pos = (Math.ceil(left/increment)+1)*increment;			   
			if(pos>0) { pos=0; clearInterval(automatisation);} ul.animate({ left: pos }, 1000 );
		}
	}
	
	var automatisation = setInterval(animateFn,5000);
	
	rightBtn.click(function(){
		clearInterval(automatisation);
		animate(+1);

  	});
	leftBtn.click(function(){
		clearInterval(automatisation);
		animate(-1);
  	});
	
   $(".liwsm").hover(
     function(){
       $(this).find(".ul2").stop().fadeTo('fast', 1).show();
     },
     function(){
       $(this).find(".ul2").stop().hide();
     }
   );
}

$(document).ready(function() {
	initCarrousel($("#carrousel"),$("a.left"),$("a.right"));
});
