// Checks to see if menu is in URL and needs to remain open
function reOpenMenu(menuItem) {
	found = false;
	for (cnt in menuItem) {
        number_of_children = menuItem[cnt].subMenu.length;
	    if (number_of_children >0)
	    {
			for(i=0; i<number_of_children; i++)
			{
		   	 	if(menuItem[cnt].subMenu[i].mid == url4[0])
		    	{
					found = true;
					break;
			    }
			}
	    } 
	    if(found == true)
	    {
			 menuItem[cnt].expanded = true;
			 break;
	    }
	}
}

// Takes menu that user selected and expands or contracts it
function expandMenu(itemNum) {
	if (document.layers) {
		itemsPtr[itemNum].expanded= (!itemsPtr[itemNum].expanded);

		var cnt;
		for (cnt in itemsPtr) {
			if (itemNum==cnt) {
				itemsPtr[cnt].expanded= (!itemsPtr[cnt].expanded);
			} else {
				itemsPtr[cnt].expanded= false;
			}
		}
		writeMenu(menuBase.menuItems);
	}
	else {
		itemsPtr[itemNum].expanded= (!itemsPtr[itemNum].expanded);
		if (itemsPtr[itemNum].pmImg == "sixteenthnote") {
			itemsPtr[itemNum].pmImg = "quarternote";
		}
		else {itemsPtr[itemNum].pmImg = "sixteenthnote"}

		writeMenu(menuBase.menuItems);
	}
}

// Starts the script by activating writeMenu() function
// Calls reOpenMenu() to see if any menu is in URL and needs to be opened
function writeMenu3() {
	reOpenMenu(menuBase.menuItems);
	writeMenu();
}

// Calls writeMenu2() to produce the menu
function writeMenu() {

	var tmpIndent = "";
	code = "";
	var link = "";
	var opener = "";
	code = "<table border=\"0\" align=\"left\" width=\"167\">";
	writeMenu2(menuBase.menuItems, tmpIndent)
	drawMenu(code);
	code = "";
}

// Writes the menu
function writeMenu2(menuItem, tmpIndent) {

	var opener;
	var opener2;
	var link;

	for (cnt in menuItem) {
		if (menuItem[cnt].menuLink) {
			link = menuItem[cnt].menuLink;
			opener = "<td valign=\"top\" width=\"8\" align=\"left\" nowrap>";
			opener2 = "<IMG SRC=\"images/5x5.gif\" BORDER=\"0\" WIDTH=\"5\" HEIGHT=\"0\"></td>";
		}
		else {
			link = "javascript:expandMenu('" + itemNum + "')\" onMouseOver=\"window.status='Expand or contract this menu'; return true\" onMouseOut=\"window.status=''";
			opener = "<td valign=\"top\" width=\"8\" align=\"left\" nowrap><IMG SRC=\"images/"+ menuItem[cnt].pmImg +".gif\" BORDER=\"0\" width=\"8\">";
			opener2 = "<IMG SRC=\"images/5x5.gif\" BORDER=\"0\" WIDTH=\"5\" HEIGHT=\"0\"></td>";
		}
		code += "<tr>" + opener + tmpIndent + opener2 + "<td valign=\"top\" width=\"158\" align=\"left\"><A HREF=\"" + link + "\" CLASS=\"MenuText" + menuItem[cnt].weight + "\">" + menuItem[cnt].menuText + "</A></td></tr>";
		itemsPtr[itemNum]=menuItem[cnt];
		itemNum++;
		if ((menuItem[cnt].subMenu.length>0) && (menuItem[cnt].expanded==true)) {
			writeMenu2(menuItem[cnt].subMenu, indent+tmpIndent);
		}
		else {
		}
	}
}

// When window loads, cause writeMenu3() to start
window.onload=writeMenu3;

function drawMenu(full_code) {

	// writes the code to the different browsers
	
	//NETSCAPE4
	if (document.layers) {
		document.menu.document.write(full_code);
		//location.reload()
		document.menu.document.close();
	}
	//NETSCAPE6
	if (document.getElementById&&!document.all) {
		document.getElementById("menu").innerHTML = full_code;
	}
	//MSIE
	if (document.all) {
		menu.innerHTML = full_code;
	}
	
}
