/* Created by Martin Hintzmann 2008 martin [a] hintzmann.dk
 * MIT (http://www.opensource.org/licenses/mit-license.php) licensed.
 *
 * Version: 0.1
 *
 * Requires:
 *   jQuery 1.2+
 */
(function($) {
	$.fn.boxShadow = function( option ) { //xOffset, yOffset, blurRadius, shadowColor, transparentColor) {
		if (!$.browser.msie) return;
		return this.each(function(){
			var el = $(this);
			var shadow = el.boxShadowParse(this.currentStyle["box-shadow"]);
			shadow = $.extend(shadow, option);
			if (shadow.xOffset == 0 && shadow.yOffset == 0 && shadow.blurRadius == 0) return;
			
			$(this).css({
				position:	"relative",
				zoom: 		1,
				zIndex:		"2"
			});
			$(this).parent().css({
					position:	"relative"
			});
			
			var div=document.createElement("div");
			$(this).parent().append(div);

			var _top, _left, _width, _height;
			if (shadow.blurRadius != 0) {
				//$(div).css("filter", "progid:DXImageTransform.Microsoft.Blur(pixelRadius="+(blurRadius)+", enabled='true')");
				$(div).css("filter", "progid:DXImageTransform.Microsoft.Blur(pixelRadius="+(shadow.blurRadius)+", makeshadow='false', ShadowOpacity=1.0, enabled='true')");
				_top = 		0;//-blurRadius/2-1;
				_left =		0;//-blurRadius/2-1;
				_width =	$(this).outerWidth()-(shadow.blurRadius)/2-1;
				_height =	$(this).outerHeight()-(shadow.blurRadius)/2-1;
			} else {
				_top = 		shadow.yOffset;
				_left =		shadow.xOffset;
				_width = 	$(this).outerWidth();
				_height = 	$(this).outerHeight();
			}
			$(div).css({
				top: 		_top,
				left:		_left,
				width:		_width,
				height:		_height,
				backgroundColor:	shadow.shadowColor,
				position:	"absolute",
				zIndex:		1
			});

			if (shadow.transparentColor){ // make it 'transparent'
				var div=document.createElement("div");
				$(this).parent().append(div);
				$(div).css({
					top: 				0,
					left:				0,
					width:				$(this).outerWidth(),
					height:				$(this).outerHeight(),
					backgroundColor:	shadow.transparentColor,
					position:			"absolute",
					zIndex:				1
				});
			};
			
	  });
	};
	
	$.fn.boxShadowParse = function(value) 
	{
		value = String(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)')
	
		var shadow = {
			xOffset		: 0,
			yOffset		: 0,
			blurRadius	: 0,
			shadowColor	: null,
			transparentColor: null
		};

		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.shadowColor = color.replace(/^\s+/, '');
				value = value.replace(shadow.shadowColor, '');
			}

			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.xOffset = shadow.yOffset = value[0];
					break;
				case 2:
					shadow.xOffset = value[0];
					shadow.yOffset = value[1];
					break;
				case 3:
					shadow.xOffset = value[0];
					shadow.yOffset = value[1];
					shadow.blurRadius = parseInt(value[2]);
					break;
				case 4:
					shadow.xOffset = value[0];
					shadow.yOffset = value[1];
					shadow.blurRadius = parseInt(value[2]);
					shadow.transparentColor = value[3];
					break;	
			}
			if ((!shadow.xOffset && !shadow.yOffset && !shadow.blurRadius) || shadow.shadowColor == 'transparent') {
				shadow.xOffset = shadow.yOffset = shadow.blurRadius = 0;
				shadow.shadowColor = null;
			}
		}

		return shadow;
	};

})(jQuery);
