(function($){
	$.fn.extend({ 
		infiniteCarousel: function(options)
		{
			var defaults = 
			{
				transitionSpeed : 1500,
				displayTime : 6000,
				textholderHeight : .2,
				icons: 0,
				httpHost: ''
			};
		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);

				var numImages = $('ul img', obj).length; // Number of images
				var imgHeight = $('ul img:first', obj).height();
				var imgWidth = $('ul img:first', obj).width();
				var playing = 0;
			
				$('p', obj).hide(); // Hide any text paragraphs in the carousel
				$(obj).width(imgWidth);
			
				// Move last image and stick it on the front
				$(obj).css({'overflow':'hidden','position':'relative'});
				if (numImages > 1)
					$('li:last', obj).prependTo($('ul', obj));
				else
					$('li:last', obj).clone().prependTo($('ul', obj));
				$('ul', obj).css('left',-imgWidth+'px');
				$('ul', obj).width(imgWidth*(numImages+1));
				
				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(){
							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 clearInt;
				if (numImages != 1)
					clearInt = setInterval(function(){shift('next');},o.displayTime+o.transitionSpeed);
				else
					$("#carouselIcons").hide();
  		});
    	}
	});
})(jQuery);


