﻿/* applies a class to the first menu item in the object given by the menu id, the parent to the UL */
function differentiateHomeLink(menuId){
	var menuItem = document.getElementById(menuId);
	if (menuId != null){
		var listObjects = menuItem.getElementsByTagName("UL");
		var allChildren = listObjects[0].childNodes; // in the case of multiple ULs, this is only done for the first one
		for(var i = 0; i < allChildren.length; i++){
			if (allChildren[i].tagName == "LI"){ // if we found the first list item, set the class and then stop
				allChildren[i].className="home_link";
				break;
			}
		}
	}
}

function differentiateLastLink(menuId){
	var menuItem = document.getElementById(menuId);
	if (menuId != null){
		var listObjects = menuItem.getElementsByTagName("UL");
		var allChildren = listObjects[0].childNodes; // in the case of multiple ULs, this is only done for the first one
		allChildren[allChildren.length-1].className="last_link";
	}
}

function setSubmenuPositions() {
	/* get all top menus LIs*/
	var allTopMenus = document.getElementById("mainlevelmainnav").childNodes;
	/*  for each LI item, find its children. set their position. */
	/*  repeat the above until no more children are found. */
	for(var i = 0; i<allTopMenus.length; i++){
		var item = allTopMenus[i];
		var itemWrapper = item.lastChild;
		var heightExemplifier = itemWrapper.lastChild;
		/* if a dropdown menu falls down, the last child would be a UL tag */
		if (itemWrapper.tagName == "UL"){
			/* all the ULS that are children of this dropdown menu need to have their positions corrected */
			var allULs = itemWrapper.getElementsByTagName("UL");
			for(var j = 0; j < allULs.length; j++){
				var correctPos = j*heightExemplifier.offsetHeight;
				allULs[j].style.top = ""+correctPos+"px";
			}
		}
	}
}

function insertPaddersForMenus(){
	var mainMenuElement = document.getElementById('mainlevelmainnav');
	var allLiItems = mainMenuElement.childNodes;
	for(var i = 0; i < allLiItems.length; i++){
		// if last child is UL, correct that UL
		var possibleUL = allLiItems[i].lastChild;
		if(possibleUL.tagName == 'UL'){
			var oldHTML = possibleUL.innerHTML;
			var newHTML = '<li class="menu_top_padder">&nbsp;</li>'+oldHTML+'<li class="menu_bottom_padder">&nbsp;</li>';
			possibleUL.innerHTML = newHTML;
		}
		var childULs = possibleUL.getElementsByTagName("UL");
		for(var j = 0; j < childULs.length; j++){
			var childUL = childULs[j];
			var oldHTML = childUL.innerHTML;
			var newHTML = '<li class="submenu_top_padder">&nbsp;</li>'+oldHTML+'<li class="menu_bottom_padder">&nbsp;</li>';
			childUL.innerHTML = newHTML;
		}
	}
}

function correctMainMenuPositions(){
	var allTopMenus = document.getElementById("mainlevelmainnav").childNodes;
	for(var i=0; i < allTopMenus.length; i++){
		var item = allTopMenus[i];
		
	}
}
