window.onload = init;

/**
 * Initializes various components of the site
 */
function init()
{
	// set up the navbar
	initializeNavbar();
} // end function init()

/**
 * Makes all of the navigation sections collapsable
 */
function initializeNavbar()
{
	// get the node for the navbar (content only, not info)
	var nav_list       = $("#nav-content").get(0);
	var nav_list_items = getChildrenByTagName(nav_list, "li");
	
	// loop through all the parts of the navbar
	// and set up the onClick event for the nested lists
	var menu_header;
	var header_text;
	var new_node;
	for (var i in nav_list_items)
	{
		// get the menu header
		menu_header = nav_list_items[i].firstChild;
		
		// if the node is not a text node, skip over it
		// this is for menus headers that are links and don't have any child links
		if (Node.TEXT_NODE != menu_header.nodeType)
		{
			continue;
		}
		
		// get the title for the menu header
		header_text = menu_header.nodeValue;
		
		// create the new node for the menu header
		new_node              = document.createElement("a");
		new_node.textContent  = header_text;
		new_node.style.cursor = "n-resize";
		new_node.addEventListener("click", toggleMenu, false);
		
		// replace the old menu header with the new menu header
		nav_list_items[i].replaceChild(new_node, menu_header);
	} // end loop through the menu headers
} // end function initializeNavbar()

/**
 * Opens/closes a menu
 */
function toggleMenu()
{
	// get the list item for the menu
	var menu = this.parentNode;
	
	// check if the menu is currently hidden
	if (menu.getElementsByTagName("ul")[0].style.display == "none")
	{
		// show the menu and change the cursor
		menu.getElementsByTagName("ul")[0].style.display = "block";
		this.style.cursor = "n-resize";
	} // end if the menu is currently hidden
	// the menu is currently visible
	else
	{
		// hide the menu and change the cursor
		menu.getElementsByTagName("ul")[0].style.display = "none";
		this.style.cursor = "s-resize";
	} // end if the menu is currently visible
} // end function toggleMenu()