(function($){
	$.fn.jbRotator = function(paramsu){
		var o = this;
		var imgListe = new Array();
		var params = $.extend({
			xml: '',
			tempo: 10000,
			height: ''
		},paramsu);

		if (params.xml == '') return this;

		// Recuperation du xml des images.
		$.ajax({
			async: false, 
			type: 'GET', 
			dataType: 'xml', 
			url: params.xml, 
			success: function(data){
				$('track', data).each(function(i){
					imgListe[i] = new Array();
					imgListe[i]['title'] = $('title', this).text();
					imgListe[i]['src'] = $('location', this).text();
					imgListe[i]['url'] = $('link', this).text();
					// Prechargement des images.
					if (document.images){
						imgListe[i]['obj'] = new Image();
						imgListe[i]['obj'].src = $('location', this).text();
					}
				});
		}});

		if (imgListe.length < 1) return this;

		// Affiche la premiere image.
		$(o).html('<a href="'+imgListe[0]['url']+'"><img src="'+imgListe[0]['src']+'" alt="'+imgListe[0]['title']+'" title="'+imgListe[0]['title']+'" /></a>');

		if (params.height == '') params.height = $(o).innerHeight();
		$(o).css('height',params.height).css('position', 'relative');

		if (imgListe.length < 2) return this;

		// Controle le chargement des images puis commance le diaporama.
		imgLoadControlle();

		function imgLoadControlle(){
			var ok = true;
			if (document.images){
				for (var i=0; i<imgListe.length; i++){
					if (!(imgListe[i]['obj'].complete)) ok = false;
				}
			}
			if (ok){
				for (var i=1; i<imgListe.length; i++){
					$(o).append('<a href="'+imgListe[i]['url']+'" style="position:absolute;top:0;left:0;z-index:'+i+';display:none;"><img src="'+imgListe[i]['src']+'" alt="'+imgListe[i]['title']+'" title="'+imgListe[i]['title']+'" /></a>');
				}
				setTimeout(function(){rotator(o, 0);}, params.tempo);
			}else
				setTimeout(function(){imgLoadControlle();}, 200);
		}

		function rotator(o, courent){
			var next = courent+1;
			if (imgListe.length <= next){
				next = 0;
				$('a:eq('+next+')', o).css('display', 'block');
				$('a:eq('+courent+')', o).fadeOut('slow');
			}else{
				$('a:eq('+next+')', o).fadeIn('slow',function(){
					$('a:eq('+courent+')', o).css('display', 'none');
				});
			}
			setTimeout(function(){rotator(o, next);}, params.tempo);
		}

		return this;
	}
})(jQuery);
