/*
 * 
 * BUG : If there is margin-bottom set to X on the element then the shadow is X px too large on the bottom 
 * 
 */

function dropShadows(toShadow,startColor,endColor) {		
	this.shadowed         = new Array();
	this.numShadows       = 10;
	for(i=0;i<toShadow.length;i++)  this.shadowed[i] = this.getElement(toShadow[i]);		
	this.backgrounds = new Array();
	this.shadowSize  = 1;
	this.startColor = startColor;
	this.endColor   = endColor;

}

dropShadows.prototype = {
	
	getElement:function(id,scope) {

	    if (scope == null)
	        scope = "document";
	    else
	        scope = scope + ".document";
	
		return eval(scope).getElementById(id);
/*	
	    if (document.all) {
	        return eval(scope).all[id];
	    }
	    else if (document.getElementById) {
	        return eval(scope).getElementById(id);
	    }
*/
			
	},
	
	// from http://www.quirksmode.org/dom/getstyles.html
	getStyle:function (el,styleProp) {
		var x = this.getElement(el);
		if (x.currentStyle)			
			var y = x.currentStyle[styleProp];
		else if (window.getComputedStyle)
			var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
		return y;
	},
	
	
	// Color Fade taken and modified from here: http://www.elctech.com/snippets/javascript-color-fade-function-find-the-hex-value-between-two-hex-values
	colorFade:function(p) { 
		var c = ((this.startColor>>16)+((this.endColor>>16)-(this.startColor>>16))*p)<<16|(this.startColor>>8&0xFF)+((this.endColor>>8&0xFF)-(this.startColor>>8&0xFF))*p<<8|(this.startColor&0xFF)+((this.endColor&0xFF)-(this.startColor&0xFF))*p;
		var color = '#' + c.toString(16) + '';
		return  color;
	},

	
	
	buildShadows:function(numShadows,contentIndex,mFloat) {
		
		var newShadow = document.createElement('div');
		newShadow.style.position   = "relative";
		newShadow.style.cssFloat   = mFloat;
		newShadow.style.styleFloat = mFloat;
		newShadow.style.top  = (-1 * this.shadowSize) + 'px';			
		newShadow.style.left = (-1 * this.shadowSize) + 'px';
				
		// Base case : final shadow
		if (numShadows == 1) {
			var nowShadowing = this.shadowed[contentIndex].cloneNode(true);
			nowShadowing.style.marginLeft = '0px';
			newShadow.appendChild(nowShadowing);
			return newShadow;
		}
		
		// First call
		if (numShadows == this.numShadows) {
			
		}

		newShadow.style.backgroundColor = this.colorFade(numShadows/this.numShadows);
		newShadow.appendChild(this.buildShadows(numShadows-1,contentIndex,mFloat)); 
		return newShadow;
		
	},
	
	init:function() {
		
		// Step throgh shadowed and create shadow DIVs
		for(i=0;i<this.shadowed.length;i++)  {

			// Clone element to be shadowed to append later			
			// var nowShadowing = this.shadowed[i].cloneNode(true);
			var marginLeftName = (window.getComputedStyle ? 'margin-left' : 'marginLeft');
			var floatName      = (window.getComputedStyle ? 'float' : 'styleFloat');
			var mLeft  = this.getStyle(this.shadowed[i].id,marginLeftName);
			var mFloat = this.getStyle(this.shadowed[i].id,floatName);			
			
			// Get next HTML element (skip over text) sibling to aid in insertion later
			var nextSib = this.shadowed[i].nextSibling;
			while ((nextSib != null) && (nextSib.attributes == null))
				nextSib = nextSib.nextSibling;

			// Get parent and create new shadow div container
			var nsParent        = this.shadowed[i].parentNode;
			var shadowContainer = document.createElement('div');

			shadowContainer.style.width = this.shadowed[i].clientWidth + 'px';
			shadowContainer.className = 'shadowContainer';

			// recursively build the shadows
			shadowContainer.appendChild(this.buildShadows(this.numShadows,i,mFloat));

			// Position the shadow
			
			shadowContainer.style.position        = "relative";
			shadowContainer.style.top             = (this.shadowSize * this.numShadows) + 'px';
			shadowContainer.style.left            = (this.shadowSize * this.numShadows) + 'px';
			shadowContainer.style.marginBottom    = (this.shadowSize * this.numShadows) + 'px';
			shadowContainer.style.marginRight     = (this.shadowSize * this.numShadows) + 'px';
			
									 
			// Delete current div being shadowed
			nsParent.removeChild(this.shadowed[i]);
			if (mLeft != 'auto') // IE puts in value of 'auto' if not set, which can mess up layout.
				shadowContainer.style.marginLeft = mLeft;
			shadowContainer.style.cssFloat   = mFloat;
			shadowContainer.style.styleFloat = mFloat;
			
			// Insert new shadowed version. If this isn't the last child of it's parent, insert it, if it is, 
			// append it.
			if (nextSib != null)
				nsParent.insertBefore(shadowContainer,nextSib);
			else
				nsParent.appendChild(shadowContainer);			
				

		}//end creating shadows for toShadow divs		
		
		
	}
	
}