function Menu(id) {
	this.menu_id = id;
	this.items = new Array();
	this.actions = new Array();
	this.addItem = addItem;
	this.writeMenus = writeMenus;
	if (!window.menus) window.menus = new Array();
	this.label = "menuLabel" + window.menus.length;
	window.menus[this.label] = this;
	window.menus[window.menus.length] = this;
	if (!window.ActiveMenu) window.ActiveMenu = new Array();
	this.timer = 0;
	if (!window.HoverMenu) window.HoverMenu = new Array();
}

function addItem(name, path) {
	this.items[this.items.length] = name;
	this.actions[this.actions.length] = path
}

function writeMenus() {
	if (window.triedWriting) return;
	document.writeln('<span id="menucont"></span>');
	var container = document.getElementById('menucont');
	if (!container) return;
	container.style.borderColor = 'red';
	container.style.borderWidth = '1px';
	window.triedToWriteMenus = true;
	container.menus = new Array();
	for (var x = 0; x < window.menus.length; x++) {
		container.menus[x] = window.menus[x];
	}
	//alert(container.menus.length);
	window.menus.length = 0;
	var countItems = 0;
	var countMenus = 0;
	var content = '';
	for (var x = 0; x < container.menus.length; x++) {
		var menu = container.menus[x];
		content += '<div id="menu'+x+'" class="menu" style="display:none;position: absolute;filter:alpha(opacity=100);opacity:1;border: 1px outset #adadad;background:#dfdfdf;">'+
		'<ul onmouseover="addHover();" onmouseout="remHover();remMenu();">';
		for (var i = 0; i < menu.items.length; i++) {
			var item = menu.items[i];
			var action = menu.actions[i];
			content += '<li><a href="'+action+'">'+item+'</a></li>';
		}
		content += '</ul></div>';
	}
	container.innerHTML += content;
	for (var x = 0; x < container.menus.length; x++) {
		var menuLayer = document.getElementById('menu'+x);
		container.menus[x].menuLayer = 'menu'+x;
		menuLayer.Menu = container.menus[x];
		menuLayer.Menu.container = 'menu'+x;
		menuLayer.style.zIndex = 1;
	}
}

function showMenu(id, x, y) {
	if (window.ActiveMenu.length > 0) {
		window.ActiveMenu[0].style.display = 'none';
		window.ActiveMenu.length=0;
	}
	if (this.timer) {
		clearTimeout(this.timer);
	}
	var l = id.menuLayer || id;
	var menu = document.getElementById(l);
	menu.style.display = 'block';
	menu.style.left = menu.style.pixelLeft = (x || (window.pageX + document.body.scrollLeft) || 0)+'px';
	menu.style.top = menu.style.pixelTop = (y || (window.pageY + document.body.scrollTop) || 0)+'px';
	window.ActiveMenu[window.ActiveMenu.length] = menu;
	menu.Menu.xOffset = document.body.scrollLeft;
	menu.Menu.yOffset = document.body.scrollTop;
}

function remMenu(){
	if (window.HoverMenu.length == 0) {
		this.timer = setTimeout('killMenu()', 6000);
	}
}

function killMenu(){
	if (window.HoverMenu.length == 0) {
		if (window.ActiveMenu.length > 0) {
			window.ActiveMenu[0].style.display = 'none';
			window.ActiveMenu.length=0;
		}
	}
}

function addHover() {
	window.HoverMenu[window.HoverMenu.length] = this.menu;
}
function remHover() {
	window.HoverMenu.length = 0;
}
