/*
	VERSION: Drop Shadow jQuery Plugin 0.1  11-12-2009
	
	AUTHOR: Ryan Sorensen - Nextline Media LLC

	REQUIRES: jquery.js and jquery.dimensions.js and dd_roundies (optional)

	SYNTAX:  $(selector).dropShadow(options);  // Creates new drop shadows
	EXAMPLE: $(selector).dropShadow({blur:10,radius:'4px'});  // Creates new drop shadows

	OPTIONS:

		left    : integer (default = 0)
		top     : integer (default = 0)
		blur    : integer (default = 5)
		color   : string (default = "#5b5b5b")
		radius  : string (default = "0px")  //Need DD_roundies to apply radius
*/


(function($){
	
	$.fn.dropShadow = function(options){
		// Default options
		var opt = $.extend({
			left:   0,
			top:    0,
			blur:   5,
			color:  '#5b5b5b',
			radius: '0px'  //DDroundies required
			}, options);

			this.each(function(){
				if(opt.radius != '0px'){
					try{
						DD_roundies.addRule('#'+this.id, opt.radius, true);
					}catch(err){}
				}

				if(!$.browser.msie){
					non_ie_shadow(this, opt);
				}else{
					//applyShadow(this, opt);	
				}

			});

			function non_ie_shadow(box, opt){

				$(box).css('-moz-box-shadow',opt.left+'px '+opt.top+'px '+opt.blur+'px '+opt.color)
				      .css('-webkit-box-shadow',opt.left+'px '+opt.top+'px '+opt.blur+'px '+opt.color)
				      .css('box-shadow',opt.left+'px '+opt.top+'px '+opt.blur+'px '+opt.color);

			}

			function applyShadow(box, opt){

				var shadowId;
				var $$ = $(box);
				// 
				// $$.css({
				// 	position:'relative',
				// 	zoom:'1'
				// });
				
				// Create ID for shadow
				if ($$.attr('id')) {
					shadowId = $$.attr('id') + "_dropShadow";
				}else {
					shadowId = "ds" + (1 + Math.floor(9999 * Math.random()));
				}

				$$.before('<div id="'+shadowId+'" class="jq_dropShadow"></div>');
				var offset = 1; //for some reason when a blur is set it moves down/left 1px.  This fixes that issue.
				var soft = Math.ceil(opt.blur/2);  //This makes the shadow a bit softer. The bigger it gets the softer the shadow will be.
				var blur = ((opt.blur/2)+.5)+soft;  //Sets the correct blur amount to try and match firefox/safari/chrome etc...
				var shadow = $('#'+shadowId).css('filter', 'progid:DXImageTransform.Microsoft.Blur(pixelRadius="'+(blur)+'", ShadowOpacity="0.20")')
											.css('height',$$.outerHeight() + opt.blur - (soft*2))
											.css('width',$$.outerWidth()  + opt.blur - (soft*2))
											.css('left',$$.position().left + opt.left + strip_px($$.css('margin-left')) - offset - opt.blur)
											.css('top',$$.position().top + opt.top + strip_px($$.css('margin-top')) - offset - opt.blur)
											.css('position','absolute')
											.css('background-color',opt.color);

				shadow.css({  //This is here to fix a positioning issue.  Without it the shadow will be positioned over the element.
					left: $$.position().left + opt.left + strip_px($$.css('margin-left')) - offset - opt.blur,
					top: $$.position().top + opt.top + strip_px($$.css('margin-top')) - offset - opt.blur
				});

				// var shadow = $('#jquery-shadow').css('filter', 'progid:DXImageTransform.Microsoft.Blur(pixelRadius="'+((opt.blur/2)+.5)+'", ShadowOpacity="0.20")')
				// 				   .css('height',that.outerHeight() + opt.blur)
				// 				   .css('width',that.outerWidth()  + opt.blur)
				// 				   .css('left',that.position().left + opt.left - offset - opt.blur)
				// 				   .css('top',that.position().top + opt.top - offset - opt.blur)
				// 				   .css('position','absolute')
				// 				   .css('z-index','-1')
				// 				   .css('background-color',opt.color);


				$(window).resize(function(){
					try {
						shadow.css({
							display:'none'
							//left: $$.position().left + opt.left + strip_px($$.css('margin-left')) - offset - opt.blur,
							//top: $$.position().top + opt.top + strip_px($$.css('margin-top')) - offset - opt.blur
						});
						
						if(ds_timer){
							clearTimeout(ds_timer);
						}
						
						var ds_timer = setTimeout(function(){
							shadow.css({
								display: 'block',
								left: $$.position().left + opt.left + strip_px($$.css('margin-left')) - offset - opt.blur,
								top: $$.position().top + opt.top + strip_px($$.css('margin-top')) - offset - opt.blur
							});
							},500);
					}catch(e){}
				});

			}

			return this;
		};

	// Removes 'px' from string
	function strip_px(value){
		if (typeof(value)!='string') return value;
		return parseInt((( value != "auto" && value.indexOf("%") == -1 && value != "" && value.indexOf("px") !== -1)? Math.round(value.slice(0, value.indexOf("px"))) : 0))
	}
	
})(jQuery);
