(function($){
	/* 
		This is a slideshow plugin adapted from http://jonraasch.com/blog/a-simple-jquery-slideshow
		It can be used on 1 or multiple returned element from the jQuery selector, multiple slideshows
		are possible on a single page, each slideshow with it's own interval, fadeSpeed, and randomness
		completely independent. A known issue is that if you make the interval and fadeSpeed too close,
		or at least very very close, you end up with race conditions between the next call of the function
		by setInterval, and the callback of the opacity animation. Just don't do it >:-|
	*/
	$.fn.slideShow = function(options){
		_this = $(this); // save this
		
		var defaults = {
			randomize: false, // randomly pick a starting image
			fadeSpeed: 'slow', // string, or integer
			interval: 5000 // interval between slide switches
		};
		
		var options = $.extend(defaults, options);
		
		_this.each(function(){
			var container = $(this);
			var images = $('img', container);
			
			if(options.randomize) {
				var active = $(images[Math.floor(Math.random() * (images.length))]).addClass('active');
			} else {
				var active = images.first().addClass('active');
			}
			
			setInterval(function(){
				var active = container.find('img.active');
				// find the next element, or swing back around to the first one
				var next = active.next().length ? active.next() : images.first();
				active.addClass('last_active');
				// makes the next image transparent, layers it on top of the current image, then makes itself opaque
				next.css('opacity', 0.0).addClass('active').animate({opacity: 1.0}, options.fadeSpeed, function(){
					active.removeClass('active last_active');
				});
			}, options.interval);
			
		});
		
		return _this; // return the initial jQuery object for chaining
	}
})(jQuery);
