/*	Loads Scripts (replaces windows.onload, allows script to safely load sooner)	*/
//var foobar = new domFunction(
//	function () {
//
///*	Initiate Drop Down Menu Script	*/
//		subMenu ("nav-main");
//
//		return true
//	},
//	{'nav-main' : 'id'}
//	
//);

/*	Set Global "Active Menu" Holder	*/
var subMenuActive = null;

/*	Function to build drop down menues.
		classValue = the class assigned to the menu(es) that use wishes to build into drop downs.
		Currently only supports menues constructed out of nested ULs.	*/
function subMenu (idValue) {
	var rootElement = document.getElementById(idValue);

/*	Sets flag class of "dropDownsEnabled" to root menu element (root UL), and creates a node list of nested ULs.	*/
	rootElement.className += " dropDownsEnabled";
	var	subMenuElements = findSubMenus(rootElement);

	return true;

}

/*	Checks to see if the testElement has the requested classValue. Returns true/false of the class match.	*/
function classSearch(testElement, classValue) {
	var classMatch = false;
	
	if (testElement.className) {	
		var classes = testElement.className.split(' ');
		for (classSearchNo = 0; classSearchNo < classes.length; classSearchNo++) {
			if (classes[classSearchNo] == classValue) {
				classMatch = true;
			}
		}
	}
	
	return classMatch;
}

/*	Searches for nested elements of the same nodeName as "testElement". Looks two levels down (so UL - LI - UL, ignoring the LI).
		Then applies Drop Down Menu behaviors to matches via dropDownsSet.	*/
function findSubMenus (testElement) {
	var nextLevel = Array();
	var nextLevelCount = 0;

	for (i=0; i<testElement.childNodes.length; i++) {	//Runs through testElement's children
		var childElement = testElement.childNodes[i];	//Sets the childNode to its own variable
		for (j=0; j<childElement.childNodes.length; j++) {	//Runs through the childElement's children
			var grandChildElement = childElement.childNodes[j];
			if (childElement.childNodes[j].nodeName == testElement.nodeName) {	//Checks to see if the (grandchild) is the same type of node as testElement
				dropDownsSet(childElement);
				nextLevel[nextLevelCount] = childElement.childNodes[j];
				nextLevelCount ++;
			}
		}
	}
}

/*	Applies Drop Down Menu behaviors to the element.	*/
function dropDownsSet(styleElement) {
	var clickElement

/*	Searches for the element that will be clicked.
		This takes into account possibility that the styleElement may also be the clickElement (feature in XHTML 2+)	*/
	if (styleElement.getAttribute("href")){
		clickElement = styleElement;
	} else {
		for(k=0; k < styleElement.childNodes.length; k++) {
			var styleElementChild = styleElement.childNodes[k];
			if(styleElementChild.nodeType == 1 && styleElementChild.getAttribute("href")) {
				clickElement = styleElementChild
			}
		}
	}

/*	Checks to see if current element points to section of site user is currently using, and applies flag class if appropriate.	*/

//	checkCurrentSection (clickElement, styleElement);
/*
	alert(clickElement.getAttribute("href"));
*/									
/*	Applies Drop Down Menu behaviors	*/
	clickElement.onclick = function(){
		return (dropDownsClick(styleElement));
	}
}

/*	Checks linkElement to current page's URL, and indicates if the link is connected to current site section.	*/
function checkCurrentSection (linkElement, styleElement) {
/*	Splits href value of linkElement and current pages location.pathname, respectively	*/
	var linkDirectories = linkElement.getAttribute("href").split('/');
	var pathDirectories = window.location.pathname.split('/');
/*	Checks for match in the first directory level of both paths.
	If there's a match, applies flag classes of currentSection, and subMenuOn, and sets subMenuActive = styleElement.	
	if(((linkDirectories[0] = "http:") && (linkDirectories[3] == pathDirectories[1])) || ((linkDirectories[1] != "http:") && (linkDirectories[1] == pathDirectories[1]))) {
		styleElement.className += " currentSection subMenuOn";
		subMenuActive = styleElement;
	}
*/	
	var firstDir = 0
	var currentSection = 1
	
	if(linkDirectories[0] == "http:") {firstDir = 3;}

	for (l=0; ((l + firstDir) < linkDirectories.length) && (l < (pathDirectories.length - 1)); l++) {
		if (linkDirectories[firstDir] != pathDirectories [(l + firstDir)]) {currentSection = 0}
	}
	
	return true;
}

/*	Applies Drop Down Menu Behaviors	*/
function dropDownsClick(styleElement) {
/*	If styleElement is already current active Menu (class="subMenuOn"), the link works as normal.	*/
	if (classSearch(styleElement, "subMenuOn")){
		return true;
	} else {
/*	If styleElement is not current active Menu, the current active menu is deactivated (flag class of subMenuOn removed), the new active menu is activated (flag class of subMenuOn is assigned), and subMenuActive is updated to reflect changes. Finally, the link is suppressed.	*/
		if(!(subMenuActive == null)) {
			subMenuActive.className = subMenuActive.className.replace(/subMenuOn/g," ");
		}
		styleElement.className += " subMenuOn";
		subMenuActive = styleElement;
		return false;
	}
}