(function($){

	$.fn.itiAnimation = function()
	{
		var debug = false ;
		var current_id = 0 ;
		var current_pos = 0 ;
		var fadeTime = 1000 ;
		var animation = this ;
		current_id = animation.find('a.image:first').attr('rel') ;
	
		if ( debug ) animation.find('#debug').show() ;
	
		start(current_id,0) ;
		
		animation.find('div.liens a.puce').hover(function(){
			start($(this).attr('rel'),500) ;
		}) ;
		
		function start(id_start,time)
		{
			if ( id_start != null ) switcher(id_start,true,time) ;
			animation.stopTime('animation') ;
			animation.everyTime(8000,'animation',function() {
				switcher(getNext()) ;
			}) ;
		}
		
		function getNext()
		{
			next = animation.find('a.image[rel='+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 = fadeTime ;
			
			if ( debug ) $('#debug').prepend('vers='+vers+' current='+current_id+' direct='+direct+' time='+time+"\n") ;
			
			animation.find('a.puce').removeClass('puce_selected') ;
			animation.find('a.texte').hide() ;
			animation.find('a.puce[rel='+vers+']').addClass('puce_selected') ;
			animation.find('a.texte[rel='+vers+']').show() ;
			
			if ( direct )
			{
				if ( 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='+current_id+']').css('z-index',3) ;
					animation.find('a.image[rel='+vers+']').css('z-index',2) ;
					animation.find('a.image[rel='+current_id+']').fadeTo(0,100) ;
					animation.find('a.image[rel='+current_id+']').fadeTo(time,0) ;
					current_id = vers ;
				}
				else
				{
					animation.find('a.image').css('z-index',1) ;
					animation.find('a.image').fadeTo(0,100) ;
					animation.find('a.image[rel='+current_id+']').css('z-index',3) ;
					animation.find('a.image[rel='+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='+current_id+']').css('z-index',3) ;
				animation.find('a.image[rel='+vers+']').css('z-index',2) ;
				animation.find('a.image[rel='+current_id+']').fadeTo(time,0) ;
				current_id = getNext() ;
			}
		}
	};

})(jQuery);
