var $A = Array.from = function(iterable)
{
	if (!iterable) return [];
	if (iterable.toArray)
	{
		return iterable.toArray();
	}
	else
	{
		var results = [];
		for (var i = 0, length = iterable.length; i < length; i++)
			results.push(iterable[i]);
		return results;
	}
}


Function.prototype.bind = function()
{
	var __method 	= this,
		args 		= $A(arguments),
		object 		= args.shift();

	return 	function()
			{
				return __method.apply(object, args.concat($A(arguments)));
			}
}

Function.prototype.bindAsEventListener = function(object)
{
	var __method 	= this,
		args 		= $A(arguments),
		object 		= args.shift();

	return 	function(event)
			{
				return __method.apply(object, [event || window.event].concat(args));
			}
}

function addLoadEvent(func)
{
	var oldonload = window.onload;

	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			if (oldonload)
			{
				oldonload();
			}

			func();
		}
	}
}

//-------------------------------------------------------------------------------------------------------------------------------------------------------//

function menu()
{
	this.m_timer_id 		= 0;
	this.m_delay			= 500;
	
	this.m_bDisplayed 		= false;
	
	this.m_currentMenuID	= null;
	this.m_currentSubMenuID	= null;
}

menu.prototype.initMenu = function(ident)
{
	var td_list = document.getElementsByTagName("TD");
	var pattern = eval("/"+ident+"/gi");
		
	for(var i = 0, td; td = td_list[i]; i++)
	{
		var id = td.id;
		
		if(id && id.search(pattern) > -1)
		{
			td.onmouseover 	= function(event,id){ return this.openMenu(event,id); }.bindAsEventListener(this,id);
			td.onmouseout	= function(event) 	{ return this.closeMenu(event); }.bindAsEventListener(this);
		}
	}
}

menu.prototype.hide = function()
{
	if(typeof this.m_currentSubMenuID == "undefined" || this.m_currentSubMenuID.length < 1) { return; }
	document.getElementById(this.m_currentSubMenuID).style.display = "none";
	this.m_bDisplayed = false;
}

menu.prototype.show = function()
{
	if(this.m_currentSubMenuID.length < 1) { return; }
	
	var elm = document.getElementById(this.m_currentSubMenuID);
		elm.style.display 	= "block";
		elm.onmouseover 	= function(event) 	{ this.clearTimer(); }.bindAsEventListener(this)
		elm.onmouseout 		= function(event) 	{ this.closeMenu(event); }.bindAsEventListener(this)
		
	this.m_bDisplayed = true;
}

menu.prototype.openMenu = function(event,menuID)
{
	if(this.m_bDisplayed) this.hide();
	
	this.m_currentMenuID	= menuID;
	this.m_currentSubMenuID = menuID+"_div";
	this.clearTimer(); 
	this.show();
	
	return true;
}

menu.prototype.closeMenu = function(event,menuID)
{
	var target 	= event.originalTarget ? event.originalTarget : event.srcElement;
	
	if(this.m_delay && target.parentNode.id.toLowerCase() != this.m_currentMenuID.toLowerCase())
	{
		var scope 		= this;
		this.m_timer_id = setTimeout(function(){ this.hide(); }.bind(this),this.m_delay);
	}
	
	return true;
}

menu.prototype.clearTimer = function()
{
	if(this.m_timer_id) 
	{
		clearTimeout(this.m_timer_id);
		this.m_timer_id = 0;
	}
}