(function($) {

	var cycles = {};

	$.cycle = function(label, options){

		// Check if we should instantiate a new cycle
		if(typeof(options) == "object" || !options){

			// Override default options
			this.options = $.extend({
				label:			label,
				selector:		this.selector,
				delay:			5000,
				speed:			500,
				offset:			2,
				auto_start:		true
			}, options);

			this.scrolling = false;

			// Select the items
			this.items = $(this.options.selector);

			// Set the display width
			this.display_width = this.items.parent().width();

			// Position the items
			this.items.each(function(){
				var prev = $(this).prev();
				if(prev.length){
					var left = prev.position().left + prev.outerWidth() + options.offset;
				}else{
					var left = 0;
				}
				$(this).css('left', left +'px');
			});

			// Add this to the cycle list
			cycles[label] = this;

			// Start the cycle
			if(this.options.auto_start){
				$.cycle(label, "start");
			}

		}else{

			var cycle = cycles[label];

			switch(options){
				case "start":

					// Start the timer
					$(document).oneTime(cycle.options.delay, label, function(){

						// Show the next item
						$.cycle(label, "next");

					});

					break;
				case "stop":

					// Stop the timer for the given label
					$(document).stopTime(label);

					break;
				case "previous":

					if(!cycle.scrolling){

						cycle.scrolling = true;

						// Stop the timer for the given label
						$(document).stopTime(label);

						// Retrieve the item list
						var items = $(cycle.options.selector);

						// Get the last items
						var last = items.last();

						// Calculate the offset to move the last item to
						var left = -(last.outerWidth() + cycle.options.offset);

						// Set the position for the last item
						last.css('left', left +'px');

						// Move the last item to the first position
						last.prependTo(last.parent());

						// Reset the num animated
						var num_animated = 0;

						// Calculate the number of pixels to scroll
						var scroll_amt = last.outerWidth() + cycle.options.offset;

						// Animate all the items
						$(items).animate({left: '+='+ scroll_amt}, cycle.options.speed, function(){
							num_animated++;

							if(num_animated == items.length){
								cycle.scrolling = false;
							}
						});

						// Restart the timer
						$.cycle(label, "start");

					}

					break;
				case "next":

					if(!cycle.scrolling){

						cycle.scrolling = true;

						// Stop the timer for the given label
						$(document).stopTime(label);

						// Reset the num animated
						var num_animated = 0;

						// Retrieve the item list
						var items = $(cycle.options.selector);

						var next = items.eq(1);

						if(next.length){

							// Calculate the number of pixels to scroll
							var scroll_amt = next.position().left;

							// Animate all the items
							$(items).animate({left: '-='+ scroll_amt}, cycle.options.speed, function(){

								// Increment the number of animated items
								num_animated++;

								// Check if all the animations are complete
								if(num_animated == items.length){

									cycle.scrolling = false;

									// Get the first/last items
									var first	= items.first();
									var last	= items.last();

									// Calculate the offset to move the first item to
									var left = last.position().left + last.outerWidth() + cycle.options.offset;

									// Set the position for the first item
									first.css('left', left +'px');

									// Move the first item to the last position
									first.appendTo(first.parent());

								}

							});

						}

						// Restart the timer
						$.cycle(label, "start");

					}

					break;
			}

		}

	};

})(jQuery);

