var Menus = new Array();
var SubMenus = new Array();
var curMenu = null;
var SubLock = null;
var HideTimer = null;

function MenuInit( menuItemName, is_shown_onclick )
{
	var i = 0;
	var j = 0;
	while( Menus[i] = document.getElementById(menuItemName+""+(i+1)) )
	{
		if( sub = document.getElementById( "sub_"+Menus[i].id ) )
		{
			//sub.style.zIndex='10000';
			if( is_shown_onclick == true )
				Menus[i].onclick = showSubMenu;
			else
				Menus[i].onmouseover = showSubMenu;
			Menus[i].submenu = sub;
			SubMenus[SubMenus.length] = sub;
			sub.parent = Menus[i];
			sub.onmouseover = cancelHide;
			sub.onclick = function ()
			{
				document.onclick = null;
			}
			sub.onmouseout = function ()
			{
				document.onclick = hideAllSubMenu;
				HideTimer = setTimeout( "setHide()", 500 );
			}
		}
		else
		{
			Menus[i].onmouseover = hideElse;
		}
		i++;
	}
	for( i=0; i< SubMenus.length; i++ )
	{
		lnks = SubMenus[i].getElementsByTagName('A');
		for( j=0; j<lnks.length; j++ )
		{
			lnks[j].parent = SubMenus[i];
		}
	}
	
	document.onclick = hideAllSubMenu;
}

function hideAllSubMenu()
{
	for( i=0; i<SubMenus.length; i++ )
		SubMenus[i].style.display = 'none';
}

function showSubMenu()
{
	showPath( this );
	document.onclick = null;
	//if( !this.submenu.positioned )
	{
		node = this;
		var left = 0;//node.offsetLeft;
		var top = 0;//node.offsetTop;
		while( node.offsetParent )
		{
			left += node.offsetLeft;
			top += node.offsetTop;
			node = node.offsetParent;
		}
		
		if( this.submenu.getAttribute( 'name' ) )
		{
			if( this.submenu.getAttribute( 'name' ).match( /bottom/ ) )
				top += this.offsetHeight + 5;
			if( this.submenu.getAttribute( 'name' ).match( /right/ ) )
				left += this.offsetWidth + 5;
			if( this.submenu.getAttribute( 'name' ).match( /left/ ) )
			{
				this.submenu.style.display = "block";
				left -= (this.submenu.offsetWidth);
			}
			if( this.submenu.getAttribute( 'name' ).match( /align_r/ ) )
			{
				this.submenu.style.display = "block";
				left += this.offsetWidth + 5;
			}
		}
		else
			top += this.offsetHeight;
		
		this.submenu.style.left = left+'px';
		this.submenu.style.top = top+'px';
		this.submenu.positioned = true;
		
	}

	this.submenu.style.display = "block";
	curMenu = this.submenu;

}



function showPath( obj )
{
	for( i=0; i<SubMenus.length; i++ )
	{
		SubMenus[i].style.display = 'none';
	}
	
	var menu = null;
	if( obj.parent )
		menu = obj.parent;

	while( menu )
	{
		if( menu.tagName == 'DIV' )
		{
			menu.style.display = 'block';
		}
		if( menu.parent )
			menu = menu.parent;
		else
			break;
	}
}

function hideElse()
{
	
	if( curMenu )
	{
		curMenu.style.display = 'none';
		curMenu = null;
	}

	showPath( this );
}
function cancelHide()
{
	if( curMenu )
		if( this.id == curMenu.id )
			curMenu = null;
	
	if( this.timeout )
		clearTimeout( this.timeout );
	if( HideTimer )
		clearTimeout( HideTimer );
}

function setHide()
{
	for( i=0; i<SubMenus.length; i++ )
	{
		SubMenus[i].style.display = 'none';
	}
}

/* Transparency Patch For IE */
/* Shadow for popupMenu */
function setAlpha( obj )
{
	if( obj.runtimeStyle && obj.src.match( /\.png$/ ) )
	{
		obj.runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src='+obj.src+', sizingMethod="scale")';
		obj.src = 'images/spacer.gif';
	}
}

function scale( obj )
{
	alert( obj.offsetParent.offsetHeight );
}