// Version: 1.5
// Date: 09/09/2008
// Last modified: Pete Raleigh

//nav_nab_default_udm_level3
function hasRealChildNodes(obj) {
	var result = false;
	for (var x = 0; x < obj.childNodes.length; x++) {
		child = obj.childNodes[x];
		if(child.tagName == "UL") {
			return hasRealChildNodes(child);
		} else {
			if (child.tagName == "LI") return true;
		}
	}
	return false;
}

var debug = false;

//nav_nab_default_udm_level3
function removeEmptyElements(obj) {
	// Search each child for children
	for (var x = 0; x < obj.childNodes.length; x++) {
		var child = obj.childNodes[x];
		if (hasRealChildNodes(child)) {
			// Found a LI item - skip
		} else {
			// Remove UL
			if ((child.tagName == "UL") && (!hasRealChildNodes(child))) {
				obj.removeChild(child);
			} else {
				removeEmptyElements(child);
			}			
		}
	}
}

//nav_nab_default_udm_level3
var navWCMParents = new Array();
// Traverse the level 2 items and if there are no level 3 items, change the class
function indicateParents(obj) {
	for (var x = 0; x < navWCMParents.length; x++) {
		var parId = navWCMParents[x];
		var parObj = document.getElementById(parId);
		var childId = "o" + parId.substring(1);
		var childObj = document.getElementById(childId);
		if (childObj) {
			var showIndicator = false;
			// Check if it contains an UL childtag
			for (var y = 0; y < childObj.childNodes.length; y++) {
				var grandChild = childObj.childNodes[y];
				if (grandChild.tagName == "UL") {
					showIndicator = true;
					var first = true;
					for (var z = 0; z < grandChild.childNodes.length; z++) {
						var lis = grandChild.childNodes[z];
						if (lis.tagName == "LI") {
							if (first) lis.className = "first";
							first = false;
						}
					}
				}				
			}
			if (!showIndicator && parObj) {
				if (parObj.className == "bold") parObj.className = "noImghigh";//
				else parObj.className = "noImg";
			}
		}
					
	}
}

//nav_nab_default_level3
function updateList(obj, maxdepth) {
	var parentCount = 0;
	var startlevel = -1;
	var parents = new Array();
	var sibparents = new Array();
	var child = obj.firstChild;
	while (child) {
		var anchor = child.firstChild;
		next = child.nextSibling;
		if (child.tagName == "LI") {
			var c = child.className;
			var highlighted = (c.substring(0,1) == "h")?true:false;
			// Update the anchor tag class - highlight by default.
			// We may need to change this to bold if a child element is the current site area.
			anchor.className = (highlighted)?"high":"";
			var d = c.substring(1)*1;
			if (startlevel == -1) {
				startlevel = d;
			} else {
				if (d > maxdepth) {
					obj.removeChild(child);
				} else {
					if (d > ld) {
						var last = parents[d-1];
						var newDiv = document.createElement("div");
						newDiv.id = "navLvl" + (ld);
						var newUl = document.createElement("ul");
						newUl.id = "ul-NavLvl" + (ld);
						// Expand the current branch
						newUl.className = "high";
						// Set the first child
						child.className = "first";
						newUl.appendChild(child);
						newDiv.appendChild(newUl);
						last.appendChild(newDiv);
						sibparents[d] = newUl;
						if (highlighted) {
							last.firstChild.className = "bold";
							anchor.className = "noImgHigh";
						}
					} else {
						// Sibling
						var sib = sibparents[d];
						if (sib) {
							// Sibling parent found so link to parent
							sib.appendChild(child);
							if (highlighted) {
								// Highlight the current item
								anchor.className = "noImghigh";
								// Update the top-level-parent's anchor tag (UL -> DIV -> LI)
								var parli = sib.parentNode.parentNode;
								anchor = parli.firstChild;
								if (anchor.tagName == "A") anchor.className = "bold";
							}
						}						
					}
				}
			}
			// Check for the ID of each of the level 2 items... store
			if (d == startlevel) navWCMParents[parentCount++] = anchor.id;
			// Update parent to indicate parent
			parents[d] = child;
			ld = d;
		}
		child = next;
	}
}
