;(function($){
	
	$.slideshow = {
		
		// "constants" for changing slides
		NEXT: 1,
		PREV: -1,
		
		// The number of items in the slideshow
		length: 0,
		
		// Are we paused? Initially, yes.
		paused: true,
		
		// Timeout controlling the slideshow when unpaused
		timeout: null,
		
		init: function()
		{
			var self = this;
			
			// Store the slideshow's length for finding edge cases later on
			this.length = $('#slideshow div').length;
			
			// Controls
			var $next = $('#controls .next');
			var $prev = $('#controls .prev');
			var $pause = $('#controls .pause');
			
			// Bind the control's event handlers
			$next.click(function() 
			{
				self.change.apply(self.current(), [self.NEXT]);
				return false;
			});

			$prev.click(function() 
			{
				self.change.apply(self.current(), [self.PREV]);
				return false;
			})
			
			$pause.click(function() 
			{
				self.pause.apply(self.current(), []);
				return false;
			});
			
			// Start playing
			this.pause();
		},
		
		current: function()
		{
			return $('#slideshow .current').length ? $('#slideshow .current') : $('#slideshow div:first');
		},
		
		change: function(which)
		{
			// Store the current item so that we can find the next one
			var $current = $(this);
			
			// Find out which direction we're going in
			switch (which)
			{
				// Going to the next item
				case $.slideshow.NEXT:
				
					$current = $current.next('div');
					
					// Check for edge cases
					if (!$current.length)
					{
						$current = $('#slideshow div').eq(0);
					}
					break;
				
				// Heading backwards	
				case $.slideshow.PREV:
				
					$current = $current.prev('div');
					
					// Check for edge cases
					if (!$current.length)
					{
						$current = $('#slideshow div').eq($.slideshow.length - 1);
					}
					break;
					
				// Just in case
				default:
					$current = $('#slideshow div').eq(0);
					break;
			}
			
			// Hide all other elements
			$('#slideshow div').removeClass('current')
			
			// Create the current item
			$current.addClass('current');
				
			// Show the one we found
			$('#slideshow div:not(.current)')
				.css('z-index', 1)
				.stop()
				.fadeTo('normal', 0);

			$('#slideshow div.current')
				.css('z-index', 2)
				.stop()
				.fadeTo('normal', 1);
			
			// Reset the timeout
			clearTimeout($.slideshow.timeout);
			
			// And re-enable it if we're not paused
			if (!$.slideshow.paused) 
			{
				$.slideshow.timeout = setTimeout(function() 
				{
					$('#controls .next').click();
				}, 7000);
			}
		},
		
		pause: function()
		{
			if ($.slideshow.paused)
			{
				$.slideshow.timeout = setTimeout(function() 
				{
					$('#controls .next').click();
				}, 7000);
				
				$.slideshow.paused = false;
				
				// Update the play/pause button
				$('#controls .pause').removeClass('paused');
			} 
			else 
			{
				clearTimeout($.slideshow.timeout);
				$.slideshow.paused = true;
				
				// Update the play/pause button
				$('#controls .pause').addClass('paused');
			}
		}
	};
	
})(jQuery);
