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

function init()
{
	document.onmousemove = hideMenu
}

function placemenu(menu,position)
{
	var browserWidth							= window.innerWidth;
	var browserLeft 							= (browserWidth / 2) - 390;
	document.getElementById(menu).style.left	= browserLeft + position - 8;
}

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.getElementById(menu).style.visibility=status;
	}
}

function showSubMenu(from,menu,status)
{
	if (status == 'visible')
	{
		var browserWidth = window.innerWidth;
		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.getElementById(currentCell).offsetHeight;
			menuItemPosition	= menuItemPosition + cellHeight
		}
		
		menuLeft = document.getElementById(currentMenu).style.left;
		menuLeft = new Number(menuLeft.slice(0,(menuLeft.length-2)))+3;
		maxMenuLeft = menuLeft + 300;
		if (maxMenuLeft > browserWidth)
		{
			document.getElementById(menu).style.left =	menuLeft - 153;
			subMenuType = 1;
		} else {
			document.getElementById(menu).style.left =	menuLeft + 147;
			subMenuType = 0;
		}
		
		var fromTop = new String(document.getElementById(currentMenu).style.top); 
		menuItemPosition = menuItemPosition + new Number(fromTop.slice(0,(fromTop.length-2)));
		menuItemPosition = menuItemPosition - 12;
		document.getElementById(menu).style.top = menuItemPosition;
		subMenuShown = menu;
	}
	else
		subMenuShown = null;
	document.getElementById(menu).style.visibility=status;
}


function hideMenu(evt)
{
	if (menuShown != null)
	{
		var x 				= evt.pageX;
		var y 				= evt.pageY;

		var el = document.getElementById(menuShown);

		var cssIntHeight 	= document.defaultView.getComputedStyle(el,null).getPropertyValue('height');
		var IntHeight 		= new Number(cssIntHeight.slice(0,(cssIntHeight.length-2)));
		IntHeight 			= IntHeight + 29;

		var cssIntWidth  	= document.defaultView.getComputedStyle(el,null).getPropertyValue('width');
		var IntWidth 		= new Number(cssIntWidth.slice(0,(cssIntWidth.length-2)));

		var cssTopCornerY 	= document.getElementById(menuShown).style.top;
		var topCornerY		= new Number(cssTopCornerY.slice(0,(cssTopCornerY.length-2)));
		topCornerY 			= topCornerY - 29;
	
		var cssTopCornerX 	= document.getElementById(menuShown).style.left;
		var topCornerX 		= new Number(cssTopCornerX.slice(0,(cssTopCornerX.length-2)));

		var bottomCornerY 	= topCornerY + IntHeight;
		var bottomCornerX 	= topCornerX + IntWidth;
		
		if (subMenuShown != null)
		{
			var subel = document.getElementById(subMenuShown);
	
			var subcssIntHeight 	= document.defaultView.getComputedStyle(subel,null).getPropertyValue('height');
			var subIntHeight 		= new Number(subcssIntHeight.slice(0,(subcssIntHeight.length-2)));
	
			var subcssIntWidth  	= document.defaultView.getComputedStyle(subel,null).getPropertyValue('width');
			var subIntWidth 		= new Number(subcssIntWidth.slice(0,(subcssIntWidth.length-2)));
	
			var subcssTopCornerY 	= document.defaultView.getComputedStyle(subel,null).getPropertyValue('top');
			var subtopCornerY		= new Number(subcssTopCornerY.slice(0,(subcssTopCornerY.length-2)));
		
			var subcssTopCornerX 	= document.defaultView.getComputedStyle(subel,null).getPropertyValue('left');
			var subtopCornerX 		= new Number(subcssTopCornerX.slice(0,(subcssTopCornerX.length-2)));
	
			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');
	}
}
