function inString(haystack,needle) {
	if (haystack.indexOf(needle) + 1) { //'+1' because indexOf returns -1 if string is not found
		return true;
	} else {
		return false;
	}
}

function stretchFillerImage(imgObject) {
	imgObject.style.height=imgObject.parentNode.offsetHeight-imgObject.previousSibling.previousSibling.offsetHeight-imgObject.nextSibling.nextSibling.offsetHeight+'px';
}


function clearChildren(domNode) {

	if (domNode.hasChildNodes()) var throwaway_node = domNode.removeChild(domNode.childNodes[0]);
	if (domNode.hasChildNodes()) clearChildren(domNode);

}

function makeDropShadows(classToShadow) {
	
	var divs = document.getElementsByTagName('h1');
	for (i=0;i<divs.length;i++) {
		if (divs[i].className == classToShadow) {
		
			var originalContent = divs[i].childNodes;

			//div for drop shadow
			var tmpShadowDiv = document.createElement("div");
			tmpShadowDiv.className = classToShadow + 'Shadow';
			tmpShadowDiv.style.textAlign = divs[i].style.textAlign;
			for (j=0;j<originalContent.length;j++) tmpShadowDiv.appendChild(originalContent[j].cloneNode(true)); //appendChild removes the node from its source, so we use cloneNode to use a copy

			//div for type layer
			var tmpTypeDiv = document.createElement("div");
			tmpTypeDiv.className = classToShadow + 'ShadowedType';
			tmpTypeDiv.style.textAlign = divs[i].style.textAlign;
			for (j=0;j<originalContent.length;j++) tmpTypeDiv.appendChild(originalContent[j].cloneNode(true)); //appendChild removes the node from its source, so we don't have to clear divs[i] below

			divs[i].style.textAlign = 'left';
			clearChildren(divs[i]);
			divs[i].appendChild(tmpShadowDiv);
			divs[i].appendChild(tmpTypeDiv);

		}
	}
	
}

function reportTop(e) {
	alert(e.target.offsetTop+','+(Number(e.target.parentNode.offsetHeight)-Number(e.target.offsetHeight))+','+e.target.parentNode.offsetHeight);
}

function makePanelBoxes() {
	var divs = document.getElementsByTagName('div');
	for (var i=0;i<divs.length;i++) {
	
		if (inString(divs[i].className,'panelBox')) {
				
			if ( 
				(inString(navigator.userAgent.toLowerCase(),'msie')) && 
				(inString(navigator.userAgent.toLowerCase(),'windows')) && 
				!(inString(navigator.userAgent.toLowerCase(),'opera')) //Opera reports itself as MSIE/Windows, but also adds an 'Opera' string
				) {
				divs[i].style.visibility = 'hidden';
				
				//for problem where bottom padding ends up being the SUM of top AND bottom padding in IE/Win when there are two floats next to each other
				//it appears to be related to this bug: http://lukeplant.me.uk/articles/webdev/cssbugs/double_padding_on_cleared_divs_report.html				
				if (inString(divs[i].className,'split')) divs[i].style.paddingBottom='0'; 

			}

			//change CSS class for 'with JavaScript' modifications
			divs[i].className = divs[i].className.replace('panelBox','panelJSBox');
			
			//images container div
			var tmpPanelImageDiv = document.createElement("div");
			tmpPanelImageDiv.className='panelImages';
			
			if (inString(divs[i].className,'wide')) var imgTypeStr = 'wide';
			if (inString(divs[i].className,'split')) var imgTypeStr = 'split';
			if (inString(divs[i].className,'offset')) var imgTypeStr = 'splitOffset';

			//top image
			var tmpPanelImageTop = document.createElement("img");
			tmpPanelImageTop.src = imgTypeStr + 'BoxTop.png';
			tmpPanelImageTop.className = 'panelTop';

			//middle 'filler' image
			var tmpPanelImageMiddle = document.createElement("img");
			tmpPanelImageMiddle.src = imgTypeStr + 'BoxMiddle.png';
			tmpPanelImageMiddle.className = 'panelMiddle';
			
			//bottom image
			var tmpPanelImageBottom = document.createElement("img");
			tmpPanelImageBottom.src = imgTypeStr + 'BoxBottom.png';
			tmpPanelImageBottom.className = 'panelBottom';

			//insert container div
			imagesDiv = divs[i].insertBefore(tmpPanelImageDiv,divs[i].firstChild);
			
			//insert top and bottom images
			imagesDiv.insertBefore(tmpPanelImageBottom,imagesDiv.firstChild);
			imagesDiv.insertBefore(tmpPanelImageTop,imagesDiv.firstChild);
			
			//figure out the panel's minimum height, based on the combined heights of the top and bottom images (to keep those images from overlapping)
			var topAndBottomHeight = tmpPanelImageTop.offsetHeight + tmpPanelImageBottom.offsetHeight;			
			
			
			//min height div ("wedge") (for browsers that don't support the min-height CSS declaration)
			var tmpPanelMinHeightDiv = document.createElement("div");
			tmpPanelMinHeightDiv.className = 'wedge';
			//tmpPanelMinHeightDiv.style.height = topAndBottomHeight + 'px';
			tmpPanelMinHeightDiv.style.height = Number(topAndBottomHeight -12) + 'px';  // ******************* //
			tmpPanelMinHeightDiv.style.height = Number(topAndBottomHeight -28) + 'px';  // ******************* //
			divs[i].insertBefore(tmpPanelMinHeightDiv,divs[i].firstChild);
			
			
			//special handling for panels with 'panelMore' divs in them
			var subDivs = divs[i].getElementsByTagName('div');			
			var numOverMores = 0;
			var overMoreDiv = new Array();
			for (var j=0;j<subDivs.length;j++) {
				if (inString(subDivs[j].className,'panelMore')) {
				
					//panelMore overlay (inserted after the grass overlay)
					overMoreDiv[numOverMores] = subDivs[j].cloneNode(true);
					overMoreDiv[numOverMores].className = 'panelOverlayMore';
					overMoreDiv[numOverMores].style.width = Number(subDivs[j].offsetWidth - 37) + 'px'; //37 for padding
					if (isIE5Win) overMoreDiv[numOverMores].style.width = Number(subDivs[j].offsetWidth) + 'px'; //old box model
					if (inString(subDivs[j].parentNode.className,'left')) overMoreDiv[numOverMores].style.left = '14px';
					if (inString(subDivs[j].parentNode.className,'right')) overMoreDiv[numOverMores].style.right = '14px';
					numOverMores++;
				
					//min height div (other "wedge") (this one is dynamically sized, so min-height can't be used in place of this one)
					//forces 'panelMore' to the bottom without requiring absolute positioning (so it can still affect the flow)
					var tmpPanelMinHeightDiv2 = document.createElement("div");
					tmpPanelMinHeightDiv2.className = 'wedge';
					//tmpPanelMinHeightDiv2.style.height = Number(topAndBottomHeight-subDivs[j].offsetHeight -11 -28) + 'px';
					tmpPanelMinHeightDiv2.style.height = Number(divs[i].offsetHeight-subDivs[j].offsetHeight -11 -28 -2) + 'px'; //this seems problematic in IE5/Mac
					subDivs[j].parentNode.insertBefore(tmpPanelMinHeightDiv2,subDivs[j].parentNode.firstChild);
					
					j++; //increment by one to compensate for added div (avoid infinite loop)
					
				}
			}	
			
						
			//explicity set the panel's height in IE5/Mac, based on its actual height (for 'bottom' positioning)
			//  Ironically, setting the 'height' style to the object's actual height fixes the 
			//  bottom-positioning-inside-a-relatively-positioned-box-with-no-height problem.
			//  For a description of the problem, see: http://www.l-c-n.com/IE5tests/right_pos/bottom.shtml
			if ((inString(navigator.userAgent.toLowerCase(),'msie')) && (inString(navigator.userAgent.toLowerCase(),'mac'))) {
			
				//get rid of leaking from poorly inherited margins
				var subDivs = divs[i].getElementsByTagName('div');
				for (var j=0;j<subDivs.length;j++) {
					if (inString(subDivs[j].className,'panelContent')) {
					
						var tmpHeight = Number(subDivs[j].offsetHeight);
						
						if ((inString(divs[i].className,'split'))||(inString(divs[i].className,'offset'))) {
							//spacer div for floats in split panels
							var tmpPanelSpacerDiv = document.createElement("div");
							tmpPanelSpacerDiv.className = 'spacer';
							divs[i].appendChild(tmpPanelSpacerDiv);
						}
						
						if (tmpHeight < topAndBottomHeight) tmpHeight = topAndBottomHeight - 28; //padding
				
						//get rid of leaking from poorly inherited margins
						//also fixes grass position for panels with short content and no 'panelMore'
						if (tmpHeight < 0) tmpHeight = 0;
						subDivs[j].style.height = tmpHeight + 'px';
						subDivs[j].style.overflow = 'hidden';
						
					}
				}
			
				//spacer div for float re-alignment
				var tmpPanelSpacerDiv = document.createElement("div");
				tmpPanelSpacerDiv.className = 'spacer';
				divs[i].parentNode.insertBefore(tmpPanelSpacerDiv,node_after(divs[i]));	
				
			}	
			
						
			//set middle image height
			var tmpHeight = divs[i].offsetHeight - topAndBottomHeight;
			tmpHeight = tmpHeight - 1; //to accomodate panelJS's 1px bottom padding
			if (tmpHeight < 0) tmpHeight = 0;
			tmpPanelImageMiddle.style.height = tmpHeight + 'px';


			//insert middle image
			imagesDiv.insertBefore(tmpPanelImageMiddle,imagesDiv.firstChild.nextSibling);	
						
						
			if ((inString(navigator.userAgent.toLowerCase(),'msie')) && (inString(navigator.userAgent.toLowerCase(),'mac'))) {		
				var tmpHeight = Number(divs[i].offsetHeight -28);
				if (tmpHeight < 0) tmpHeight = 0;
				divs[i].style.height = tmpHeight + 'px';
			}
			

			//grass overlay
			var tmpPanelGrassOverlay = document.createElement("img");
			tmpPanelGrassOverlay.src = 'grassOverlayA.png';
			tmpPanelGrassOverlay.className="grassOverlay";
			tmpPanelGrassOverlay.style.zIndex = Number(last_child(divs[i]).style.zIndex) +1;
			divs[i].appendChild(tmpPanelGrassOverlay);
			
			
			//panelMore overlay
			for (var q=0;q<overMoreDiv.length;q++) {
				overMoreDiv[q].style.zIndex = Number(last_child(divs[i]).style.zIndex) +1;
				divs[i].appendChild(overMoreDiv[q]);
			}
					
		}
	}
	
}


function makeGrassFade() {
	var tmpFadeMiddleImg = document.createElement("img");
	tmpFadeMiddleImg.src = 'grassFade.png';
	document.getElementById('grassFade').style.zIndex='500'; //override effects of cascadeDivs()
	document.getElementById('grassFade').appendChild(tmpFadeMiddleImg);
}


function cascadeDivs() {
	divs = document.getElementsByTagName('div');
	var j = 1;
	for (var i=0;i<divs.length;i++) {
		divs[i].style.zIndex=j;
		j++;
	}
}


function getWindowHeight() {
	var y;
	if (self.innerHeight) { // all except IE
		y = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // IE6 strict mode
		y = document.documentElement.clientHeight;
	} else if (document.body) { // other IEs
		y = document.body.clientHeight;
	}
	return y;
}


function decoratePage() {

	//only run if not IE 5.0
	if (!(inString(navigator.userAgent.toLowerCase(),'msie 5.0'))) {	
		cascadeDivs();
		makePanelBoxes();		
		makeGrassFade();			
	}
		
	//only run if not IE5/Mac
	if (!((inString(navigator.userAgent.toLowerCase(),'msie')) && (inString(navigator.userAgent.toLowerCase(),'mac')))) {
		makeDropShadows('sectionHeader');
	}	
	
}
