/*
	Name: vsShadow
	Author: Rui Pereira
	URI: http://iRui.ac/

	This work is licensed under a Creative Commons License
	http://creativecommons.org/licenses/by/3.0/
*/


/*
	Version 1.0 - 2007/01/28
	Version 1.1 - 2007/03/28
	Version 1.2 - 2007/10/31
*/


// Search for all DIVs of class cast-shadow or cast-shadow-hidden and adds an extra shadow div
function vsCastShadow() {

	// Get all DIVs
	var allDIVs = document.getElementsByTagName("div");
	
	for (var i = 0; i < allDIVs.length; i++) {
		var isShadow = false;
		var shadowDIV = allDIVs[i];						
						
		// We are searching for DIVs with cast-shadow class, but that might not be the only one on the DIV
		var classTokens = shadowDIV.className.split(' ');
	    for (var j = 0; j < classTokens.length; j++) {
			var thisToken = classTokens[j];
			if ((thisToken == "cast-shadow") || (thisToken == "cast-shadow-hidden")) {
			isShadow = true;
	        if(thisToken == "cast-shadow-hidden") isHidden=true;
	      }
	    }
							
		// The "processed" attribute is set to prevent possible infinite nesting problems
	  	if (isShadow && (shadowDIV.getAttribute("processed") != "true")) {    	
	      

			// We create an absolute positioned element with the same size as the element that should cast the shadow
			// All other "pieces of shadow" will also be absolute positioned according to this one
			shadowDIV.setAttribute("processed", "true");
	      
			var newShadowDIV = document.createElement('div');
			newShadowDIV.className = 'vs-shadow';
	                        
			var top = document.createElement('div');
			top.className = "vs-top";
			newShadowDIV.appendChild(top);                  
		                        
			var topLeft = document.createElement('div');
			topLeft.className = "vs-topleft";
			newShadowDIV.appendChild(topLeft);

			var topRight = document.createElement('div');
			topRight.className = "vs-topright";
			newShadowDIV.appendChild(topRight);

			var bottom = document.createElement('div');
			bottom.className = "vs-bottom";
			newShadowDIV.appendChild(bottom);

			var bottomLeft = document.createElement('div');
			bottomLeft.className = "vs-bottomleft";
			newShadowDIV.appendChild(bottomLeft);

			var bottomRight = document.createElement('div');
			bottomRight.className = "vs-bottomright";
			newShadowDIV.appendChild(bottomRight);

			var left = document.createElement('div');
			left.className = "vs-left";
			newShadowDIV.appendChild(left);

			var leftTop = document.createElement('div');
			leftTop.className = "vs-lefttop";
			newShadowDIV.appendChild(leftTop);

			var leftBottom = document.createElement('div');
			leftBottom.className = "vs-leftbottom";
			newShadowDIV.appendChild(leftBottom);

			var right = document.createElement('div');
			right.className = "vs-right";
			newShadowDIV.appendChild(right);

			var rightTop = document.createElement('div');
			rightTop.className = "vs-righttop";
			newShadowDIV.appendChild(rightTop);

			var rightBottom = document.createElement('div');
			rightBottom.className = "vs-rightbottom";
			newShadowDIV.appendChild(rightBottom);

			// Start hidden
			if(isHidden) newShadowDIV.style.visibility = "hidden";

			// The shadow element is inserted as the first child of the element
			shadowDIV.insertBefore(newShadowDIV,shadowDIV.firstChild);


			// Two function are added to show and hide the shadow
			// The Shadow element must remain the first child for this to work
			shadowDIV.showShadow = function() {
				this.firstChild.style.visibility = "visible";
			}			
			shadowDIV.hideShadow = function() {
				this.firstChild.style.visibility = "hidden";
			}
		}		
	
		isShadow = false;
		isHidden = false;
	}  
}

// Search for all DIVs of class cast-shadow or cast-shadow-hidden and attaches extra functions to control the visibility of the shadow div
// The shadow div is assumed to be the first child div element
function attachShadowEventHandlers() {
	// Get all DIVs
	var allDIVs = document.getElementsByTagName("div");  

	for (var i = 0; i < allDIVs.length; i++) {

		var isShadow = false;
	    var shadowDIV = allDIVs[i];						
							
		// We are searching for DIVs with cast-shadow class, but that might not be the only one on the DIV
		var classTokens = shadowDIV.className.split(' ');
	    for (var j = 0; j < classTokens.length; j++) {
	      var thisToken = classTokens[j];
	      if ((thisToken == "cast-shadow") || (thisToken == "cast-shadow-hidden")) {
	        isShadow = true;
	      }
	    }
		
		if (isShadow) {    	
			// Two function are added to show and hide the shadow
			// The Shadow element must be the first DIV child for this to work			
			shadowDIV.showShadow = function() {
				this.getElementsByTagName("div")[0].style.visibility = "visible";
			}			
			shadowDIV.hideShadow = function() {			
				this.getElementsByTagName("div")[0].style.visibility = "hidden";
			}
		}
	}		
}


// Event Listener
// by Scott Andrew - http://scottandrew.com
// edited by Mark Wubben, <useCapture> is now set to false

function addEvent(obj, evType, fn){
	if(obj.addEventListener){
		obj.addEventListener(evType, fn, false); 
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent('on'+evType, fn);
		return r;
	} else {
		return false;
	}
}	

// Starting on vSlider 3.1, only the shadow events are added (the actual shadow rendering is part of the template itself, to improve performance)
addEvent(window, "load", attachShadowEventHandlers);