/* 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() {
		if (!$.browser.msie) return;
		if($.browser.version > 8) return;
		var IE6 = $.browser.version < 7;
		return this.each(function(){
			/*$(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 (blurRadius != 0) {
				$(div).css("filter", "progid:DXImageTransform.Microsoft.Blur(pixelRadius="+(blurRadius)+", enabled='true')");
				_top = 		yOffset-blurRadius-1;
				_left =		xOffset-blurRadius-1;
				_width =		$(this).outerWidth()+1;
				_height =	$(this).outerHeight()+1;
			} else {
				_top = 		yOffset;
				_left =		xOffset;
				_width = 	$(this).outerWidth();
				_height = 	$(this).outerHeight();
			}
			$(div).css({
				top: 			_top,
				left:			_left,
				width:		_width,
				height:		_height,
				background:	shadowColor,
				position:	"absolute",
				zIndex:		1
			}); */
			
			
			var el = $(this);
			var shadow = el.boxShadowParse(this.currentStyle["box-shadow"]);
			
			if (shadow.color == null) return;
			
			el.boxShadowRemove();
			
			//if (shadow.x == 0 && shadow.y == 0 && shadow.radius == 0) return;

			if (el.parent().css("position")=="static") {
				el.parent().css({position:"relative"});
			}
			el.parent().css({zIndex:"0"});
			if (IE6) {
				el.parent().css({zoom:"1"});
			}
			
			/* var span=document.createElement("span"); */
			/*var div=document.createElement("div"); */
			var div=el.clone();
			div.empty();
			
			$(div).addClass("jQueryBoxShadow");
			
			$(div).css({
				padding:		this.currentStyle["padding"],
				margin:			this.currentStyle["margin"],				
				width:			el.width(),
				height:			el.height(),
				position:		"absolute",
				zIndex:			"-1",
				background:		shadow.color!=null?shadow.color:el.css("color"),
				left:			el.position().left + (-parseInt(shadow.radius)+parseInt(shadow.x))+"px",
				top:			el.position().top + (-parseInt(shadow.radius)+parseInt(shadow.y))+"px"
			});
			if (shadow.radius != 0) {
				$(div).css("filter", "progid:DXImageTransform.Microsoft.Blur(pixelRadius="+parseInt(shadow.radius)+", enabled='true')");
			}	
			/*el.append(span); */
			el.parent().append(div);
			
			$(window).resize(function() {
				$(div).css("left", el.position().left + (-parseInt(shadow.radius)+parseInt(shadow.x))+"px");
				$(div).css("top", el.position().top + (-parseInt(shadow.radius)+parseInt(shadow.y))+"px");
			});
			
	  });
	};
	
	/* Borrowed from textshadow plugin */
	$.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 = {
			x      : 0,
			y      : 0,
			radius : 0,
			color  : 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.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;
			}
		}

		return shadow;
	};
	
	$.fn.boxShadowRemove = function() {
		if (!$.browser.msie) return;
		return this.each(function() {
			$(this).parent(".jQueryBoxShadow").remove();
		});
	};
	
})(jQuery);
