(function () {
	$.fn.infiniteCarousel = function (options) {
		var defaults = {
			speed: 1000,
			pause: 5000
		};

		var options = $.extend(defaults, options);

		this.each(function() {
			var obj = $(this);
			var s = $('li:first', obj).outerWidth();
			var c = $('li', obj).length;
			var o = $('li', obj);
			var act = 0;
			obj.css("overflow","hidden");
			obj.css("height",$('li:first', obj).outerHeight());
			obj.css("width",$('li:first', obj).outerWidth());
			$("ul", obj).css("overflow","hidden");
			$("ul", obj).css("height", $('li:first', obj).outerHeight());
			$("ul", obj).css("width", s * (c + 1));
			$("li", obj).css('float','left');
			var copied = false;
			function next() {
				act++;
				if (act < c) {
					var p = -s * act;
					$("ul", obj).animate({ marginLeft:p }, options.speed);
					if (!copied) {
						$("ul", obj).append($("li:nth-child("+act+")", obj).clone());
					}
				} else if (act == c) {
					var p = -s * act;
					$("ul", obj).animate({ marginLeft:p },
						options.speed, function() {
							// after animating to the first one...
							$("ul", obj).animate({ marginLeft:0 }, 0);
							act = 0;
							copied = true;
						});
				}
				setTimeout(function(){ next(); }, options.pause);
			}
			// Start the carousel...
			setTimeout(function(){ next(); }, options.pause);
		});
	};
})(jQuery);


