var menu = {
	delay: 350,
	closing:
	{
		timer: -1,
		menu: null
	}
};

function openMenu(submenu)
{
	if (menu.closing.menu!=null)
	{
		clearTimeout(menu.closing.timer);
		menuClose(menu.closing.menu);
	}
	
	submenu.style.display = "block";
}

function startMenuClose(submenu)
{
	if (menu.closing.menu==submenu)
	{
		clearTimeout(menu.closing.timer);
	}
		
	menu.closing.timer = setTimeout("menuClose()", menu.delay);
	menu.closing.menu = submenu;
}

function stopMenuClose(submenu)
{
	if (menu.closing.menu==submenu)
		clearTimeout(menu.closing.timer);
}

function menuClose()
{
	menu.closing.menu.style.display = "none";
}

function Menu(value)
{
	this.value = value;
	this.items = [];
	this.element = document.createElement("DIV");
	this.element.className = "menu";
	
	if (this.value!=null)
		this.element.innerHTML = this.value;
}
Menu.prototype.addItem = function(menuItem)
{
	this.items[this.items.length] = menuItem;
	menuItem.addTo(this.element);
}
Menu.prototype.addTo = _AddTo_;
Menu.prototype.toString = function()
{
	var returnValue = "";
	
	for (x in this.items)
		returnValue += this.items[x] + "\n";
		
	return returnValue;
}

function Submenu(value)
{
	this.value = value;
	this.items = [];
	this.element = document.createElement("DIV");
	this.element.className = "menu";
		
	this.menuitem = document.createElement("DIV");
	this.menuitem.className = "item item_out";
	
	this.menuitem.onmouseout  = _MenuOut_;
	this.menuitem.onmouseover = _MenuOver_;
	
	if (this.value!=null)
		this.menuitem.innerHTML = this.value;
		
	this.submenu = document.createElement("DIV");
	this.submenu.className = "submenu submenu_out";

	this.submenu.onmouseout  = _SubmenuOut_;
	this.submenu.onmouseover = _SubmenuOver_;
	
	this.element.parent = this;
	this.menuitem.parent = this;
	this.submenu.parent = this;
	
	this.element.appendChild(this.menuitem);
	this.element.appendChild(this.submenu);
}
Submenu.prototype.addItem = function(menuItem)
{
	this.items[this.items.length] = menuItem;
	menuItem.addTo(this.submenu);
}
Submenu.prototype.addTo = _AddTo_;
Submenu.prototype.toString = function()
{
	var returnValue = "";
	
	for (x in this.items)
		returnValue += this.items[x] + "\n";
		
	return returnValue;
}

function _MenuOut_()
{
	this.className = this.className.replace(/over|out|down/, "out");
	
	startMenuClose(this.parent.submenu);
}

function _MenuOver_()
{
	this.className = this.className.replace(/over|out|down/, "over");
	
	openMenu(this.parent.submenu);
}

function _SubmenuOut_()
{
	startMenuClose(this);
}

function _SubmenuOver_()
{
	stopMenuClose(this.parent.submenu);
}

function Item(value, onclick)
{
	this.value = value;
	this.element = document.createElement("DIV");
	this.element.className = "item item_out";
	
	this.element.onmousedown = _ItemDown_;
	this.element.onmouseout  = _ItemOut_;
	this.element.onmouseover = _ItemOver_;
	this.element.onmouseup   = _ItemOver_;
	
	if (onclick!=null)
		this.element.onclick = onclick;

	this.element.innerHTML = value;
	this.element.parent = this;
}
Item.prototype.toString = function() { return this.value; }
Item.prototype.addTo = _AddTo_;

function _ItemDown_() { this.className = this.className.replace(/over|out|down/, "down"); }	
function _ItemOut_()  { this.className = this.className.replace(/over|out|down/, "out"); }	
function _ItemOver_() { this.className = this.className.replace(/over|out|down/, "over"); }

function _AddTo_(element)
{
	this.parentElement = element;
	this.parentElement.appendChild(this.element);
}

function addMenu() {}
