/**
* jquery.delayedfade.js
*
* jQuery plugin for creating fade in and fade out effects
*
* @version 1.0.1, 2011-02-09
* @author Scott Park <scott@firefallpro.com>
* @author Stefan Woskowiak <stefan@firefallpro.com>
* @link http://www.firefallpro.com/
* @license http://www.firefallpro.com/license.txt
* @copyright Copyright (c) 2010 Firefall Pro, LLC
*
* Options:
*	duration - time the element remains displayed until fading out
*	pause - time until the next element fades in after fading out
*	fadeOut - time in milleseconds ( also 'slow', 'normal' and 'fast') for the object to fade out
*	fadeIn - time in milleseconds ( also 'slow', 'normal' and 'fast') for the object to fade in 
*/
(function($) {
	$.fn.delayedfade = function(options) {
		// Defaults
		var defaults = {
			duration: 3000,
			pause: 300,
			fadeOut: 'slow',
			fadeIn: 'normal'
		}
		
		var settings = $.extend(defaults,options);
		var selection = this;
		
		function fade(e) {
			$(e)
				.delay(settings.duration) // Frame duration
				.fadeOut(settings.fadeOut, function() {
					var next = $(this).next();
					
					if (next.length == 0) {
						next = $(this).prevAll().last();
					}
					
					next
						.delay(settings.pause) // Time between frames
						.fadeIn(settings.fadeIn, function() {
							fade(this);
						});
				});
		}

		// Setup initial state
		selection.hide().first().show(1, function() {
			fade(this)
		});
		
		return this;
	}
})(jQuery);
