(function($, undefined){
	$.fn.ifrAnimHome = function(){
		
			//nombre de ms entre les transitions
		var delay = 5000;
		
			//durée des transitions en ms
		var speed = 200;
		
		var styles = {
			titre: {
				from: {
					left: 230,
					top: 37,
					opacity: 0
				},
				to: {
					left: 246,
					top: 37,
					opacity: 1
				}
			},
			texte: {
				from: {
					left: 246,
					top: 54,
					opacity: 0
				},
				to: {
					left: 246,
					top: 74,
					opacity: 1
				}
			}	
		};
		
			//la liste
		var $this = this.first();
		
			//les items de liste
		var items = [];
		
			//indice de l'item courant
		var current = -1;
		
		var auto = true;
		
		
		var $controls = $('<div class="animhome-controls"><a href="#" class="prev"></a><a href="#" class="pause"></a><a href="#" class="next"></a></div>');
		$pauseBtn = $controls.find('.pause'); 
			
		$controls.delegate('a', 'click', clickControls);
		
		
		//on détache les éléments
		$this.find('li').each(function(){
			items.push( $(this).detach() );
		});
		
		$this.css('overflow', 'hidden')
			.after($controls);
		
		
		
		if( items.length > 0 )
		{
			showItem(0);
		}
		
		/**
		 * Affiche l'item d'indice i
		 */
		function showItem( i )
		{
			var $current = $this.find('li');
			var $next = items[i];
			
			if( $current.length === 0 )
			{
				affiche( $next );
			}
			else
			{
				masque( $current, function(){
					affiche( $next );
				});
			}
			
			current = i;
		}
		
		/**
		 * Lance l'animation d'apparition de l'item
		 */
		function affiche( $item ){
			var $halo = $item.find('.halo').hide();
			var $rect = $item.find('.rect').hide();
			var $titre = $item.find('.title');
			var $texte = $item.find('.texte');
			
			$titre.css( styles.titre.from );
			$texte.css( styles.texte.from );
			
			$item.appendTo( $this );
			
			$halo.fadeIn( speed );
			$rect.delay( speed / 2 ).slideDown( speed );
			$titre.delay( speed ).animate( styles.titre.to, speed );
			
			var animCallback = auto ?  function(){
				setTimeout(function(){
					if( auto )
					{
						controls.next();
					}
				}, delay)
			} : null;
			
			$texte.delay( speed * 3 / 2 ).animate( styles.texte.to, speed, animCallback );
			
		}
		
		/**
		 * Lance l'animation de disparition de l'item
		 */
		function masque( $item, callback )
		{
			$item.fadeOut(speed, function(){
				$item.detach().show();
				callback.call();
			});
		}
		
		
		/**
		 * Actions des contrôles
		 */
		var controls = {
				prev: function(){
					showItem( (current-1 + items.length) % items.length );
				},
				
				next: function(){
					showItem( (current+1) % items.length );
				},
				
				play: function(){
					auto = true;
					$pauseBtn.removeClass('play').addClass('pause');
					this.next();
				},
				
				pause: function(){
					auto = false;
					$pauseBtn.removeClass('pause').addClass('play');
				}
		};
		
		/**
		 * Contrôles de l'animation
		 */
		function clickControls( e )
		{
			var $target = $(e.target);
			var action = $target.attr('class'); 
			if( controls[action] ){
				controls.pause();
				controls[action]();
			}
			
			e.preventDefault();
			return false;
		}
	}
	
	$(function(){
		$('.block-4items_slideshow').ifrAnimHome();
	});
})(jQuery);
