function switchSubmenu(menuId, subId) {
	if ( ! document.getElementById ) return;
	var id = menuId + "_" + subId;
	var submenuBody = document.getElementById(id + "Body");
	var menuState   = document.getElementById(menuId + "State");

	if ( ! submenuBody.style ) return;
	if ( submenuBody.style.visibility == "hidden" ) {
		submenuBody.style.position    = "static";
		submenuBody.style.visibility  = "visible";
		submenuBody.removeAttribute('style');

		if ( ! menuState ) return;
		var thisState;
		thisState = document.createElement("INPUT");
		thisState.type  = "hidden";
		thisState.name  = menuId + "[" + subId.replace(/_/, "][") + "][state]";
		thisState.value = "open";
		thisState.id    = id + "State";
		menuState.appendChild(thisState);
	} else {
		submenuBody.style.position   = "absolute";
		submenuBody.style.visibility = "hidden";

		if ( ! menuState ) return;
		thisState = document.getElementById(id + "State");
		if ( thisState ) menuState.removeChild(thisState);
	}
}

function initSubmenu(menuId, subId) {
	if ( ! document.getElementById ) return;
	var submenuBody = document.getElementById(menuId + "_" + subId + "Body");

	if ( ! submenuBody ) return;
	var i = 0;
	var entry;
	var entryId;
	var entryState;
	for ( entry = submenuBody.firstChild; entry; entry = entry.nextSibling ) {
		switch ( entry.className ) {
			case "submenu":
				entryId = subId + "_" + i
				entryState = document.getElementById(menuId + "_" + entryId + "State");
				if ( entryState && (entryState.value == "open") ) {
					switchSubmenu(menuId, entryId);
				}
				initSubmenu(menuId, entryId);
				i++;
		}
	}
	switchSubmenu(menuId, subId)
}
function initMenu(menuId) {
	if ( ! document.getElementById ) return;
	var menuBody = document.getElementById(menuId + "Body");

	if ( ! menuBody ) return;
	var i = 0;
	var entry;
	var entryId;
	var entryState;
	for ( entry = menuBody.firstChild; entry; entry = entry.nextSibling ) {
		if ( entry.className == "submenu" ) {
			entryId = String(i);
			entryState = document.getElementById(menuId + "_" + entryId + "State");
			if ( entryState && (entryState.value == "open") ) {
				switchSubmenu(menuId, entryId);
			}
			initSubmenu(menuId, entryId);
			i++;
		}
	}
}

function submitState(menuId, action, entryId) {
	if ( ! document.getElementById ) return;
	var form  = document.getElementById(menuId + "Form");
	var state = document.getElementById(menuId + "State");
	var post  = document.getElementById(menuId + "POST");
	if ( ! form ) {
		location.href = action;
		return;
	}

	if ( entryId ) {
		var clickedState = document.createElement("INPUT");
		clickedState.type  = "hidden";
		clickedState.name  = menuId + "[clicked]";
		clickedState.value = entryId;
		state.appendChild(clickedState);

		var itemPOST = document.getElementById(menuId + "_" + entryId + "POST");
		if ( itemPOST.firstChild ) {
			for ( var node = itemPOST.firstChild; node; node = node.nextSibling ) {
				post.appendChild(node.cloneNode(false));
			}
		}
	}

	form.action = action;
	form.submit();
}

function changeHref(newHref, a) {
	if ( a && a.href ) {
		a.href = newHref;
	}
}