// Default Variables
var menuShown 			= null;		var subMenuShown 		= null;		var menuItemPosition 	= 0;
var	subMenuType 		= 0;		var colourOff 			= null;		var colourOn 			= null;
var maxMenuLeftOffset 	= 0;		var menuWidth			= 0;		var subMenuTopOffset	= 0;
var menuFromTop			= 0;		var el 					= null;		var IntHeight 			= 0;
var IntWidth  			= 0;		var topCornerY			= 0;		var topCornerX	 		= 0;
var bottomCornerY 		= 0;		var bottomCornerX 		= 0;		var subel 				= null;
var subIntHeight 		= 0;		var subIntWidth  		= 0;		var subtopCornerY 		= 0;
var subtopCornerX	 	= 0;		var subbottomCornerY 	= 0;		var subbottomCornerX 	= 0;

// Attached the onmousemove event to the hideMenu function
function init() {
	document.onmousemove = hideMenu
}

// Function to change the background colour on the menu when the mouse is over or out
function changeBackground(element,type) {
	if (type == 0)
		element.style.background=colourOff;
	else if (type == 1)
		element.style.background=colourOn;
}

// Function to show the menu
function showMenu(menu,status) {
	if (pageLoaded == 1) {
		document.getElementById(menu).style.visibility=status;
		if (status == 'visible') {
			menuShown = menu;
			// Creates the menu object
			el = document.getElementById(menuShown);
			// Gets the height and width of the menu
			IntHeight 		= parseInt(document.defaultView.getComputedStyle(el,null).getPropertyValue('height')) + menuFromTop;
			IntWidth  		= parseInt(document.defaultView.getComputedStyle(el,null).getPropertyValue('width'));
			// Get the top and bottom coordinates of the menu
			topCornerY 		= parseInt(document.getElementById(menuShown).style.top) - menuFromTop;
			topCornerX 		= parseInt(document.getElementById(menuShown).style.left);
			bottomCornerY 	= topCornerY + IntHeight;
			bottomCornerX 	= topCornerX + IntWidth;

		} else
			menuShown = null;
	}
}

// Function to show the sub menu
function showSubMenu(from,menu,status) {
	if (status == 'visible') {
		menuItemPosition = 0;
		var browserWidth = window.innerWidth;
		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 = parseInt(document.getElementById(currentMenu).style.left);
		maxMenuLeft = menuLeft + maxMenuLeftOffset;
		if (maxMenuLeft > browserWidth) {
			document.getElementById(menu).style.left =	menuLeft - menuWidth;
			subMenuType = 1;
		} else {
			document.getElementById(menu).style.left =	menuLeft + menuWidth;
			subMenuType = 0;
		}
		menuItemPosition = menuItemPosition + parseInt(document.getElementById(currentMenu).style.top) + subMenuTopOffset;
		document.getElementById(menu).style.top = menuItemPosition;
		subMenuShown = menu;
		// Creates the sub-menu objects
		subel = document.getElementById(subMenuShown);
		// Gets the height and width of the sub-menu	
		subIntHeight 		= parseInt(document.defaultView.getComputedStyle(subel,null).getPropertyValue('height'));
		subIntWidth  		= parseInt(document.defaultView.getComputedStyle(subel,null).getPropertyValue('width'));
		// Get the top and bottom coordinates of the sub-menu
		subtopCornerY 		= parseInt(document.defaultView.getComputedStyle(subel,null).getPropertyValue('top'));
		subtopCornerX 		= parseInt(document.defaultView.getComputedStyle(subel,null).getPropertyValue('left'));
		subbottomCornerY 	= subtopCornerY + subIntHeight;
		subbottomCornerX 	= subtopCornerX + subIntWidth;
	}
	else
		subMenuShown = null;
	document.getElementById(menu).style.visibility=status;
}

// Function to evaluate if the menu need to be hidden and to call the hide menu function
function hideMenu(evt) {
	if (menuShown != null) 	{
		// Gets the mouse cursor position
		var x 				= evt.pageX;
		var y 				= evt.pageY;
		// Evaluates if the menu needs to be hidden or not
		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
}

// Hides any shown menu or sub-menu
function hideAll() {
	if (menuShown != null)
		showMenu(menuShown,'hidden');
	if (subMenuShown != null)
		showSubMenu('',subMenuShown,'hidden');
}

// Hides any shown sub-menu
function hideAllSubs() {
	if (subMenuShown != null)
		showSubMenu('',subMenuShown,'hidden');
}