var activeMenu = null;
var activeOption = null;
var activeMenuHead = null;
var activeMenuBody = null;
var activeMenuSimple = null;
var popupFix = null;

var leftOff = 0;
var rightOff = 0;
var topOff = 0;
var bottomOff = 0;

var docLayers = (document.layers) ? true:false;

function getMenuHTML(menuItems) {
	var menuHTML = "";
	var linkPrinted = 0;
	var linksOnString = 0;
	for (var i=0; i<menuItems.length;i++) {
		if (menuItems[i]["hasChild"]) {
			var parentid = menuItems[i]["sectionid"];
			menuHTML += '<p style="margin:0px; padding-bottom:10px"><a class="main-menu-sublink-b" href="' + menuItems[i]["href"] + '">' + menuItems[i]["title"] + '</a>';
			linkPrinted = 1;
			linksOnString = 0;					
			for (var j=0; j<menuItems.length; j++) {
				if (menuItems[j]["parentid"] == parentid) {
					if (menuItems[j]["title"].length > 6 || linksOnString > 5) {
						menuHTML += '<br />';
						linksOnString = 0;
					} else {
						linksOnString++;
					}
					menuHTML += '<a class="main-menu-sublink" href="' + menuItems[j]["href"] + '">' + menuItems[j]["title"] + '</a> ';
				}
			}
			
			menuHTML += "</p>";
			linksOnString = 0;
		} else if (menuItems[i]["parentid"] == 0) {
			if (menuItems[i]["title"].length > 6 && linkPrinted == 1 || linksOnString > 5) {
				menuHTML += '<br />';
				linksOnString = 0;
			} else {
				linksOnString++;
			}
			linkPrinted = 1;
			menuHTML += '<a class="main-menu-sublink" href="' + menuItems[i]["href"] + '">' + menuItems[i]["title"] + '</a> ';
		}
	}
	return menuHTML;
}


function testMouseMove(e) {
	if (activeMenu != null) {
		if (!e) var e = window.event;
		if (e.pageX || e.pageY)
		{
		    posx = e.pageX;
		    posy = e.pageY;
		}
		else if (e.clientX || e.clientY) 
		{
			posx = e.clientX + document.documentElement.scrollLeft;
			posy = e.clientY + document.documentElement.scrollTop;
		}
		
		if (posx < leftOff || posx > rightOff || posy < topOff || posy > bottomOff) {
			showMenu(activeOption, activeMenu, false);
		}
	}
}

if (docLayers) {
  	document.captureEvents(Event.MOUSEMOVE)
  	document.onMouseMove = testMouseMove
} else {
  	document.onmousemove = testMouseMove
}

function showMenu(object, menuName, show, menuIndex, menuTitle, menuHref) {	
	var x = findPosX(object);
	var y = findPosY(object);
	
	var menuHead = document.getElementById(menuName + 'Head');
	var menuBody = document.getElementById(menuName + 'Body');
	var menuSimple = document.getElementById(menuName + 'Simple');
	
	if (show) {
		if (activeMenu) {
			showMenu(activeOption, activeMenu, false, 0, '');
		}
		
		if (mainMenuItems[menuIndex].length > 0) {
			var menuHeadContent = document.getElementById("homeMenuHeadContent");
			menuHeadContent.innerHTML = '<a class="popup-head" href="'+ menuHref + '" style="white-space:nowrap">' + menuTitle + '</a>';
			
			var bodyContent = '<div id="menuBodyContentDiv" style="padding:5px; ">';
			bodyContent += mainMenuItems[menuIndex];
			
			bodyContent += '</div>';
			
			var menuBodyContent = document.getElementById("homeMenuBodyContent");
			menuBodyContent.innerHTML = bodyContent;

			menuHead.style.left = x + "px";
			menuHead.style.top = y - 7 + "px";
			menuHead.style.display = "block";
	
			menuBody.style.left = x - 24 + "px";
			menuBody.style.top = (menuHead.clientHeight == 0) ? y + menuHead.offsetHeight - 10 + "px" : y + menuHead.clientHeight - 10 + "px";
			menuBody.style.display = "block";
			
			object.style.visibility = "hidden";
			
			activeMenu = menuName;
			activeOption = object;
			
			var mbCW = (menuBody.clientWidth == 0) ? menuBody.offsetWidth : menuBody.clientWidth;
			var mhCW = (menuHead.clientWidth == 0) ? menuHead.offsetWidth : menuHead.clientWidth;
			var mbCH = (menuBody.clientHeight == 0) ? menuBody.offsetHeight : menuBody.clientHeight;
			var mhCH = (menuHead.clientHeight == 0) ? menuHead.offsetHeight : menuHead.clientHeight;
			
			leftOff = x - 24;
			rightOff = x - 24 + mbCW;
			topOff = y - 7;
			bottomOff = y - 7 + mhCH + mbCH;
			
			if (rightOff < x + mhCW) {
				var menuBodyContentDiv = document.getElementById("menuBodyContentDiv");
				var divW = (menuBodyContentDiv.clientWidth == 0) ? menuBodyContentDiv.offsetWidth : menuBodyContentDiv.clientWidth;
				var dwx = (x + mhCW) - rightOff;
				divW += dwx;
				rightOff += dwx;
				menuBodyContentDiv.style.width = divW + "px";
			}
			
			if (popupFix == null) {
				popupFix = document.getElementById("popupfix");
			}
			
			mbCH = (menuBody.clientHeight == 0) ? menuBody.offsetHeight : menuBody.clientHeight;
			mbCW = (menuBody.clientWidth == 0) ? menuBody.offsetWidth : menuBody.clientWidth;
			popupFix.style.width = (mbCW-8) + "px";
			popupFix.style.height = (mbCH-14) + "px";
			popupFix.style.left = x - 21 + "px";
			popupFix.style.top = (menuHead.clientHeight == 0) ? y + menuHead.offsetHeight - 5 + "px" : y + menuHead.clientHeight - 5 + "px";
			popupFix.style.display = "block";
			
		} else {
			var menuSimpleContent = document.getElementById("homeMenuSimpleContent");
			menuSimpleContent.innerHTML = '<a class="popup-head" href="' + menuHref + '">' + menuTitle + '</a>';

			menuSimple.style.left = x + "px";
			menuSimple.style.top = y - 7 + "px";
			menuSimple.style.display = "block";

			if (popupFix == null) {
				popupFix = document.getElementById("popupfix");
			}
			popupFix.style.display = "none";
			
			object.style.visibility = "hidden";
			
			activeMenu = menuName;
			activeOption = object;
			
			var msCW = (menuSimple.clientWidth == 0) ? menuSimple.offsetWidth : menuSimple.clientWidth;
			var msCH = (menuSimple.clientHeight == 0) ? menuSimple.offsetHeight : menuSimple.clientHeight;
			
			leftOff = x;
			rightOff = x + msCW;
			topOff = y - 7;
			bottomOff = y - 7 + msCH;
			
		}
	} else {
		menuHead.style.display = "none";
		menuBody.style.display = "none";
		menuSimple.style.display = "none";
		
		if (popupFix == null) {
			popupFix = document.getElementById("popupfix");
		}
		popupFix.style.display = "none";
				
		object.style.visibility = "visible";		

		activeMenu = null;
		activeOption = null;
		leftOff = 0;
		rightOff = 0;
		topOff = 0;
		bottomOff = 0;
	}
}

function getX(object){
	var x = 0;
		
	if(object != document){
		x = object.offsetLeft;	
	}
	
	if(object.offsetParent){
		var dx = getX(object.offsetParent);
		if(dx)
			x += dx;
	}
	
	return x;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}


function getY(object){
	var y = 0;
	
	if(object != document){
		y = object.offsetTop;
	
		if(object.offsetParent){		
			var dy = getY(object.offsetParent);
			if(dy)
				y += dy;
		}
	}
	
	return y;
}

function switchSectionDisplay(sectionImg, subsection) {

	if(subsection.style.display == "none"){
		subsection.style.display = "block";
		sectionImg.src = "/common/images/bullet_col.png";
	} else {
		subsection.style.display = "none";
		sectionImg.src = "/common/images/bullet_exp.png";
	}
}
