(function($) {

	// Setup and start slideshow
	$.fn.slideshow = function(options) {
	
		// Defaults
		var defaults = jQuery.extend({
			auto : true, // Auto scroll through slides
			manualDelay : 10000, // Delay auto scroll when slide manually selected
			speed : 2000, // How long between automatic slide changes (must be numeric)
			duration : 600, // How long between transitions (must be numeric)
			navigation : null, // Navigation element parent jQuery object
			linkPrev : null, // Link to previous
			linkNext : null, // Link to next
			linkPause : null, // Link to play/pause
			counter : null // Show counter (eg. 1 of 12)
		}, options);
		
		// Start
		this.each(function() {
			
			// Setup
			var parent = $(this);
			var children = $(this).children();
			
			// Position elements
			parent.css({ position : 'relative' });
			children.each(function(i, o, u) {
				var child = $(this);
				if (i > 0) child.hide();
				child.css({ position : 'absolute', top : 0, left : 0 });
			});
			
			// Setup data
			parent.data('slideshowCurrentIndex', 0);
			parent.data('slideshowDefaults', defaults);
			
			// Navigation - Set child elements on click
			if (defaults.navigation) {
				defaults.navigation.children().each(function(i) {
					$(this).click(function() {
						parent.showSlideshowItem(i);
						return false;
					});
				});
			}
			
			// Links - Prev
			if (defaults.linkPrev) {
				defaults.linkPrev.click(function() {
					parent.showSlideshowItem(parent.data('slideshowCurrentIndex')-1);
					return false;
				});
			}
			
			// Links - Next
			if (defaults.linkNext) {
				defaults.linkNext.click(function() {
					parent.showSlideshowItem(parent.data('slideshowCurrentIndex')+1);
					return false;
				});
			}
			
			// Links - Play/Pause
			if (defaults.linkPause) {
				defaults.linkPause.click(function() {
					playPause(parent);
					return false;
				});
			}
					
			// Update Counter
			if (defaults.counter) updateCounter(defaults.counter, parent);
			
			// Auto start
			if (defaults.auto) {
			
				// Set timeout
				parent.data('slideshowTimeout', setTimeout(function() {
					parent.showNextSlideshowItem();
				}, defaults.speed));
			
			}
							
		});
		return this;
	};
	
	// Show next item (via timeout)
	$.fn.showNextSlideshowItem = function(i) {
		$(this).each(function() {
		
			// Show next
			var parent = $(this);
			var defaults = parent.data('slideshowDefaults');
			parent.showSlideshowItem(); // If no index passed then auto jump to next item
			parent.data('slideshowTimeout', setTimeout(function() {
				parent.showNextSlideshowItem();
			}, defaults.speed + defaults.duration));
		
		});
		return this;
	}
	
	// Show slideshow item
	$.fn.showSlideshowItem = function(i) {
		this.each(function() {
			
			// Check slideshow was setup
			var parent = $(this);
			var currentIndex = parent.data('slideshowCurrentIndex');
			var causeDelay = true; // Delay timeout on manual click
			if (!isNaN(currentIndex)) {
			
				// Check indexes
				if (typeof i == 'undefined') {
					i = currentIndex + 1; // If no i then move to next slide
					causeDelay = false; // Not manual change
				}
				if (i == currentIndex) return;
			
				// Check for lock (queue in future)
				if (parent.data('slideshowLock') === true) return;
				parent.data('slideshowLock', true);

				// Vars
				var currentItem = null;
				var nextItem = null;
				var defaults = parent.data('slideshowDefaults');
				
				// Delay timeout when item manually selected
				if (causeDelay) {
					var timeout = parent.data('slideshowTimeout');
					if (timeout != '') {
					
						// Clear timeout and start again after delay
						clearTimeout(timeout);
						parent.data('slideshowTimeout', setTimeout(function() {
							parent.showNextSlideshowItem();
						}, defaults.manualDelay));
						
					}
				}
				
				// Get items
				var children = parent.children();
				currentItem = $(children.get(currentIndex));
				i = i < 0 ? children.length + (i % children.length) : i % children.length; // Wrap
				nextItem = $(children.get(i));
				
				// Continue
				if (currentItem && nextItem) {

					// Layering
					currentItem.css({ zIndex : 10 });
					nextItem.css({ zIndex : 9 });
					
					// Animate
					nextItem.show();
					currentItem.fadeOut(defaults.duration, function() {
					
						// Fade complete so finish up
						parent.data('slideshowCurrentIndex', i);
						parent.removeData('slideshowLock');
					
						// Update counter
						if (defaults.counter) updateCounter(defaults.counter, parent);
					
					});
				
				}
			
			}
			
		});
		return this;
	};
	
	// Functions
	
	function playPause(parent) {
		var timeout = parent.data('slideshowTimeout');
		var defaults = parent.data('slideshowDefaults');
		if (timeout) {
			clearTimeout(timeout);
			defaults.linkPause.addClass('paused');
			defaults.linkPause.removeClass('playing');
			defaults.linkPause.text('Play');
			parent.removeData('slideshowTimeout')
		} else {
			parent.data('slideshowTimeout', setTimeout(function() {
				parent.showNextSlideshowItem();
			}, defaults.speed));
			defaults.linkPause.removeClass('paused');
			defaults.linkPause.addClass('playing');
			defaults.linkPause.text('Pause');
		}
	}
	
	function updateCounter(counter, parent) {
		counter.text((parent.data('slideshowCurrentIndex') + 1) + ' of ' + parent.children().length);
	}
	
})(jQuery);