// alib common object
function Alib() {};

// retrieve target for event
Alib.prototype.getTarget =
	function(e)
	{
		var target;

		if (e.target) { target = e.target; }
		else if (e.srcElement) { target = e.srcElement; }

		// defeat Safari bug
		if (target.nodeType == 3) { target = target.parentNode; }

		return target;
	}

// retrieve related target for event
Alib.prototype.getRelatedTarget =
	function(e)
	{
		return (e.relatedTarget) ? e.relatedTarget : e.toElement;
	}

Alib.prototype.addListener =
	function(obj, event, handler)
	{
		if (obj.addEventListener)
		{
			obj.addEventListener(event, handler, false);
		}
		else if (obj.attachEvent)
		{
			obj.attachEvent('on' + event, handler);
		}
		else
		{
			window.status = 'Seu navegador não suporta o menu.';
		}

	}

/* warning, ie is choking on addStyleSheet */
Alib.prototype.addStyleSheet =
	function(title, href)
	{
		/*
		var head = document.getElementsByTagName('head')[0];
		var link = document.createElement('link')
		link.rel = 'stylesheet';
		link.type = 'text/css';
		link.title = '';
		link.href = href;

		head.appendChild(link);
		*/

		document.write('<link rel="stylesheet" href="css/'+href+'" type="text/css">');
	}

var alib = new Alib();


/***********************************/
// alib Hmenu object
function Hmenu() {};

Hmenu.prototype.addBehavior =
	function()
	{
		// get menu roots
		var node;
		var i = 0;
		var k = 1;
		while ((node = document.getElementsByTagName('UL').item(i++)))
		{
			if (node.className == 'menu') /*node.className == 'menu'*/
			{			  node.style.display = "block";				node.className = 'itensMenu';

				for (var j = 0; j < node.childNodes.length;j++)
				{
					if (node.childNodes[j].tagName == 'LI')
					{
						Hmenu.prototype.initMenuItem(node.childNodes[j], k++);
					}
				}
			}
		}
	}

Hmenu.prototype.initMenuItem =
	function(li, num)
	{
		// to emulate :first-child
		if (num == 1)
		{
			li.style.borderTopWidth = '1px';
			li.style.borderLeftWidth = '1px';
		}

		for (var i = 0; i < li.childNodes.length; i++)
		{
			if (li.childNodes[i].tagName == 'UL')
			{
				var nodeUL = li.childNodes[i];
				li.cachedUL = nodeUL;

				// 1 + n-level submenu
				if (li.parentNode.className == 'itensMenu') //className == 'itensMenu'
				{
					// background arrow
					li.style.backgroundRepeat = 'no-repeat';
					li.style.backgroundImage = 'url("tela/seta_menu-baixo.gif")';
					li.style.backgroundColor = '#0382A2';
					li.style.backgroundPosition = '99%';
				}

				alib.addListener(li, 'mouseover', Hmenu.prototype.showSubmenu);
				alib.addListener(li, 'mouseout',  Hmenu.prototype.hideSubmenu);

				var k = 1;
				for (var j = 0; j < nodeUL.childNodes.length; j++)
				{
					if (nodeUL.childNodes[j].tagName == 'LI')
					{
						this.initMenuItem(nodeUL.childNodes[j], k++);
					}
				}
			}
		}
	}

Hmenu.prototype.showSubmenu =
	function(e)
	{
		if (!e) { var e = window.event; }

		var target = alib.getTarget(e);

		if (target.tagName == 'A') { target = target.parentNode; }
		if (target.cachedUL)
		{
			target.cachedUL.style.display = 'block';
			//document.getElementById('drop').style.visibility = 'hidden'; // drop-down
		}
	}

Hmenu.prototype.hideSubmenu =
	function(e)
	{
		if (!e) { var  e = window.event; }

		var target = alib.getTarget(e);
		var related = alib.getRelatedTarget(e);

		if (!related || related.tagName == 'HTML')
		{
			related = document.getElementsByTagName('body')[0];
		}

		Hmenu.prototype.report('out '+target.tagName+'->'+related.tagName);
		Hmenu.prototype.report("\n");

		// maybe mouse left only into successor, handle
		if (target.tagName == 'A') { target = target.parentNode; }
		if (Hmenu.prototype.isAncestor(target, related)) { return; }

		// it left into ancestor
		// find ul upon related node or body
		var commonUL = related;
		while (commonUL.tagName != 'UL' && commonUL.tagName != 'BODY')
		{
			commonUL = commonUL.parentNode;
		}

		// iterate up the tree from target till commonUL and close submenu
		Hmenu.prototype.report('coUL:'+commonUL.tagName+'::');
		while (target != commonUL && target.tagName != 'BODY')
		{
			Hmenu.prototype.report(target.tagName+'~');
			if (target.cachedUL)
			{
				target.cachedUL.style.display = 'none';
				//document.getElementById('drop').style.visibility = 'visible'; // drop-down
			}
			target = target.parentNode;
		}
		Hmenu.prototype.report("\n");
	}

Hmenu.prototype.isAncestor =
	function (ancestor, child)
	{
		while (child != ancestor && child.nodeName != 'BODY')
		{
			child = child.parentNode;
		}

		return child == ancestor ? 1 : 0;
	}

Hmenu.prototype.report = function () {};

/* INITS */

var hmenu = new Hmenu();

// register onload init
alib.addListener(window, 'load', hmenu.addBehavior);