//(c) Copyright 2009 Buttered Toast, released under MIT License.
//Originally by Pat Nakajima

//Modified by Amit Ron (Buttered Toast) so we don't need to use
//the AddGlow method. Instead, use as a normal effect:
//$("selector").animate({"text-shadow": "yellow 2pt 0pt 20pt"})
//Note that the sizes are in points, not pixels!

(function($) {
	function stepTextShadow(fx) {
		if (fx.state == 0) {
			fx.start = textShadowParse(fx.elem);
			fx.end = textShadowParse(fx.end);
		}
		
		var updatedRadius = (fx.end.radius > fx.start.radius) ?
			(fx.start.radius + (fx.end.radius - fx.start.radius) * fx.pos) :
			(fx.start.radius - (fx.start.radius - fx.end.radius) * fx.pos);
		
		var updatedx = (fx.end.x > fx.start.x) ?
			(fx.start.x + (fx.end.x - fx.start.x) * fx.pos) :
			(fx.start.x - (fx.start.x - fx.end.x) * fx.pos);
		
		var updatedy = (fx.end.y > fx.start.y) ?
			(fx.start.y + (fx.end.y - fx.start.y) * fx.pos) :
			(fx.start.y - (fx.start.y - fx.end.y) * fx.pos);
		
		if(updatedRadius < 2)
			$(fx.elem).css('text-shadow', 'none');
		else
			$(fx.elem).css('text-shadow', (fx.end.color || fx.start.color) + ' ' + updatedx + 'pt ' + updatedy + 'pt '+ updatedRadius + 'pt');
	}
	
	function textShadowParse(v) 
	{
		var value = (typeof v == "string") ? v : $(v).css("text-shadow");
		
		var shadow = {
			x      : 0,
			y      : 0,
			radius : 0,
			color  : null
		};
		
		if(value == "none")
			return shadow;
		
		value = value
			.replace(/^\s+|\s+$/gi, '')
			.replace(/\s*!\s*important/i, '')
			.replace(/\(\s*([^,\)]+)\s*,\s*([^,\)]+)\s*,\s*([^,\)]+)\s*,\s*([^\)]+)\s*\)/g, '($1/$2/$3/$4)')
			.replace(/\(\s*([^,\)]+)\s*,\s*([^,\)]+)\s*,\s*([^\)]+)\s*\)/g, '($1/$2/$3)');
		
		if (value.length > 1 || value[0].toLowerCase() != 'none') {
			value = value.replace(/\//g, ',');
			var color;
			if ( value.match(/(\#[0-9a-f]{6}|\#[0-9a-f]{3}|(rgb|hsb)a?\([^\)]*\)|\b[a-z]+\b)/i) && (color = RegExp.$1) ) {
				shadow.color = color.replace(/^\s+/, '');
				value = value.replace(shadow.color, '');
			}
			value = value
				.replace(/^\s+|\s+$/g, '')
				.split(/\s+/)
				.map(function(item) {
					return (item || '').replace(/^0[a-z]*$/, '') ? item : 0 ;
				});
			
			switch (value.length) {
				case 1:
					shadow.x = shadow.y = value[0];
					break;
				case 2:
					shadow.x = value[0];
					shadow.y = value[1];
					break;
				case 3:
					shadow.x = value[0];
					shadow.y = value[1];
					shadow.radius = value[2];
					break;
			}
			
			if ((!shadow.x && !shadow.y && !shadow.radius) || shadow.color == 'transparent') {
				shadow.x = shadow.y = shadow.radius = 0;
				shadow.color = null;
			}
		}
		
		if(typeof shadow.radius == "string")
			shadow.radius = parseFloat(shadow.radius.replace(/[^0-9]*$/, ''));
		else
			shadow.radius = 0;
		if(typeof shadow.x == "string")
			shadow.x = parseFloat(shadow.x.replace(/[^0-9]*$/, ''));
		else
			shadow.x = 0;
		if(typeof shadow.y == "string")
			shadow.y = parseFloat(shadow.y.replace(/[^0-9]*$/, ''));
		else
			shadow.y = 0;
		
		return shadow;
	};
	
	$.fx.step['textShadow'] = stepTextShadow;
})(jQuery);
