/*
	hoverFade jQuery plugin version 1.0.2
	
	Copyright (c) 2010 Dan Wellman
  
	Dual licensed under the MIT and GPL licenses:
	http://www.opensource.org/licenses/mit-license.php
	http://www.gnu.org/licenses/gpl.html
	
*/

;(function($) {
	$.hoverFade = {
		defaults: {
			newClass: "hover-anims",
			classToRemove: "hover-css",
			onClass: "on",
			trigger: "a",
			faderTemplate: "<span />",
            animSpeed: "normal",
            exclude: "",
            ignoreSiblings: "",
            retainHoverOn: ""
		}
	};
	
	$.fn.extend({
		hoverFade: function(userConfig) {
																
			//use defaults or properties supplied by user
			var config = $.extend({}, $.hoverFade.defaults, userConfig),
				el = $(this),
				fader = config.faderTemplate.replace("<", "").replace(">", "").replace(" /", "");
			
			//add fader to target el(s)
			el.removeClass(config.classToRemove).addClass(config.newClass).children().each(function() {
				$(this).find(config.trigger).not(config.exclude).append(config.faderTemplate).find(fader).not(config.ignoreSiblings).css("opacity", 0);												
			});
			
			//show element with on class
			$("." + config.onClass, el).find(fader).not(config.ignoreSiblings).css("opacity", 1);
						
			//fade in on hover, fade out on off
			$(el).find(config.trigger).not("." + config.onClass).mouseenter(function(){
				$(this).find(fader).not(config.ignoreSiblings).stop().animate({
					opacity: 1
				}, config.animSpeed);
			});
			
            $(el).find(config.trigger).not("." + config.onClass).mouseleave(function(e){
				
                //retain hover when entering specific element 'within' hover element
                if(config.retainHoverOn !== "") {
                    if ($(e.relatedTarget).attr("class").indexOf(config.retainHoverOn) == -1) {
				        $(this).find(fader).not(config.ignoreSiblings).stop().animate({
					        opacity: 0
				        }, config.animSpeed);
                    }
                } else {					
					$(this).find(fader).not(config.ignoreSiblings).stop().animate({
				        opacity: 0
			        }, config.animSpeed);	
				}
			});
			
			//return the jquery object for chaining
			return this;
		}
	});
})(jQuery);
