/* 
VERSION: 		beDropShadow 0.2 2011.04.10
REQUIRES: 		jquery.js (verified for v.1.5.2) 
AUTHOR: 		Andreas Bergsvik bergsvik@gmail.com
SYNTAX: 		$(selector).dropShadow();
				$(selector).dropShadow({options});
				
OPTIONS		: 	default (type)
size		:	5 (int)
top			: 	5 (int)
left		:	0 (int)
rgb			: 	new Array(0, 0, 0) (array)
opacity		:	0.5  (float)
winResize	: 	true (boolean)

TESTED: 
FF: 3.6.8, 3.6.16 (mac osx, win-xp)
Chrome: 10.0 (mac osx, win-xp)
Safari: 5.0 (mac osx)
IE: 6.0, 7.0, 8.0 (win-xp)
Opera: 11.01 (mac osx)

ABOUT THE PLUG-IN: 
The code uses CSS3 box-shadow syntax and the IE filter BLUR to generate the drop shadows. 

ISSUES: 
May bug when first element is floated in IE8

EXAMPLE USAGE:

$('.shadow').dropShadow({
	"size": 10, // int
	"top": 10, // int
	"left": 10, // int
	"rgb": new Array(0, 0, 0), // array
	"opacity": 0.5, // float
	"winResize" : true // bool (true || false)
});

SOON COME: 
jquery text-shadow !? 

*/
(function($) {
		  
	$.fn.removeDropShadow = function(){
		return this.each(function(){
			var $this = $(this);
			$this.next('.ieShadow').remove();
			var style = {
				"-moz-box-shadow" : "none",
				"-webkit-box-shadow" : "none",
				"box-shadow" : "none"
			}					  
			$this.removeClass('dropShadow').css(style);
		});
	}
		  
    $.fn.dropShadow = function(options){
		
		if (this.length > 1){
            this.each(function() { $(this).dropShadow(options) });
            return this;
        }
		
		// SETUP private variabls;
        var dropShadow = this;
		
		// SETUP options
		var defaults = {
			size:		5,
			top: 		5,
			left:		0,
			rgb: 		new Array(0, 0, 0), // eventuellt jobba om det och använda .join(',') el. .split(',')
			opacity:	0.5, 
			winResize: 	true // if static position you may set to false
		}
		var opt = $.extend({}, defaults, options);
		
		var blur = 	parseInt(opt.size);
		var top = 	parseInt(opt.top);
		var left =	parseInt(opt.left);
		var color = opt.rgb;
		var opacity = parseFloat(opt.opacity);
		
		var rgba = "rgba(" + color[0] + ", " + color[1] + ", " + color[2] + ", " + opacity + ")";
		var rgb = "rgb(" + color[0] + ", " + color[1] + ", " + color[2] + ")";
		
		var style = {
			"-moz-box-shadow" : left+"px "+top+"px "+blur+"px "+rgba,
			"-webkit-box-shadow" : left+"px "+top+"px "+blur+"px "+rgba,
			"box-shadow" : left+"px "+top+"px "+blur+"px "+rgba, 
			"position" : "relative"
		}
		
		return this.each(function() {
			
			if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
 			var ieversion=new Number(RegExp.$1); // capture x.x portion and store as a number
			}
			 /*
			 if (ieversion>=8)
			  document.write("You're using IE8 or above")
			 else if (ieversion>=7)
			  document.write("You're using IE7.x")
			 else if (ieversion>=6)
			  document.write("You're using IE6.x")
			 else if (ieversion>=5)
			  document.write("You're using IE5.x")
			}
			else
			 document.write("n/a")
			 */
			
			var $this = $(this);
			var zIndex = parseInt($this.parents().length);
			$this.css('zIndex', zIndex);
			// start fresh
			if( $this.hasClass('dropShadow') ){
				$this.removeDropShadow();
			}
			$this.addClass('dropShadow').css(style);
			/* internet explorer blur hack */
			
			if(ieversion != 9){
				if(navigator.appName == "Microsoft Internet Explorer")
				{
					var newId = "ie_" + Math.floor(Math.random() * 9999);
					var pos = $this.offset();
					$this.after("<div id='"+newId+"' class='ieShadow'></div>");
					var cssObj = {
						'top' : (pos.top + top - blur)	+ "px",
						'left' : (pos.left + left - blur)	+ "px",
						'background-color' : rgb,
						'position' : 'absolute',
						'z-index': zIndex - 1
					}
					var realHeight = $this.height() + parseInt($this.css('padding-top')) + parseInt($this.css('padding-bottom'));
					var realWidth = $this.width() + parseInt($this.css('padding-left')) + parseInt($this.css('padding-right'));
					$("#" + newId)
						.width( realWidth )
						.height( realHeight )
						.css(cssObj)
						.show();
					
					document.getElementById(newId).style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity='"+ opacity * 100 +"')";	
					document.getElementById(newId).style.filter += "progid:DXImageTransform.Microsoft.Blur(PixelRadius="+blur+", MakeShadow=false)";
					
					if(opt.winResize == true)
					{
						$(window).resize(function() {
							// Re-align shadow on window resize  
							var newRealWidth = $this.width() + parseInt($this.css('padding-left')) + parseInt($this.css('padding-right'));
							$('#'+newId).css({
								left: $this.offset().left + left - blur,
								top: $this.offset().top + top - blur, 
								width: newRealWidth
							});				
						});
						
					}
				} // nav internet explorer
			} // not 9
			
		});	// return 
		
		
		
    } // fn dropShadow
	
	

	
})(jQuery);
