/**
 * @name    Crossfade
 * @desc    jQuery image crossfading plugin
 * @author  Trey Shugart
 * @date    2008-05-11
 * @license GNU LGPL
 * @version 1.1.0
 * 
 * Optional: jquery.hoverintent
 */
;(function($) {
	
	$.fn.crossfade = function(options, endDelay) {
		var options = isNaN(options) 
			? options 
			: {
				startDelay : options, 
				endDelay   : (typeof endDelay === 'undefined') 
					? options 
					: endDelay
			};
		
		var options = $.extend({
			hoverIntent    : false,
			startDelay     : 1000, // amount of time it takes to fade from the first image to the second image (in milliseconds)
			endDelay       : 1000, // amount of time it takes to fade from the second image back to the first image (in milliseconds)
			startCallback  : _blank, // function to call when the crossfading begins
			endCallback    : _blank, // function to call when the second image is transitioning back to the first image
			startCondition : _blank, // condition to evaluate/return true if the crossfading is to start
			endCondition   : _blank, // condition to evaluate/return true if the crossfading is to complete
			useAttr        : 'style', // the attribute to pull the second image source path from
			regex          : 'background(-image)?:.*url\\((.+)\\)' // the regex used to find the value/src of the second image
		}, options);
		
		// filter out all objects that don't have an attribute to read the alternate image from
		// using filter('[style]') doesn't work
		$(this).filter(function() {
			return $(this).attr(options.useAttr);
		}).each(function() {
			var
				$$      = $(this),
				img     = $('<img />'),
				regex   = new RegExp(options.regex, 'i'),
				matches = $$.attr(options.useAttr).match(regex);
			
			if (!matches) {
				return;
			}
			
			var src = matches[2].replace(/'|"/g, '');
			
			// replace the part of the attribute that we used with nothing and apply necessary styles to the original image
			$$.attr(options.useAttr, $$.attr(options.useAttr).replace(regex, '')).css({
				position : 'relative',
				zIndex   : 10001
			});
			
			// create the crossfading image
			img.attr('src', src).css({
				position : 'absolute',
				zIndex   : 10000,
				opacity  : 0
			}).insertBefore($$);
			
			// if jquery.hoverIntent is available, use it
			if (options.hoverIntent && $.isFunction($.fn.hoverIntent)) {
				$$.hoverIntent($.extend(options.hoverIntent, {
					over : _over,
					out  : _out
				}));
			// otherwise use the regular hover event
			} else {
				$$.hover(_over, _out);
			}
			
			
			
			/**
			 * Function that is called on the hover (over) event
			 */
			function _over() {
				if (options.startCondition($$)) {
					$$.fadeTo(options.startDelay, 0);
					img.fadeTo(options.startDelay, 1);
					options.startCallback($$, img);
				}
			}
			
			/**
			 * Function that is called on the hover (out) event
			 */
			function _out() {
				if (options.endCondition($$)) {
					$$.fadeTo(options.endDelay, 1);
					img.fadeTo(options.startDelay, 0);
					options.endCallback($$, img);
				}
			}
		});
		
		
		
		/**
		 * A function that simply returns true so I didn't have to type: function() { return true; }, four times above.
		 */
		function _blank() {
			return true;
		}
	};
	
})(jQuery);
