/*
 * Shadow
 * Created by Edgar Verle
 * Shadow is made for the jQuery library.
 * http://www.edgarverle.com/shadow
 */

/* 
 * @name shadow
 * @type jQuery
 * @param Map options Optional settings
 * @option Number cornerHeight The height in pixels of the corners of the shadow. Default: 8
 * @option Number width The width in pixels of the shadow. Default: 4
 * @option Number startOpacity This is the opacity at the closest point to the object. Default: 80
 * @option Number endOpacity The opacity at the furthest point in the shadow from the object. Default: 20
 * @option String color The color of the shadow. Default: black
 * @externalcss css aggiuntivo aggiunto al contenitore
 */
 
jQuery.fn.extend({
	shadow: function(options) {
		DropShadow(this, options);
	}
});

function DropShadow(element, options){
	var cornerHeight = 8;
	var width = 4;
	var startOpacity = 80;
	var endOpacity = 20;
	var color = "black";
	
	if(options){
		if(options["cornerHeight"] != null)
			cornerHeight = parseInt(options["cornerHeight"]);
		if(options["width"] != null)
			width = parseInt(options["width"]);
		if(options["startOpacity"] != null)
			startOpacity = parseFloat(options["startOpacity"]);
		if(options["endOpacity"] != null)
			endOpacity = parseFloat(options["endOpacity"]);
		if(options["color"] != null)
			color = options["color"];
			
		if(startOpacity < 1)
			startOpacity = Math.round(startOpacity*100);
		
		if(endOpacity < 1)
			endOpacity = Math.round(endOpacity * 100);
	}
	
	if($(element.id+"_shadow").length > 0)
		$(element.id+"_shadow").remove();
	
	element = $(element);
	
	var id = element[0].id + "_shadow";
	var html = "<div id='"+id+"' style='float:left;"+options["externalcss"]+";height:"+element.css("height")+";";
	
	if(element.css("position") == "absolute" || element.css("position") == "relative"){
		html += "position: " + element.css("position")+"; ";
		element.css("position", "");
	}
	
	if(element.css("top")){
		html += "top:"+element.css("top")+"; ";
		element.css("top", "");
	}

	if(element.css("left")){
		html += "left:"+element.css("left")+"; ";
		element.css("left", "");		
	}
	
	html += "'><table style='float:left;border-collapse:collapse;' cellpadding='0' cellspacing='0'><tr class='shadowFirstRow'>"+
		"<td style='padding:0px;' colspan='2' rowspan='2' align='left' valign='top'></td></tr></table></div>";
	
	element.wrap(html);
		
	html = "<td align='left' valign='top' colspan='"+width+"' style='border-collapse:collapse;width:"+width+"px; height:"+cornerHeight+"px;'><table style='border-collapse:collapse;' cellpadding='0' cellspacing='0'>";
	
	var opacity = startOpacity;
	
	for(var i = 0; i < cornerHeight; i++){
		html += "<tr>";
		
		for(var j = 0; j < width; j++){
			opacity = Math.round((i - j * (cornerHeight / width)) * 10);
			
			if(opacity < 0)
				opacity = 0;
			else if(opacity < 10)
				opacity = "0"+opacity;
				
			html += "<td align='left' valign='top' style='height:1px; width:1px; background-color: "+color+"; opacity:0."+opacity+"; filter:Alpha(opacity="+opacity+");'></td>"
		}
		
		html += "</tr>";
	}
	
	html += "</table></td>";
	
	$("#"+id).children().children().children(".shadowFirstRow").append(html);
	
	html = "<tr>";
	
	for(var i = startOpacity; i >= endOpacity; i -= (startOpacity - endOpacity) / (width - 1)){
		opacity = Math.round(i);
		
		if(opacity < 10)
			opacity = "0" + opacity;
			
		html += "<td align='left' valign='top' style='background-color:"+color+"; padding:0px; margin:0px; height:"+(element.height()- cornerHeight)+"px; opacity:0."+opacity+"; filter:Alpha(opacity="+opacity+");'></td>";
	}
	
	html += "</tr>";
	
	html += "<tr><td align='left' valign='top' rowspan='"+width+"' style='width:"+cornerHeight+"px; height:"+width+"px;'><table style='border-collapse:collapse;' cellpadding='0' cellspacing='0'>";
	for(var i = 0; i < width; i++){
		html += "<tr>";
		
		for(var j = 0; j < cornerHeight; j++){
			opacity = Math.round((0 - i * (cornerHeight / width) + j)*10);
			
			if(opacity < 0)
				opacity = 0;
			else if(opacity < 10)
				opacity = "0"+opacity;
				
			html += "<td align='left' valign='top' style='width:1px; height:1px; background-color:"+color+"; opacity:0."+opacity+"; filter:Alpha(opacity="+opacity+");'></td>";
		}
		
		html += "</tr>";
	}
	
	html += "</table></td>";
	
	var temp = element.width() ;
	
	if($.browser.msie)
		temp -= cornerHeight ;
	
	for(var i = startOpacity; i >= endOpacity; i -= (startOpacity - endOpacity) / (width - 1)){
		if(i != startOpacity)
			html += "<tr>";
		
		opacity = Math.round(i);
		
		if(opacity < 10)
			opacity = "0"+opacity;
			
		html += "<td align='left' valign='top' style='height:1px; width:"+temp+"px; background-color:"+color+"; opacity:0."+opacity+"; filter:Alpha(opacity="+opacity+");'></td>";
					
		for(var j = startOpacity; j >= endOpacity; j -= (startOpacity - endOpacity) / (width - 1)){
			opacity = Math.round(j);
			
			if(opacity > i)
				opacity = Math.round(i);
			
			
			if(opacity < 10)
				opacity = "0"+opacity;
				
			html += "<td align='left' valign='top' style='width:1px; height:1px; background-color:"+color+"; opacity:0."+opacity+"; filter:Alpha(opacity="+opacity+");'></td>";
		}
		
		html += "</tr>";
	}
	
	$("#"+id).children("table").append(html);
}
