(function($) {	
	// actual slideshow function
	$.fn.slideshow = function(o) {
		// options
		options = $.extend({
			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
			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
			reverse: false, // reverses slideshow elements since the first placed element will be the last one to be shown unless reversed
			
			onStart: 'undefined',
			onComplete: 'undefined'
		}, o);
		
		// reference to the matched elements
		var $$ = this;
		
		// return false if there are no found elements
		if (typeof($$) === 'undefined') {
			return false;
		}
		
		if (options.reverse) {
			options.reverse = false;
			reverse($$);
		}
		
		$$.show();
		
		// if there is more than one slideshow element
		if ($$.length > 1) {
			for (var i = 0; i < $$.length; i++) {
				// the current position in the selection of elements
				var current = ($$.length - 1) - i;
				
				if (i === 0) {
					// first image but not first time around
					var time = options.pauseTime;
				} else {
					// otherwise calculate the delay for the current image
					var time = (i * options.pauseTime) + ((i+1) * options.transTime);
				}
				
				// pause
				$$.eq(current).fadeTo(time, 1, function() {
					// if it's the end and we are looping
					if ($$.index(this) == current && options.loop) {
						// fade in the first one
						$$.filter(':last').fadeIn(options.transTime, function() {
							// restart the slideshow
							$$.slideshow(options);
						});
					} else {
						// otherwise just fade out the current element
						$(this).fadeOut(options.transTime);
					}
				});
			}
		}
	
		/*
		*	---- STILL A WORK IN PROGRESS ----
		*	For some reason this does not return the modified jQuery object.
		*	Reverses the first children elements so that the last one will then be the first one an so forth
		*/
		function reverse(obj) {
			obj.each(function() {
				$(this).insertAfter(obj.eq(obj.length-1));
			});
			return obj;
		}
	}
})(jQuery);