(function($){

	$.fn.itiAnimation = function(options)
	{
		var settings = jQuery.extend(
		{
			debug: false,
			frequence:4000,
			fadeTime:1000,
			fadeDirect:600
		}, options );
		
		var current_id = 0 ;
		var current_pos = 0 ;
		var animation = this ;
		var slide_lance = false ;
		current_id = animation.find('div#images img:first').attr('rel') ;
		
		if ( settings.debug ) animation.find('#debug').show() ;
		
		if ( animation.find('div#images img').length > 1 )
		{
			start(current_id,0) ;
			
			animation.find('div#images img').show() ;
			animation.find('ul#puces a').click(function(){
				switcher($(this).attr('rel'),true,settings.fadeDirect) ;
				animation.stopTime('animation') ;
				if ( settings.debug ) animation.find('#debug').prepend('debut delai'+"\n") ;
				$('div#images').animate(
					{'width':'100%'},
					settings.fadeDirect,
					function(){
						if ( settings.debug ) animation.find('#debug').prepend('fin delai'+"\n") ;
						if ( slide_lance )
						{
							start(current_id,0) ;
						}
					}
				) ;
				return false ;
			}) ;
			
			animation.find('a#playpause').click(function(){
				if ( slide_lance )
				{
					animation.stopTime('animation') ;
					slide_lance = false ;
					$(this).addClass('pause') ;
				}
				else
				{
					start(current_id,0) ;
					slide_lance = true ;
					$(this).removeClass('pause') ;
				}
				return false ;
			}) ;
		}
		else
		{
			animation.find('a#playpause').hide() ;
			animation.find('ul#puces').hide() ;
		}
		
		function start(id_start,time)
		{
			if ( animation.find('div#images img').length > 1 )
			{
				if ( id_start != null ) switcher(id_start,true,time) ;
				animation.stopTime('animation') ;
				slide_lance = true ;
				animation.everyTime(settings.frequence,'animation',function() {
					switcher(getNext()) ;
				}) ;
			}
		}
		
		function getNext()
		{
			next = animation.find('div#images img[rel='+current_id+']').next('div#images img') ;
			if ( next.length == 1 ) return next.attr('rel') ;
			else return animation.find('div#images img: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='+current_id+' direct='+direct+' time='+time+"\n") ;
			
			animation.find('ul#puces a').removeClass('selected') ;
			animation.find('ul#puces a[rel='+vers+']').addClass('selected') ;
			
			animation.find('div#titres h3').stop().hide() ;
			animation.find('div#titres h3[rel='+vers+']').stop().show() ;
			
			animation.find('a.details').attr('href',animation.find('ul#puces a[rel='+vers+']').attr('href')) ;
			
			if ( direct )
			{
				if ( current_id != vers )
				{
					animation.find('div#images img').stop() ;
					animation.find('div#images img').css('z-index',1) ;
					animation.find('div#images img').fadeTo(0,100) ;
					animation.find('div#images img[rel='+current_id+']').css('z-index',3) ;
					animation.find('div#images img[rel='+vers+']').css('z-index',2) ;
					animation.find('div#images img[rel='+current_id+']').fadeTo(0,100) ;
					animation.find('div#images img[rel='+current_id+']').fadeTo(time,0) ;
					current_id = vers ;
				}
				else
				{
					animation.find('div#images img').css('z-index',1) ;
					animation.find('div#images img').fadeTo(0,100) ;
					animation.find('div#images img[rel='+current_id+']').css('z-index',3) ;
					animation.find('div#images img[rel='+current_id+']').fadeTo(0,100) ;
				}
			}
			else
			{
				animation.find('div#images img').css('z-index',1) ;
				animation.find('div#images img').fadeTo(0,100) ;
				animation.find('div#images img[rel='+current_id+']').css('z-index',3) ;
				animation.find('div#images img[rel='+vers+']').css('z-index',2) ;
				animation.find('div#images img[rel='+current_id+']').fadeTo(time,0) ;
				current_id = getNext() ;
			}
		}
	};

})(jQuery);
