var menuShown = null;
var subMenuShown = null;
var menuItemPosition = 0;
var	subMenuType = 0;

function init()
{
	document.onmousemove = hideMenu
}

function placemenu(menu,position)
{
	var browserWidth 				= document.body.clientWidth;
	var browserLeft 				= (browserWidth / 2) - 390;
	document.all[menu].style.left	= browserLeft + position;
}

function changeBackground(element,type)
{
	if (type == 0)
		element.style.background='#43737A';
	else if (type == 1)
		element.style.background='#225259';
}

function showMenu(menu,status,position)
{
	if (pageLoaded == 1)
	{
		if (status == 'visible')
		{
			placemenu(menu,position);
			menuShown = menu;
		}
		else
			menuShown = null;
		document.all[menu].style.visibility=status;
	}
}

function showSubMenu(from,menu,status)
{
	if (status == 'visible')
	{
		menuItemPosition = 0;
		var NumberDownString = new String(from.match(/\d/g));
		var NumberDown = new Number(NumberDownString.replace(/\,/g, ""));
		
		var cellHeight = 0;
		var currentCell = null;
		var currentMenu = from.slice(0,from.lastIndexOf("_"));
		
		for (i = 1; i <= NumberDown-1; i++)
		{
			currentCell			= currentMenu + '_' + i;
			cellHeight			= document.all[currentCell].clientHeight;
			menuItemPosition	= menuItemPosition + cellHeight
		}
		menuLeft = document.all[currentMenu].style.left;
		menuLeft = new Number(menuLeft.slice(0,(menuLeft.length-2)))+3;
		maxMenuLeft = menuLeft + 300; 
		if (maxMenuLeft > browserWidth)
		{
			document.all[menu].style.left =	menuLeft - 153;
			subMenuType = 1;
		} else {
			document.all[menu].style.left =	menuLeft + 147;
			subMenuType = 0;
		}
		
		var fromTop = new String(document.all[currentMenu].style.top); 
		menuItemPosition = menuItemPosition + new Number(fromTop.slice(0,(fromTop.length-2)));
		menuItemPosition = menuItemPosition - 12 + NumberDown;
		document.all[menu].style.top = menuItemPosition;
		subMenuShown = menu;
	}
	else
		subMenuShown = null;
	document.all[menu].style.visibility = status;
}

function hideMenu(evt)
{
	if (menuShown != null)
	{
		var browserWidth = document.body.clientWidth;
		//if (siteIframe.window.event != null)
		//{
		//	var x 				= siteIframe.window.event.clientX;
		//	var y 				= siteIframe.window.event.clientY;
		//} else {
			var x 				= window.event.clientX;
			var y 				= window.event.clientY;
		//}
		var el 				= document.all[menuShown];
		var e2 				= document.all["table"+menuShown];

		var IntHeight 		= new Number(e2.clientHeight);
		IntHeight 			= IntHeight + 29;

		var IntWidth  		= new Number(e2.clientWidth);
	
		var cssTopCornerY 	= document.all[menuShown].style.top;
		var topCornerY		= new Number(cssTopCornerY.slice(0,(cssTopCornerY.length-2)));
		topCornerY 			= topCornerY - 29;
		topCornerY 			= topCornerY - document.body.scrollTop;
		
		var cssTopCornerX 	= document.all[menuShown].style.left;
		var topCornerX 		= new Number(cssTopCornerX.slice(0,(cssTopCornerX.length-2)));
		topCornerX 			= topCornerX - document.body.scrollLeft;
		
		var bottomCornerY 	= topCornerY + IntHeight;
		var bottomCornerX 	= topCornerX + IntWidth;
		
		if (subMenuShown != null)
		{
			var subel 				= document.all[menuShown];
			var sube2 				= document.all["table"+subMenuShown];
	
			var subIntHeight 		= new Number(sube2.clientHeight);
			var subIntWidth  		= new Number(sube2.clientWidth);
		
			var subcssTopCornerY 	= document.all[subMenuShown].style.top;
			var subtopCornerY		= new Number(subcssTopCornerY.slice(0,(subcssTopCornerY.length-2)));
			subtopCornerY 			= subtopCornerY - document.body.scrollTop;
			
			var subcssTopCornerX 	= document.all[subMenuShown].style.left;
			var subtopCornerX 		= new Number(subcssTopCornerX.slice(0,(subcssTopCornerX.length-2)));
			subtopCornerX 			= subtopCornerX - document.body.scrollLeft;
			
			var subbottomCornerY 	= subtopCornerY + subIntHeight;
			var subbottomCornerX 	= subtopCornerX + subIntWidth;
		}
		if ((menuShown != null) && (subMenuShown != null)) {
			if (subMenuType == 0) {
				if (subbottomCornerY > bottomCornerY)
				{
					if ( (x < topCornerX) || (y < topCornerY) || (x > subbottomCornerX) || (y > subbottomCornerY) || ((x > bottomCornerX) && (y < subtopCornerY)) || ((x < subtopCornerX) && (y > bottomCornerY)) )
					{
						showMenu(menuShown,'hidden');
						showSubMenu('',subMenuShown,'hidden')
					}
				}
				else
				{
					if ( (x < topCornerX) || (y < topCornerY) || (x > subbottomCornerX) || (y > bottomCornerY) || ((x > bottomCornerX) && (y < subtopCornerY)) || ((x > bottomCornerX) && (y > subbottomCornerY)) )
					{
						showMenu(menuShown,'hidden');
						showSubMenu('',subMenuShown,'hidden')
					}
				}
			} else {
				if (subbottomCornerY > bottomCornerY)
				{
					if ( (x > bottomCornerX) || (y < topCornerY) || (x < subtopCornerX) || (y > subbottomCornerY) || ((x > subbottomCornerX) && (y > bottomCornerY)) || ((x < topCornerX) && (y < subtopCornerY)) )
					{
						showMenu(menuShown,'hidden');
						showSubMenu('',subMenuShown,'hidden')
					}
				}
				else
				{
					if ( (x > bottomCornerX) || (y < topCornerY) || (x < subtopCornerX) || (y > subbottomCornerY) || ((x > subbottomCornerX) && (y > bottomCornerY)) || ((x < topCornerX) && (y > subbottomCornerY)) )
					{
						showMenu(menuShown,'hidden');
						showSubMenu('',subMenuShown,'hidden')
					}
				}
			}
		} else {
			if ( (x < topCornerX) || (x > bottomCornerX) || (y < topCornerY) || (y > bottomCornerY) )
			{
				showMenu(menuShown,'hidden');
			}
		}
	}
	return
}

function hideAll()
{
	if (menuShown != null)
	{
		showMenu(menuShown,'hidden');
	}
	if (subMenuShown != null)
	{
		showSubMenu('',subMenuShown,'hidden');
	}
}

function hideAllSubs()
{
	if (subMenuShown != null)
	{
		showSubMenu('',subMenuShown,'hidden');
	}
}
