// Author: Ryan Tovar
// Date Created: 09.09.2009

function FindRootNode(xmlFileURL)	{
	try	{
		// This is for finding the root node in Internet Explorer.
		var xmlDocument = new ActiveXObject("Microsoft.XMLDOM");
		xmlDocument.async = false;
		xmlDocument.load(xmlFileURL);
		childNodeBaseIndex = 0; // According to documentation found on the interwebs, childNodes array index starts from 0 in this browser.
		return xmlDocument.documentElement;
	}
	catch(e)	{
		try	{
		// This is for finding the root node in Mozilla-based browsers such as Firefox.
		var xmlDocument = document.implementation.createDocument("","",null);
		xmlDocument.async = false;
		xmlDocument.load(xmlFileURL);
		childNodeBaseIndex = 1; // According to documentation found on the interwebs, childNodes array index starts from 1 in this browser.
		return xmlDocument.documentElement;
		}
		catch(e)	{
			// This is for finding the root node in Safari-based browsers, such as Chrome (and of course, Safari).
			var xmlDocument = new window.XMLHttpRequest();
			xmlDocument.open("GET",xmlFileURL,false);
			xmlDocument.send(null);
			childNodeBaseIndex = 1; // According to documentation found on the interwebs, childNodes array index starts from 1 in this browser.
			return xmlDocument.responseXML.getElementsByTagName("items")[0];
		}
	}
}

function MenuItem(itemName,itemURL,parentIndex,newWindow,itemLevel,itemDivClass,hasChildren)	{
	this.itemName = itemName; // Text that should appear in the menu as a clickable link.
	this.itemURL = itemURL; // URL that the link should go to.
	this.parentIndex = parentIndex; // Index number of the parent node in the menu array.
	this.newWindow = newWindow; // If set to true, opens a new window.
	this.itemLevel = itemLevel; // Used for determining indention when printing left menu or sitemap.
	this.itemDivClass = itemDivClass; // Holds class information; will be set to InactiveNavItem (default), ActiveNavItem or ActiveTopLevelNavItem.
	this.hasChildren = hasChildren; // Used for determining the presence of the arrow when printing left menu.
	return this;
}

function PrintLeftMenu(targetMenuItemURL)	{
	var targetMenuItemIndex = FindMenuItemIndex(targetMenuItemURL);
	if (targetMenuItemIndex >= 0)	{
		SetRelatedItemsToActive(targetMenuItemIndex); // Set class for all items related to target item (both parents and children) to ActiveNavItem.
		HighlightTargetItem(targetMenuItemIndex);
	}
	SetTopLevelItemsToActive(); // Set class for all top level items to ActiveTopLevelNavItem.
	document.write("<div id='LeftNavWrapper' style='width:185px;'>");
	for (var i=0;i<menuStack.length;i++)	{
		if (menuStack[i].hasChildren)	{
			document.write("<div id='lcramenu" + i + "' class='" + menuStack[i].itemDivClass + "'>");
			document.write("<div class='MenuLevel_" + menuStack[i].itemLevel + "'><img src='/library/media/public/images/homepage09/menu_arrow.png' border='0'>&nbsp;</div>");
			if (menuStack[i].newWindow == "true")	{
				document.write("<div class='MenuText'><a href='" + menuStack[i].itemURL + "' target='_blank'>" + menuStack[i].itemName + "</a></div>");
			}
			else	{
				document.write("<div class='MenuText'><a href='" + menuStack[i].itemURL + "'>" + menuStack[i].itemName + "</a></div>");
			}
			document.write("</div>");
		}
		else	{
			document.write("<div id='lcramenu" + i + "' class='" + menuStack[i].itemDivClass + "'>");
			document.write("<div class='MenuLevel_" + menuStack[i].itemLevel + "'><img src='/library/media/public/images/homepage09/menu_space.png' border='0'>&nbsp;</div>");
			if (menuStack[i].newWindow == "true")	{
				document.write("<div class='MenuText'><a href='" + menuStack[i].itemURL + "' target='_blank'>" + menuStack[i].itemName + "</a></div>");
			}
			else	{
				document.write("<div class='MenuText'><a href='" + menuStack[i].itemURL + "'>" + menuStack[i].itemName + "</a></div>");
			}
			document.write("</div>");
		}
	}
	document.write("</div>");
}

function PrintSiteMap()	{
	SetAllItemsToActive(); // Set class for ALL items to ActiveNavItem.
	SetTopLevelSitemapItemsToActive(); // Set class for all top level items to ActiveTopLevelSitemapItem.
	document.write("<div id='SitemapWrapper'>");
	for (var i=0;i<menuStack.length;i++)	{
		if ((menuStack[i].hasChildren) && (menuStack[i].itemLevel == 0))	{
			// Since the top level items already have their own class applied to them to make them stand out, don't include the arrow image (use the spacer instead).
			document.write("<div class='" + menuStack[i].itemDivClass + "'>");
			document.write("<div class='MenuLevel_" + menuStack[i].itemLevel + "'><img src='/library/media/public/images/homepage09/menu_space.png' border='0'></div>");
			if (menuStack[i].newWindow == "true")	{
				document.write("<div class='MenuText'><a href='" + menuStack[i].itemURL + "' target='_blank'>" + menuStack[i].itemName + "</a></div>");
			}
			else	{
				document.write("<div class='MenuText'><a href='" + menuStack[i].itemURL + "'>" + menuStack[i].itemName + "</a></div>");
			}
			document.write("</div>");
		}
		else if ((menuStack[i].hasChildren) && (menuStack[i].itemLevel != 0))	{
			// Menu item has children, so include the arrow image.
			document.write("<div class='" + menuStack[i].itemDivClass + "'>");
			document.write("<div class='MenuLevel_" + menuStack[i].itemLevel + "'><img src='/library/media/public/images/homepage09/menu_arrow.png' border='0'></div>");
			if (menuStack[i].newWindow == "true")	{
				document.write("<div class='MenuText'><a href='" + menuStack[i].itemURL + "' target='_blank'>" + menuStack[i].itemName + "</a></div>");
			}
			else	{
				document.write("<div class='MenuText'><a href='" + menuStack[i].itemURL + "'>" + menuStack[i].itemName + "</a></div>");
			}
			document.write("</div>");
		}
		else	{
			// Menu item has no children, so don't include the arrow image (use the spacer instead).
			document.write("<div class='" + menuStack[i].itemDivClass + "'>");
			document.write("<div class='MenuLevel_" + menuStack[i].itemLevel + "'><img src='/library/media/public/images/homepage09/menu_space.png' border='0'></div>");
			if (menuStack[i].newWindow == "true")	{
				document.write("<div class='MenuText'><a href='" + menuStack[i].itemURL + "' target='_blank'>" + menuStack[i].itemName + "</a></div>");
			}
			else	{
				document.write("<div class='MenuText'><a href='" + menuStack[i].itemURL + "'>" + menuStack[i].itemName + "</a></div>");
			}
			document.write("</div>");
		}
	}
	document.write("</div>");
}

function PrintSiteMapCategory(categoryURL)	{
	SetAllItemsToActive(); // Set class for ALL items to ActiveNavItem.
	SetTopLevelSitemapItemsToActive(); // Set class for all top level items to ActiveTopLevelSitemapItem.
	startIndex = FindMenuItemIndex(categoryURL);
	document.write("<div id='SitemapWrapper'>");
	for (var i=startIndex;i<menuStack.length;i++)	{
		if ((menuStack[i].hasChildren) && (menuStack[i].itemLevel == 0))	{
			// Since the top level items already have their own class applied to them to make them stand out, don't include the arrow image (use the spacer instead).
			document.write("<div class='" + menuStack[i].itemDivClass + "'>");
			document.write("<div class='MenuLevel_" + menuStack[i].itemLevel + "'><img src='/library/media/public/images/homepage09/menu_space.png' border='0'></div>");
			if (menuStack[i].newWindow == "true")	{
				document.write("<div class='MenuText'><a href='" + menuStack[i].itemURL + "' target='_blank'>" + menuStack[i].itemName + "</a></div>");
			}
			else	{
				document.write("<div class='MenuText'><a href='" + menuStack[i].itemURL + "'>" + menuStack[i].itemName + "</a></div>");
			}
			document.write("</div>\n");
		}
		else if ((menuStack[i].hasChildren) && (menuStack[i].itemLevel != 0))	{
			// Menu item has children, so include the arrow image.
			document.write("<div class='" + menuStack[i].itemDivClass + "'>");
			document.write("<div class='MenuLevel_" + menuStack[i].itemLevel + "'><img src='/library/media/public/images/homepage09/menu_arrow.png' border='0'></div>");
			if (menuStack[i].newWindow == "true")	{
				document.write("<div class='MenuText'><a href='" + menuStack[i].itemURL + "' target='_blank'><strong>" + menuStack[i].itemName + "</strong></a></div>");
			}
			else	{
				document.write("<div class='MenuText'><a href='" + menuStack[i].itemURL + "'><strong>" + menuStack[i].itemName + "</strong></a></div>");
			}
			document.write("</div>\n");
		}
		else	{
			// Menu item has no children, so don't include the arrow image (use the spacer instead).
			document.write("<div class='" + menuStack[i].itemDivClass + "'>");
			document.write("<div class='MenuLevel_" + menuStack[i].itemLevel + "'><img src='/library/media/public/images/homepage09/menu_space.png' border='0'></div>");
			if (menuStack[i].newWindow == "true")	{
				document.write("<div class='MenuText'><a href='" + menuStack[i].itemURL + "' target='_blank'>" + menuStack[i].itemName + "</a></div>");
			}
			else	{
				document.write("<div class='MenuText'><a href='" + menuStack[i].itemURL + "'>" + menuStack[i].itemName + "</a></div>");
			}
			document.write("</div>\n");
		}
		if ((i+1) == menuStack.length)	{
			break;
		}
		if (menuStack[i+1].itemLevel == 0)	{
			break;
		}
	}
	document.write("</div>\n");
}

function PrintMenuStack()	{
	for (var i=0; i<menuStack.length; i++)	{
		document.write("[" + i + "] " + menuStack[i].itemName + "<br>");
	}
}

function PopulateMenuStack(xmlNode)	{
	if (xmlNode.nodeType == 1)	{
		// The XML file may inlcude nodes that are commented out, so we need to make sure to exclude those while parsing the XML file. FYI, 1 = ELEMENT_NODE and 8 = COMMENT_NODE. 
		if (xmlNode.hasChildNodes() == false)	{
			// This is the base case. Since the current node has no children, we just push it onto the stack. By default we set the div's display class to InactiveNavItem.
			menuStack.push(new MenuItem(xmlNode.getAttribute("title"),xmlNode.getAttribute("url"),parentStack[parentStack.length-1],xmlNode.getAttribute("openInNewWindow"),currentLevel,"InactiveNavItem",false));
		}
		else	{
			if (xmlNode.tagName == "items")	{
				// The root node is a special case. We don't want the root node to show up in the menu, but we do need to account for it. Push "root" onto the parentStack and call the function
				// recursively on the children without pushing it onto the menuStack or incrementing the level counter.
				parentStack.push("root");
				for (var i=childNodeBaseIndex; i<xmlNode.childNodes.length; i++)	{
					PopulateMenuStack(xmlNode.childNodes[i]);
				}
			}
			else	{
				// This is the recursive part of the function. Push the current node onto the stack, push the index number of this node onto the parentStack, increment the level counter
				// and recursively call the function on all children of the current node. By default we set the div's display class to InactiveNavItem.
				menuStack.push(new MenuItem(xmlNode.getAttribute("title"),xmlNode.getAttribute("url"),parentStack[parentStack.length-1],xmlNode.getAttribute("openInNewWindow"),currentLevel,"InactiveNavItem",true));
				parentStack.push(menuStack.length-1);
				currentLevel++;
				for (var i=childNodeBaseIndex; i<xmlNode.childNodes.length; i++)	{
					PopulateMenuStack(xmlNode.childNodes[i]);
				}
				parentStack.pop(); // Since we are done with this node's children, we can remove this node's index number from the parentStack.
				currentLevel--;
			}
		}
	}
}

function SetRelatedItemsToActive(menuItemIndex)	{
	if (menuItemIndex == -1)	{
		// Index will be -1 if the URL of the current page is not in the menuStack array. In this case, we only want the top level menu items to display.
		menuItemIndex = 0;
	}
	for (var i=0;i<menuStack.length;i++)	{
		// Activate any menu items at the same level as the current item (parentIndex is the same). We also want to activate any children of the current item.
		if ((menuStack[i].parentIndex == menuStack[menuItemIndex].parentIndex) || (menuStack[i].parentIndex == menuItemIndex))	{
			menuStack[i].itemDivClass = "ActiveNavItem";
		}
	}
	if (menuStack[menuItemIndex].parentIndex != "root")	{
		// If this is not one of the top-level items (i.e. the parent is not the root node), then recursively call the function on the index of the current node's parent.
		SetRelatedItemsToActive(menuStack[menuItemIndex].parentIndex);
	}
}

function SetAllItemsToActive()	{
	for (var i=0;i<menuStack.length;i++)	{
		menuStack[i].itemDivClass = "ActiveNavItem";
	}
}

function HighlightTargetItem(menuItemIndex)	{
	menuStack[menuItemIndex].itemDivClass = menuStack[menuItemIndex].itemDivClass + " HighlightedMenuItem";
}

function SetTopLevelItemsToActive()	{
	for (var i=0;i<menuStack.length;i++)	{
		if (menuStack[i].itemLevel == 0)	{
			menuStack[i].itemDivClass = "ActiveTopLevelNavItem";
		}
	}
}

function SetTopLevelSitemapItemsToActive()	{
	for (var i=0;i<menuStack.length;i++)	{
		if (menuStack[i].itemLevel == 0)	{
			menuStack[i].itemDivClass = "ActiveTopLevelSitemapItem";
		}
	}
}

function FindMenuItemIndex(menuItemURL)	{
	for (var i=0;i<menuStack.length;i++)	{
		if (menuStack[i].itemURL == menuItemURL)	{
			return i; // If the URL parameter passed to this function matches an item in the menuStack, then return the index number of that item.
		}
	}
	return -1; // Otherwise, return -1.
}

var currentLevel = 0;
var menuStack = [];
var parentStack = [];
var childNodeBaseIndex;
oRootNode = FindRootNode("/library/js/menu/WWWMenuAllSite09.xml");
PopulateMenuStack(oRootNode);