(function($){


	$.fn.itiAnimation = function(options)
	{
			var settings = jQuery.extend(
			{
				debug: false,
				current_id : 0,
				frequence:4000,
				fadeTime:1000
			}, options );
		
		var animation = this ;
		settings.current_id = animation.find('a.image:first').attr('rel') ;
	
		if ( settings.debug ) animation.find('#debug').show() ;
	
		start(settings.current_id,0) ;
		
		animation.find('div.puces a.puce').hover(function(){
			start($(this).attr('rel'),500) ;
		}) ;
		
		function start(id_start,time)
		{
			if ( animation.find('a.image').length <= 1 )
			{
				animation.find('div.puces').hide() ;
			}
			else
			{
				if ( id_start != null ) switcher(id_start,true,time) ;
				animation.stopTime('animation') ;
				animation.everyTime(settings.frequence,'animation',function() {
					switcher(getNext()) ;
				}) ;
			}
		}
		
		function getNext()
		{
			next = animation.find('a.image[rel='+settings.current_id+']').next('a.image') ;
			if ( next.length == 1 ) return next.attr('rel') ;
			else return animation.find('a.image:first').attr('rel') ;
		}
		
		function switcher(vers,direct,time)
		{
			if ( direct == null ) direct = false ;
			if ( time == null ) time = settings.fadeTime ;
			
			if ( settings.debug ) $('#debug').prepend('vers='+vers+' current='+settings.current_id+' direct='+direct+' time='+time+"\n") ;
			
			animation.find('a.puce').removeClass('selected') ;
			animation.find('a.texte').hide() ;
			animation.find('a.puce[rel='+vers+']').addClass('selected') ;
			animation.find('a.texte[rel='+vers+']').show() ;
			
			if ( direct )
			{
				if ( settings.current_id != vers )
				{
					animation.find('a.image').stop() ;
					animation.find('a.image').css('z-index',1) ;
					animation.find('a.image').fadeTo(0,100) ;
					animation.find('a.image[rel='+settings.current_id+']').css('z-index',3) ;
					animation.find('a.image[rel='+vers+']').css('z-index',2) ;
					animation.find('a.image[rel='+settings.current_id+']').fadeTo(0,100) ;
					animation.find('a.image[rel='+settings.current_id+']').fadeTo(time,0,function(){
						animation.find('a.image[rel='+vers+']').css('z-index',4) ;
					}) ;
					settings.current_id = vers ;
				}
				else
				{
					animation.find('a.image').css('z-index',1) ;
					animation.find('a.image').fadeTo(0,100) ;
					animation.find('a.image[rel='+settings.current_id+']').css('z-index',3) ;
					animation.find('a.image[rel='+settings.current_id+']').fadeTo(0,100) ;
				}
			}
			else
			{
				animation.find('a.image').css('z-index',1) ;
				animation.find('a.image').fadeTo(0,100) ;
				animation.find('a.image[rel='+settings.current_id+']').css('z-index',3) ;
				animation.find('a.image[rel='+vers+']').css('z-index',2) ;
				animation.find('a.image[rel='+settings.current_id+']').fadeTo(time,0,function(){
					animation.find('a.image[rel='+vers+']').css('z-index',4) ;
				}) ;
				settings.current_id = getNext() ;
			}
		}
	};

})(jQuery);