/**
 * Copyright 2010 by Alexander Fischer.
 * Alle Rechte vorbehalten.
 */
(function($) {
	$.fn.carousel = function () {
		return this.each(function () {
			// Configurable variables
			var scrollWidth = 1000;
			var scrollSpeed = 0.08;
			
			// Initialize some variables
			var $wrapper = $('> div',this),
				$slider = $wrapper.find('> ul'),
				$items = $slider.find('> li');
			
			// compute total width
			var totalWidth = 0;
			$items.each(function() {
				totalWidth += $(this).outerWidth(true);
			});
			
			// Append first items to the end
			var padding = 0;
			while (padding < 1000) {
				$items.filter(":last").after($items.clone().addClass("cloned"));
				$items = $slider.find("> li");
				padding += totalWidth;
			}
			

//			function scroll(item) {
//				var distance = item.outerWidth(true);
//				var duration = distance / scrollSpeed;
//				
//				$wrapper.animate({
//					scrollLeft: "+=" + distance
//				}, duration, "linear", function() {
//					if ($wrapper.scrollLeft() >= totalWidth) {
//						$wrapper.scrollLeft($wrapper.scrollLeft()-totalWidth);
//						nextItem = $items.first();
//					} else {
//						nextItem = item.next();
//					}
//					scroll(nextItem);
//				});
//			}
//			scroll($items.first());
			
			function scroll() {
				var duration = scrollWidth / scrollSpeed;
				$wrapper.animate({
					scrollLeft: "+=" + scrollWidth
				}, duration, "linear", function() {
					if ($wrapper.scrollLeft() >= totalWidth) {
						$wrapper.scrollLeft($wrapper.scrollLeft()-totalWidth);
					}
					scroll();
				});
			}
			scroll();
		})
	}
})(jQuery);
