(function($){
	$.fn.extend({ 
		infiniteCarousel: function(options)
		{
			var defaults = 
			{
				transitionSpeed : 1500,
				displayTime : 6000,
				textholderHeight : .2,
				icons: 0,
				httpHost: '',
				altText: 0
			};
		var options = $.extend(defaults, options);
		options.current = 0;
	
    		return this.each(function() {
    			var guid = (((1+Math.random())*0x10000)|0).toString(16);
				var o = options;
				var obj = $(this);

				// get the images' parameters
				var numImages = $('img.carouselImg', obj).length; // Number of images
				var imgHeight = $('img.carouselImg:first', obj).height();
				var imgWidth = $('img.carouselImg:first', obj).width();
				var playing = 0;
			
				$(obj).width(imgWidth);
				
				// place the first text at the right spot and hide the others
				$('.carouselText').hide().css("z-index", 20000).css('height', 0).css('line-height', 0);
				$("<div id='carouselTextBottom'><div id='carouselTextTop'><div id='carouselTextMiddle'></div></div></div>").appendTo($("#carousel"));
			
				// Move last image and stick it on the front
				$(obj).css({'overflow':'hidden','position':'relative'});
				$('li:last', obj).prependTo($('ul', obj));
				$('ul', obj).css('left',-imgWidth+'px');
				$('ul', obj).width(imgWidth*(numImages+1));
				
				$("#carouselTextTop").css('background', 'transparent url(' + o.httpHost + 'images/background_carrousel_text_top.png) no-repeat top left')
					.css('padding-top', '20px');
				$("#carouselTextBottom").css('padding-bottom', '20px')
					.css('background', 'transparent url(' + o.httpHost + 'images/background_carrousel_text_bottom.png) no-repeat bottom left')
					.css('position', 'absolute')
					.css('left', 5)
					.css('top', 60)
					.css('width', '244px');
				$("#carouselTextMiddle").html($('#carouselText0').html())
					.css('font-weight', 'normal')
					.css('font-style', 'italic')
					.css('font-size', '14px')
					.css('padding', '0 20px')
					.css('color', '#8B864E')
					.css('background', 'transparent url(' + o.httpHost + 'images/background_carrousel_text_middle.png) repeat-y center left');
				
				if (!$('#carouselText0') || $('#carouselText0').html() == '')
					$("#carouselTextBottom").hide();
				
				if (o.icons)
				{
					var icon = $('#carouselIcons img:eq(0)');
					icon.attr('src', icon.attr('src').replace('_off', '_on'));
					var icons = $('#carouselIcons img');
					icons.each(function(i) {
						$(icons[i]).click(function(){
							// uncomment the 3 next lines if clicking on an icon pauses the carousel
							//playing = 0;	
							//setTimeout(function(){$('#play_btn'+guid).fadeIn(250);},o.transitionSpeed);
							//clearTimeout(clearInt);
							shift(i);
						});
					});
				}
				function shift(direction)
				{
					if (playing == 1)
						return;
					if (direction == "next")
					{
						playing = 1;
						// Copy leftmost (first) li and insert it after the last li
						$('li:first', obj).clone().insertAfter($('li:last', obj));
						// Update width and left position of ul and animate ul to the left
						$('ul', obj)
							.width(imgWidth*(numImages+1))
							.animate({left:-imgWidth*2},o.transitionSpeed,function(){
								$('li:first', obj).remove();
								$('ul', obj).css('left',-imgWidth+'px').width(imgWidth*numImages);
								playing = 0;
							});
						if (o.icons)
						{
							var icon = $('#carouselIcons img:eq(' + o.current + ')');
							icon.attr('src', icon.attr('src').replace('_on', '_off'));
							var icon = $('#carouselIcons img:eq(' + ((o.current+1)%numImages) + ')');
							icon.attr('src', icon.attr('src').replace('_off', '_on'));
						}
						o.current = (o.current+1)%numImages;
					}
					else if (direction == "prev")
					{
						playing = 1;
						// Copy rightmost (last) li and insert it after the first li
						$('li:last', obj).clone().insertBefore($('li:first', obj));
						// Update width and left position of ul and animate ul to the right
						$('ul', obj)
							.width(imgWidth*(numImages+1))
							.css('left',-imgWidth*2+'px')
							.animate({left:-imgWidth},o.transitionSpeed,function(){
								$('li:last', obj).remove();
								$('ul', obj).width(imgWidth*numImages);
								playing = 0;
							});
						if (o.icons)
						{
							var icon = $('#carouselIcons img:eq(' + o.current + ')');
							icon.attr('src', icon.attr('src').replace('_on', '_off'));
							var icon = $('#carouselIcons img:eq(' + (o.current - 1 < 0 ? numImages - 1 : o.current - 1) + ')');
							icon.attr('src', icon.attr('src').replace('_off', '_on'));
						}
						o.current = (o.current - 1 < 0 ? numImages - 1 : o.current - 1);
					}
					else if (o.current < direction)
					{
						playing = 1;
						var diff = direction - o.current;
						for (var i = 0; i < diff; i++)
							$('li:eq(' + i + ')', obj).clone().insertAfter($('li:last', obj));
						// Update width and left position of ul and animate ul to the left
						$('ul', obj)
							.width(imgWidth*(numImages + diff))
							.animate({left:-imgWidth*(diff + 1)},o.transitionSpeed,function(){
								for (var i = 0; i < diff; i++)
									$('li:first', obj).remove();
								$('ul', obj).css('left',-imgWidth+'px').width(imgWidth*numImages);
								playing = 0;
							});
						if (o.icons)
						{
							// disable former icon
							var icon = $('#carouselIcons img:eq(' + o.current + ')');
							icon.attr('src', icon.attr('src').replace('_on', '_off'));
							// and activate current icon
							var icon = $('#carouselIcons img:eq(' + direction + ')');
							icon.attr('src', icon.attr('src').replace('_off', '_on'));
						}
						o.current = direction;
					}
					else if (o.current > direction)
					{
						playing = 1;
						var diff = o.current - direction;
						for (var i = 0; i < diff; i++)
							$('li:eq(' + (numImages - 1) + ')', obj).clone().insertBefore($('li:first', obj));
						// Update width and left position of ul and animate ul to the left
						$('ul', obj)
							.width(imgWidth*(numImages + diff))
							.css('left',-imgWidth*(diff + 1)+'px');
							$('ul', obj).animate({left:-imgWidth},o.transitionSpeed,function(){
								for (var i = 0; i < diff; i++)
									$('li:last', obj).remove();
								$('ul', obj).css('left',-imgWidth+'px').width(imgWidth*numImages);
								playing = 0;
							});
						if (o.icons)
						{
							// disable former icon
							var icon = $('#carouselIcons img:eq(' + o.current + ')');
							icon.attr('src', icon.attr('src').replace('_on', '_off'));
							// and activate current icon
							var icon = $('#carouselIcons img:eq(' + direction + ')');
							icon.attr('src', icon.attr('src').replace('_off', '_on'));
						}
						o.current = direction;
					}
					var opa = $("#carouselTextBottom").css('opacity');
					if (opa != 0)
					{
						$("#carouselTextBottom").fadeOut(1000, function()
							{
								$("#carouselTextMiddle").html($('#carouselText' + o.current).html());
								if ($('#carouselText' + o.current) && $('#carouselText' + o.current).html() != '')
								{
									$("#carouselTextBottom").fadeIn(1000);
								}
							}
						);
					}
					else
					{
						$("#carouselTextMiddle").html($('#carouselText' + o.current).html());
						if ($('#carouselText' + o.current) && $('#carouselText' + o.current).html() != '')
						{
							$("#carouselTextBottom").fadeIn(2000);
						}
					}
				}
				var clearInt = setInterval(function(){shift('next');},o.displayTime+o.transitionSpeed);
  		});
    	}
	});
})(jQuery);


