(function($) {
	$(function() {
		$('#rotation').each(function() {
			var interval = $(this).attr('class').match(/duree-([0-9]+)/);
			if ( $(this).find('.rotation-fixe').length ) { return false; }
			var $items = $(this).find('li')
			var $liste = $(this).find('ul')
			var compteur = 0;
			var self = $(this).bind('next', function(e) {
				compteur++;
				var pos = compteur % $items.length;
				if ( compteur >= $items.length ) {
					$liste.find('.item'+pos).clone().appendTo( $liste );
					var next = $liste.find('.item'+pos).eq(1);
				} else {
					var next = $liste.find('li').eq(compteur);
				}
				$(this).find('.rotation-inner').animate({
					height : next.height() + 'px'
				},{
					duration : 600
				});
				$(this).find('ul').animate({
					left : '-=980px'
				},{
					duration: 600,
					complete : function() {
						if ( compteur >= $items.length ) {
							$liste.find('li').eq(0).remove();
							$(this).animate({
								left : '+=980px'
							},{
								duration: 0
							});
						}
					}
				});
			});

			$(this).find('a').click(function() {
				$(this).parents('#rotation').trigger('next');
				return false;
			});

			$(this).find('.rotation-inner').css({
				height: $items.eq(0).height()+'px'
			});

			setInterval(function() {
				self.trigger('next');
			}, parseInt(interval[1]) * 1000)
		});
	});
})(jQuery);
