/**
 * simpleSlides - jQuery Plugin
 *
 * Version: 1.0
 * Created: 2009-09-10
 *
 * Copyright (c) 2009 Kreck Design Solutions
 *
 * Licensed under FreeBSD license.
 *
 * Based on the Simple jQuery Slideshow Script, released by Jon Raasch (jonraasch.com) under FreeBSD license.
 * Thank you, Jon!   http://jonraasch.com/blog/a-simple-jquery-slideshow
 * Additional options and features, and conversion to plugin, by Kreck Design Solutions (www.kreck.com).
 *
 * Requirements:
 * - jQuery (John Resig, http://www.jquery.com/)
 *
 *
 * Options ("opts"):
 *	.delay - time between switches
 *  .activeClass - the CSS class to apply to active img
 *	.animationLength - how long the fading is animated
 *
 * Here are some sample styles you should include:
 * (Replace "#slideshow" with whatever your container is named!)
 


#slideshow {
	position:relative;
	height:210px;
}
#slideshow DIV {
	position:absolute;
	top:0;
	left:0;
	z-index:8;						// should be on the bottom
	opacity:0.0;
	height: 210px;
}
#slideshow DIV.active {
	z-index:10;						// should be on the top
	opacity:1.0;
}
#slideshow DIV.last-active {
	z-index:9;						// should be somewhere in the middle
}
#slideshow DIV IMG {
	height: 210px;
	display: block;
	border: 0;
	margin-bottom: 10px;
}

 
 *
 **/

(function($) {
	$.fn.simpleSlides = function(usageOptions) {
		
		var defaultOptions = {
			delay: 4500,
			activeClass : 'active',
			lastActiveClass : 'active-last',
			animationLength : 800,
			randomize : false
		};
		
		var opts = $.extend({}, defaultOptions, usageOptions);
		
		var $slides = this;
		
		var slideSwitch = function() {
		
			var $active = $slides.filter('.' + opts.activeClass);
			if ($active.length == 0) {
				if (opts.randomize) {
					$active = $slides.eq(Math.floor(Math.random() * $slides.length ));
				}
				else {
					$active = $slides.filter(':last');	// making the last one active, means the first will be what gets activated!
				}
			}
		
			// use this to pull the divs in the order they appear in the markup
			var $next = $active.next().length ? $active.next() : $slides.filter(':first');
		
			// pull the divs randomly?
			if (opts.randomize) {
				var $sibs  = $active.siblings();	// Hmm, should make this $slides.not($active) or similar ???
				var rndNum = Math.floor(Math.random() * $sibs.length );
				$next  = $sibs.eq(rndNum);
			}
		
			$active
					.addClass(opts.lastActiveClass)
					.animate({opacity: 0.0}, opts.animationLength / 1.5);
		
			$next
					.css({opacity: 0.0})
					.addClass('active')
					.animate({opacity: 1.0}, opts.animationLength, function() {
						$active.removeClass(opts.activeClass + ' ' + opts.lastActiveClass);
					});
		
		};
		
		this.css({opacity: 0.0});
		slideSwitch();
		setInterval( slideSwitch, opts.delay);
		return this;
				
	};
})(jQuery);



