var activesubmenu = null;
var timers = null;

function initsubmenu(id) {
	var menu_array = getmenuarray(id);
	if (!menu_array)
	    return;
	    
	var container = document.getElementById("submenucontainer");
	var submenu = document.getElementById("submenu");
	var staticsubmenu = document.getElementById("staticsubmenu");
	removechildren(submenu);
	if (menu_array.length > 1) {
	    addmenuitems(submenu, menu_array);
	    return true;
	} else {
	    container.style.display = "none";
	    if (staticsubmenu != null)
	        staticsubmenu.style.display = "block";
	    return false;
	}
}
function showsubmenu(id) {
	if (name == activesubmenu) {
		persistsubmenu();
		return;
	}

	var submenu = document.getElementById("submenu");
	var submenucontainer = document.getElementById("submenucontainer");
	var staticsubmenu = document.getElementById("staticsubmenu");
	if (submenu != null) {
		persistsubmenu();
		if (initsubmenu(id)) {
		    activesubmenu = id;
	        submenucontainer.style.display = "block";
	        if (staticsubmenu != null)
	            staticsubmenu.style.display = "none";
	    } else
	        submenucontainer.style.display = "none";
	}
}
function persistsubmenu() {
	if (timers == null)
		return;

	for (var i = 0; i < timers.length; i++) {
		clearTimeout(timers[i]);
	}
	timers = null;
}
function inithidesubmenu() {
	if (timers == null)
		timers = new Array();

	timers.push(setTimeout("hidesubmenu();", 500));
}
function hidesubmenu() {
	if (activesubmenu == null)
		return;

	var submenucontainer = document.getElementById("submenucontainer");
	var staticsubmenu = document.getElementById("staticsubmenu");
	if (submenucontainer != null) {
		timers = null;
		submenucontainer.style.display = "none";
		if (staticsubmenu != null)
		    staticsubmenu.style.display = "block";
		activesubmenu = null;
	}
}
function getmenuarray(id) {
	for (var i = 0; i < menu_elements.length; i++) {
		if (menu_elements[i][0] == id)
			return menu_elements[i];
	}
	return null;

}
function createmenuitem(name, href) {
	var listitem = document.createElement("li");
	var anchor = document.createElement("a");
	listitem.appendChild(anchor);

	anchor.href = href;
	anchor.innerHTML = name;

	return listitem;
}
function addmenuitems(submenu, menu_array) {
    var background = document.getElementById("submenubackground");
    
    background.className = "background_portal" + menu_array[0];
    submenu.className = "submenu submenu_portal" + menu_array[0];
	for (var i = 1; i < menu_array.length; i+=2) {
		submenu.appendChild(createmenuitem(menu_array[i], menu_array[i + 1]));
	}
}
function removechildren(element) {
	while (element.hasChildNodes())
		element.removeChild(element.firstChild);
}
