/*
Expandable Listmenu Script
Author : Daniel Nolan
http://www.bleedingego.co.uk/webdev.php
*/

function initMenus() {
	
	// don't do anything if the getElementsByTagName method isn't available
	if (!document.getElementsByTagName) {
		return;
	}
	
	// get a collection of all the list item elements in DOM
	var aMenus = document.getElementsByTagName("LI");
	// for each list item in the DOM...
	for (var i = 0; i < aMenus.length; i++) {
		var mclass = aMenus[i].className;
		// if this list item has a class of treenode...
		if (mclass.indexOf("treenode") > -1) {
			// get all immediate children of the li.treenode
			var submenu = aMenus[i].childNodes;
			for (var j = 0; j < submenu.length; j++) {
				// for each anchor that's an immediate child of li.treenode...
				if (submenu[j].tagName == "A") {
					// set the onclick event to this function
					submenu[j].onclick = function() {
						var node = this.nextSibling;
						// infinite loop
						while (1) {
							// if the current node isn't null...
							if (node != null) {
								// if this is an unordered list element, switch the display
								if (node.tagName == "UL") {
									var d = (node.style.display == "none")
									node.style.display = (d) ? "block" : "none";
									this.className = (d) ? "treeopen" : "treeclosed";
									return false;
								}
								node = node.nextSibling;
							} else {
								// the current node is null so break out of the loop by returning false???
								return false;
							}
						}
						return false;
					}
					// toggle this menu item between treeopen and treeclosed
					submenu[j].className = (mclass.indexOf("open") > -1) ? "treeopen" : "treeclosed";
				}
				// if this is an unordred list element, toggle between display block and none
				if (submenu[j].tagName == "UL") {
					submenu[j].style.display = (mclass.indexOf("open") > -1) ? "block" : "none";
				}
			}
		}
	}
}

window.onload = initMenus;
