(function($) {
	// so we don't get an undefined error
	$.slideshow = {};
	
	// options
	$.slideshow.options = {
		removeFirst: false, // whether or not to remove the first element; used for an initial image mask
		transTime: 1000, // time in milliseconds it will take for the animation between images to complete
		pauseTime: 2000, // time in milliseconcs it will show each image before transitioning to the next one
		pauseFirstTime: 2000, // the pause time for the first element or image
		loop: true, // whether to loop the slideshow or not, if not then it will stop on the last one but not fade it out, this would be achieved by using an onComplete callback
		play: true, // whether to play or not, including when the page loads and if the stop/pause button is pressed
		
		onStart: 'undefined',
		onComplete: 'undefined'
	}
	
	// actual slideshow function
	$.fn.slideshow = function(options) {
		// reference to the matched elements
		var $$ = $(this);
		
		$$.show();
		
		// return false if there are no found elements
		if (typeof($$) === 'undefined') {
			return false;
		}
		
		// set options
		if (typeof(options) !== 'undefined') {
			for (var i in options) {
				o(i, options[i]);
			}
		}
		
		for (var i = 0; i < $$.length; i++) {
			var current = ($$.length - 1) - i;
			if (i === 0) {
				// use pauseFirstTime
				var time = o('pauseFirstTime');
			} else {
				// otherwise calculate the delay for the current image
				var time = ((i+1) * o('pauseTime')) + ((i+1) * o('transTime'));
			}
			
			// pause
			$$.eq(current).fadeTo(time, 1, function() {
				// if it's the end and we are looping
				if ($$.index(this) == current && o('loop')) {
					// fade in the first one
					$$.filter(':last').fadeIn(o('transTime'), function() {
						// restart the slideshow
						$$.slideshow(o());
					});
				} else {
					// otherwise just fade out the current element
					$(this).fadeOut(o('transTime'));
				}
			});
		}
	}
	
	/*
	*	@param string option String name of the option you wish to retrieve or set
	*	@param mixed val Mixed value you wish to set the specified option to
	*	If val is set, then the specified option will be set to that value. If not
	*	then the option's current value will be returned.
	*/
	var o = function(option, val) {
		if (typeof(option) === 'undefined') {
			return $.slideshow.options;
		} else if (typeof(val) === 'undefined') {
			return $.slideshow.options[option];
		} else {
			$.slideshow.options[option] = val;
			return $.slideshow.options[option];
		}
	}
})(jQuery);