(function( $ ){
	$.fn.hoverFadeColor = function(){		
		var $self = $(this),
		args = Array.prototype.slice.apply(arguments),
		namespace = "hoverFadeColor",
		config = {
			'color': null,
			'fadeToSpeed': 300,
			'fadeFromSpeed': 700,
			'stopHoverAnimationOnOut' : false
		},
		
		instancesConfig = function() {
			return {
				enable : true
			}
		},		
		
		instanceMethods = {
			hover: function(color){
				var $this = $(this);
				instancesConfig = $(this).data(namespace + "-instancesConfig");
				instancesConfig.inHover = true;
				$(this).data(namespace + "-instancesConfig", instancesConfig);
				
				if (!instancesConfig.enable) return;
				var hovercolor = $(this).data(namespace + "-hoverColor");
				var originalColor = $(this).data(namespace + "-originalColor");				
				
				if (hovercolor == null && $.browser.msie && $.browser.version.substr(0,1)<9) {										
					setTimeout( function() {
						if(hovercolor == null) {
							hovercolor = $this.css("color");
							$this.data(namespace + "-hoverColor", hovercolor);
						}

						$this.css('color', originalColor);

						$this.stop().animate({
							color: hovercolor
						}, config.fadeToSpeed);
					},0);
				} else {
					if(hovercolor == null) {
						hovercolor = $(this).css("color");
						$(this).data(namespace + "-hoverColor", hovercolor);
					}

					$(this).css('color', originalColor);

					$(this).stop().animate({
						color: hovercolor
					}, config.fadeToSpeed);
				}				
			},
			out: function (){				
				if (!instancesConfig.enable) return;
				var originalColor = $(this).data(namespace + "-originalColor");
				
				if (config.stopHoverAnimationOnOut) $(this).stop();
				$(this).animate({
					color: originalColor
				}, config.fadeFromSpeed);
				
				instancesConfig = $(this).data(namespace + "-instancesConfig");
				instancesConfig.inHover = false;
				$(this).data(namespace + "-instancesConfig", instancesConfig);
			},
			
			enable: function(value){
				instancesConfig = $(this).data(namespace + "-instancesConfig");
				instancesConfig.enable = value;
			}
		};
		
		if ($self.data(namespace + "-defined")) {
			config = $self.data(namespace + "-config");
			instancesConfig = $self.data(namespace + "-instancesConfig");
			if (args.length > 0) {
				instanceMethods[args[0]].apply(this, args.slice(1, args.length));
			}
			return $self;
		}else{
			$self.data(namespace + "-defined", true);
			if (args.length > 0 && $.isPlainObject(args[0])) {
				config = $.extend(config, args[0]);
			}
			$self.data(namespace + "-config", config);			
			
			this.each( function() {
				//var originalColor = $(this).css("color");			
				
				$(this).data(namespace + "-instancesConfig", new instancesConfig());
				$(this).data(namespace + "-originalColor", $(this).css("color"));
				$(this).data(namespace + "-hoverColor", config.color);
				$(this).hover(instanceMethods.hover, instanceMethods.out);
			});
		}
		return $self;
	}
})( jQuery );
