//Attaches an event handler to a DOM element
/*function addEvent(element, eventname, func, use_capture)
{
        if (element.addEventListener)
        {
                element.addEventListener(eventname, func, use_capture);
                return true;
        }
        else if (element.attachEvent)
        {
                var r=element.attachEvent('on'+eventname, func);
                return r;
        }
        else
        {
                element['on'+eventname]=func;
                return true;
        }

        return false;
}*/


var timer=new Object;

//Sets the visibility of the DOM element el to hidden
function hide_el(el)
{
	el.style.visibility='hidden';
}

//Sets the visibility of the DOM element el to visible
function show_el(el)
{
	el.style.visibility='visible';
}

//Cancels hiding of all menus
function cancel_hide()
{
	if (timer['linkbar'])
		clearTimeout(timer['linkbar']);
	timer['linkbar']=0;
}

//Displays the menu with ID which
function sm(which)
{
	cancel_hide();

	element=document.getElementById(which);
	if (!element)
		return;
	
	if (timer[which])
		clearTimeout(timer[which]);
			
	show_el(document.getElementById(which));
}

//Hides the menu with ID which after a brief delay
function hm(which)
{
	element=document.getElementById(which);
	if (!element)
		return;

	if (timer[which])
		clearTimeout(timer[which]);

	timer[which]=setTimeout("reset_menu('"+which+"')", 30);
}

//Hides all menus after a delay
function hmd()
{
	if (timer['linkbar'])
		clearTimeout(timer['linkbar']);
	
	timer['linkbar']=setTimeout("hide_menus()", 500);
}

//Hides all menus
function hide_menus()
{
	timer['linkbar']=0;

	element=document.getElementById('linkbar');
	if (!element)
		return;

	hide_children(element);
}

//Hides the menu with ID which, and all submenus
function reset_menu(which)
{
	timer[which]=0;
	element=document.getElementById(which);
	if (!element)
		return;
		
	hide_el(element);
	hide_children(element);
}

//Shows the menu with ID which while hiding any sibling menus
function showonly(which)
{
	sm(which);
	hs(which);
}

//Hides the sibling menus of the menu with ID which
function hs(which)
{
	var menu;
	var subm;
	
	var element=document.getElementById(which);
	if (!element)
		return;
	
	for (var i=0; i < element.parentNode.parentNode.childNodes.length; i++)
	{
		subm=element.parentNode.parentNode.childNodes.item(i);
		
		if (subm.nodeType == 1 && classForElement(subm) == 'menuitem')
		{
			for (var j=0; j < subm.childNodes.length; j++)
			{
				menu=subm.childNodes.item(j);
				if (menu.nodeType == 1 && classForElement(menu) == 'menu' && menu.getAttribute('id') != which)
				{
					hide_el(menu);
					hide_children(menu);
				}
			}
		}
	}
}

//Hides the children of the menu with ID which
function hc(which)
{
	element=document.getElementById(which);
	if (!element)
		return;
		
	hide_children(element);
}

//Returns the class attribute for the DOM element el
//MSI rewrites the attribute name from class to className for no reason
function classForElement(el)
{
	c=el.getAttribute('class');
	if (!c)
		c=el.getAttribute('className');	//IE is a pile of shite
	return c;
}

//Hides all child menus of the DOM element el
function hide_children(el)
{
	var j;

	for (j=0; j < el.childNodes.length; j++)
	{
		var menu=el.childNodes.item(j);
		if (menu.nodeType == 1)
		{
			if (classForElement(menu) == 'menu')
			{
				hide_el(menu);
				hide_children(menu);
			}
			else if (classForElement(menu) == 'menuitem')
				hide_children(menu);
		}
	}
}

/*function setupEvents()
{
	if (document.getElementById)
	{
		linkbar=document.getElementById('linkbar');
		addEvent(linkbar, 'mouseout', hmd, false);

		for (linkbar.childNodes as c)
		{
		
		}
	}
}*/

//addEvent(window, 'load', setupEvents, false);
