/***************************
Author: Sherwin Sena Aborot
Date: 9/16/2008
Purpose:
	To furnish layout that can no longer/hard to trim using current css/html versions.
***************************/

function adjustHeight(target,source) {
	try {
		targetObj = document.getElementById(target);
		sourceObj = document.getElementById(source);

		if ( targetObj.offsetHeight < sourceObj.offsetHeight ) {
			targetObj.style.height = sourceObj.offsetHeight + "px";
		} else if ( targetObj.offsetHeight > sourceObj.offsetHeight ) {
			sourceObj.style.height = ( targetObj.offsetHeight - 5 ) + "px";
		}
	} catch(Exception) {
		alert("AdjustHeight: " + Exception);
	}
}

function adjustBodyHeight() {
	try {
		var pageBody = document.getElementsByTagName("body")[0];
		var navBody = document.getElementById("objNavigation");
		var contentBody = document.getElementById("objBody");
		var navHeight = navBody.offsetHeight;
		var contentHeight = contentBody.offsetHeight;
		
		// USE FILTER SO THAT ONLY STATIC PAGES ARE USING THIS
		if ( pageBody.id != "ProductSelectorPage" ) {
			if ( navHeight > contentHeight ) {
				contentBody.style.height = navHeight + "px";
			}
		}
	} catch(Exception) {
		alert("AdjustBodyHeight: " + Exception);
	}
}

function initMenuSystem() {
	try {
		// get all reference needed
		var nav = document.getElementById('nav');
		var navLIs = nav.getElementsByTagName('LI');

		// add onclick on items with subnav and no links, also remove + sign on items without subs
		for ( i=0;i<navLIs.length;i++ ) {
			var hasSubMenu = navLIs[i].getElementsByTagName('UL')[0] || "noSub";
			var hasNoLink = navLIs[i].getElementsByTagName('A')[0].href.indexOf('#') >= 0;
		
			if ( hasSubMenu != "noSub" ) {
				if ( hasNoLink ) {
					navLIs[i].getElementsByTagName('A')[0].href = "javascript:void(0);";
					navLIs[i].getElementsByTagName('A')[0].onclick = function() { resetMenu(); showSubNav(this.parentNode.parentNode); }
				}
			} else {
				navLIs[i].getElementsByTagName('A')[0].style.backgroundImage = "none";
			}
		}
		
		// show item linked on current page being viewed
		var bodyRef = document.getElementsByTagName('BODY')[0];
		var bodyClass = bodyRef.className;
		
		var activePageLinks = getElementsByClassName(nav,"li",bodyClass+'MenuItem');
		resetMenu();
		for ( i=0;i<activePageLinks.length;i++ ) {
			showSubNav(activePageLinks[i]);
		}
	} catch(Exception) {
		alert("InitMenuSystem: " + Exception);
	}
}

function showSubNav(parentLI) {
	try {
		// highlight active item
		parentLI.style.display = "block";
		parentLI.getElementsByTagName('A')[0].className = "litUpActiveItem";
		if ( parentLI.getElementsByTagName('DIV')[0].className.indexOf('litUpActiveItem') < 0 ) {
			parentLI.getElementsByTagName('DIV')[0].className += " litUpActiveItem";
			if ( parentLI.parentNode.className != "nav_class" ) {
				parentLI.parentNode.className += " litUpActiveItem";
			}
		}
		
		// show sub nav
		var activeParent = parentLI.getElementsByTagName('UL');
		if ( activeParent.length > 0 ) {
			activeParent[0].style.display = "block";
			if ( activeParent[0].className.indexOf('litUpActiveItem') < 0 ) {
				activeParent[0].className += " litUpActiveItem";
				var activeAnchors = activeParent[0].getElementsByTagName('A');
				for ( i=0;i<activeAnchors.length;i++ ) {
					if ( activeAnchors[i].parentNode.parentNode.parentNode.className.indexOf("litUpActiveItem") >= 0 ) {
						activeAnchors[i].style.display = "block";
					} else {
						activeAnchors[i].style.display = "none";
					}
				}
			}
		}
		
		//show parents
		while ( parentLI.parentNode.className != "nav_class" ) {
			parentLI.parentNode.style.display = "block";
			if ( parentLI.parentNode.nodeName == "LI" ) {
				parentLI.parentNode.getElementsByTagName('A')[0].className = "litUpItem";
			}
			parentLI = parentLI.parentNode;	
		}
	} catch(Exception) {
		alert("ShowSubNav: " + Exception);	
	}
}

function resetMenu() {
	try {
		var nav = document.getElementById('nav');
		var navULs = nav.getElementsByTagName('UL');
		var navAs = nav.getElementsByTagName('A');
		var navDIVs = nav.getElementsByTagName('DIV');
		
		// reset ANCHORS nav item status
		for ( i=0;i<navAs.length;i++ ) {
			navAs[i].className = "";
		}	

		// reset UL nav item status
		for ( i=0;i<navULs.length;i++ ) {
			var classOffset = navULs[i].className.indexOf('litUpActiveItem');
			var subAnchors = navULs[i].getElementsByTagName('A');
			if ( classOffset >= 0 ) {
				navULs[i].className = navULs[i].className.substring(0,classOffset);
			}
		}
		
		// reset DIV nav item status
		for ( i=0;i<navDIVs.length;i++ ) {
			var classOffset = navDIVs[i].className.indexOf('litUpActiveItem');
			if ( classOffset >= 0 ) {
				navDIVs[i].className = navDIVs[i].className.substring(0,classOffset);
			}
		}	
		
		// hid all subnavs
		for ( i=0;i<navULs.length;i++ ) {
			if ( navULs[i].className != "nav_class" ) {
				navULs[i].style.display = "none";
			}
		}
	} catch(Exception) {
		alert("ResetMenu: " + Exception);
	}
	return false;
}

/*
	Written by Jonathan Snook, http://www.snook.ca/jonathan
	Add-ons by Robert Nyman, http://www.robertnyman.com
*/
function getElementsByClassName(oElm, strTagName, strClassName) {
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}