// Slippy: A Simple Slider Widget
// Author: Thomas Peri
// Version 2009-12-22

var Slippy;

Slippy = function (options) {
	
	$(function () {
	
		var height, width, items, current, next, advance, begun;
	
		// set up the container except for its height
		$(options.container).css({
			position: 'relative',
			top: 0,
			left: 0,
			display: 'block',
			overflow: 'hidden'
		});
		
		// grab the width, set some other initial values
		width = $(options.container).width();
		height = options.minHeight || 0;
		items = [];
		current = 0;
		next = 0;
		begun = false;
		
		// stash all the items, and determine the tallest height
		$(options.item).each(function () {
			items.push(this);
			height = Math.max(height, $(this).height());
		});
		
		// make the container be the height of the tallest item
		$(options.container).css({
			height: height
		});
		
		// set up each item
		$(options.item).each(function () {
			$(this).css({
				position: 'absolute',
				top: 0,
				width: width,
				left: width,
				paddingTop: (height - $(this).height()) / 2
			});
		});
		
		// cycle through
		advance = function () {
			// If there's a current one yet,
			// animate it leftwards out of the viewport...
			if (begun) {
				$(items[current]).animate({
					left: -width
				});

				// ...then, relocate the next item to the right of the viewport
				// and animate it leftwards into visible position.
				$(items[next]).css({
					left: width
				}).animate({
					left: 0
				});
			} else {
				// If we haven't begun yet, put the first one where it needs to go
				// without animating it.
				$(items[next]).css({
					left: 0
				});
			}
			
			
			// What used to be the next item is now the current one.
			current = next;
			
			// Calculate which one is the new next one.
			next = (next + 1) % items.length;
			
			// Let the next iteration know we've already done one.
			begun = true;
		};
		
		advance();
		if (items.length > 1) {
			setInterval(advance, options.duration);
		}

	});
};
