$.fn.caroussel = function(){
	
	return this.each(function(){
		// Variables
		var $slide = $(this)
			$ul = $slide.find('ul'),
			$li = $ul.find('li'),
			liWidth = parseInt($li.css('width'), 10) + parseInt($li.css('marginRight'), 10),
			ulMargin = parseInt($ul.css('marginLeft'), 10) - liWidth,
			liLength = $ul.find('li').length,
			ulWidth = liWidth * liLength,
			next = $('.next', $(this)),
			prev = $('.prev', $(this)),
			click = false;

			$ul.css('width',ulWidth+"px");

			var slideauto = window.setTimeout(function(){ $(document.getElementById('body')).slideauto(ulMargin,click) }, 5000);
		
		// Clic next (en evitant qu'il y ait plusieurs clics d'un seul coup)
		$('.control:not(.animated)', $slide).live('click', function(){
			var click = true;
			
			if( $(this).hasClass('next') )  ulMargin = parseInt($ul.css('marginLeft'), 10) - liWidth;
			else  ulMargin =  parseInt($ul.css('marginLeft'), 10) + liWidth;
			
			window.setTimeout(function(){ $(document.getElementById('body')).slideauto(ulMargin,click) }, 1);
			clearTimeout(slideauto);
		});
		
	});
};

$.fn.slideauto = function(ulMargin,click){
	ulWidth = liWidth * liLength;
	totalWidth = ulWidth - (ulWidth * 2);
	
	$('.control').addClass("animated");
		
	if ( ulMargin == totalWidth ){
		$ul.animate({ "margin-left": 0 }, 500, function() {
			$('.control').removeClass("animated");
			ulMargin = parseInt($ul.css('marginLeft'), 10) - liWidth;
			if( !click ){
				slideauto = window.setTimeout(function(){ $(document.getElementById('body')).slideauto(ulMargin,click) }, 5000);
			}
		});
	}
	else if ( ulMargin == liWidth ){
		$ul.animate({ "margin-left": totalWidth+liWidth }, 500, function() {
			$('.control').removeClass("animated");
			ulMargin = parseInt($ul.css('marginLeft'), 10) - liWidth;
			if( !click ){
				slideauto = window.setTimeout(function(){ $(document.getElementById('body')).slideauto(ulMargin,click) }, 5000);
			}
		});
	}
	else {
		$ul.animate({ "margin-left": ulMargin }, 500, function() {
			$('.control').removeClass("animated");
			ulMargin = parseInt($ul.css('marginLeft'), 10) - liWidth;
			if( !click ){
				slideauto = window.setTimeout(function(){ $(document.getElementById('body')).slideauto(ulMargin,click) }, 5000);
			}
		});
	}
}

$(function(){
	$(document.getElementById('news')).caroussel();
});


/*----- GESTION DU DEROULE DU SOUS-MENU -----*/
$(".menu li").mouseover( function() { 
	$(this).find("ul.sous-menu").stop().animate({height: 40}, "slow"); 
});

$(".menu li").mouseleave( function() { 
	$(this).find("ul.sous-menu").stop().animate({height: 0}, "slow"); 
});
/* END ----- GESTION DU DEROULE DU SOUS-MENU -----*/



