var theContent = new Array();
theContent[0] = new Array();
theContent[1] = new Array();
var lastPopped = new Array();
var maxLevel = 7;
var lastLevel = 1;
var levelCount = 1;
if (is.ie4up) document.layers = document.all;

function buildContent(subMenu,level, homeURL, homename) {
	var nextLevel = (level+1);
	var popup = "";
	var specialeffect = "";
	var theClass = 'globalnav';
	if (subMenu < 2000000) {
		theContent[0][subMenu] = menustart;
		theContent[1][subMenu] = menustart;
		theClass = 'subnav';
		finalmenuend=menuend;
	} else {
		theClass = 'globalnav';
		finalmenuend=globalmenuend;
		//alert('setting the class to globalnav');
		if (subMenu < 2000010) {
			theContent[0][subMenu] = globalmenustart[2000000];
			theContent[1][subMenu] = globalmenustart[2000000];
		} else {
			theContent[0][subMenu] = globalmenustart[2000010];
			theContent[1][subMenu] = globalmenustart[2000010];
		}
	}
	
	//alert(homename);
	// first the homebutton of a submenu
	if (is.ie4up && subMenu <2000000) {
		specialeffect = 'onmouseover="itemOver(this);" onmouseout="itemOut(this);"';
	} else {
		specialeffect = "";
	}
	if (subMenu < extraOptionsID) {
		theContent[0][subMenu] += '<tr onclick="location.href=\'' + homeURL + '\'"' + specialeffect + '>';
		theContent[1][subMenu] += '<tr onclick="location.href=\'' + homeURL + '\'"' + specialeffect + '>';

	//homebutton of a submenu
		popup = "";
		theContent[0][subMenu] += '<td width="136" class="subnav" bgcolor="#e9e9e9" valign="bottom"><a valign="bottom" class="' + theClass + '" href="' + homeURL + '" onmouseover="' + popup + '"><b>' + homename +  '</b></a></td>';
		theContent[1][subMenu] += '<td width="136" class="subnav" bgcolor="#e9e9e9" valign="bottom"><a valign="bottom" class="' + theClass + '" href="' + homeURL + '" onmouseover="' + popup + '"><b>' + homename +  '</b></a></td>';
		theContent[0][subMenu] += '<td width="16" class="subnav" bgcolor="#e9e9e9" valign="bottom" background="/general/images/navigation/vert_curb.gif"><img class="subnav" src="' + spacer + '" height="15" width="1" alt="" border="0" /></td></tr>';
		theContent[1][subMenu] += '<td width="16" class="subnav" bgcolor="#e9e9e9" valign="bottom" background="/general/images/navigation/vert_curb.gif"><img class="subnav" src="' + spacer + '" height="15" width="1" alt="" border="0" /></td></tr>';
		//theContent[subMenu] += '<td class="subnav" bgcolor="#e9e9e9"><img class="subnav" src="' + spacer + '" height="1" width="1" alt="" border="0" /></td></tr><tr><td height="1" class="subnav" colspan="2"><img class="subnav" src="/general/images/navigation/pixel_blue.gif" width="144" height="1" border="0" alt="" /></td></tr>';
	}

	//submenu content
	for (var i in menus[subMenu]) {
		popup = (menus[subMenu][i][2]) ? 'popUp(\'' + menus[subMenu][i][2] + '\',' + nextLevel + ',event,document.subnav_' + level + '_' + subMenu + '_' + i + '.y);' : (nextLevel<=maxLevel) ? 'hideSubMenu(' + nextLevel + ');' : '';
		popup1 = 'hideSubMenu(' + nextLevel + ');'
		if (is.ie4up && subMenu <2000000) {
			specialeffect = ' onmouseover="'+ popup + ';itemOver(this);" onmouseout="itemOut(this);"';
			specialeffect1 = ' onmouseover="'+ popup1 + ';itemOver(this);" onmouseout="itemOut(this);"';
		} else {
			specialeffect = "";
			specialeffect1 = "";
		}
		theContent[0][subMenu] += '<tr onclick="location.href=\'' + menus[subMenu][i][1] + '\'"' + specialeffect + '>';
		theContent[1][subMenu] += '<tr onclick="location.href=\'' + menus[subMenu][i][1] + '\'"' + specialeffect1 + '>';
		theContent[0][subMenu] += '<td width="136" class="subnav"><a class="' + theClass + '" href="' + menus[subMenu][i][1] + '" onmouseover="' + popup + '">' + menus[subMenu][i][0] + '</a>';
		theContent[1][subMenu] += '<td width="136" class="subnav"><a class="' + theClass + '" href="' + menus[subMenu][i][1] + '" onmouseover="' + popup1 + '">' + menus[subMenu][i][0] + '</a>';

		//2nd td (arrow)
		if (menus[subMenu][i][2] && nextLevel!=(maxLevel)) {
			//arrow submenu
			theContent[0][subMenu] += '</td><td background="/general/images/navigation/vert_curb.gif" width="16" class="subnav"><a href="doNothing();" onmouseover="' + popup + '"><img name="subnav_' + level + '_' + subMenu + '_' + i + '" class="subnav" src="' + subIndicator + '" vspace="4" hspace="4" width="4" height="6" border="0" alt="" /></a></td>';
			theContent[1][subMenu] += '</td><td background="/general/images/navigation/vert_curb.gif" width="16" class="subnav"><img src="' + spacer + '" vspace="4" hspace="4" width="4" height="6" border="0" alt="" /></td>';
			buildContent(menus[subMenu][i][2],nextLevel,menus[subMenu][i][1],menus[subMenu][i][0]);
		} else {
			theContent[0][subMenu] += '</td><td background="/general/images/navigation/vert_curb.gif" width="16"><img class="subnav" src="' + spacer + '" height="1" width="16" alt="" border="0" /></td>';
			theContent[1][subMenu] += '</td><td background="/general/images/navigation/vert_curb.gif" width="16"><img class="subnav" src="' + spacer + '" height="1" width="16" alt="" border="0" /></td>';
		}
		theContent[0][subMenu] += '</tr>\n';
		theContent[1][subMenu] += '</tr>\n';
	}
	theContent[0][subMenu] += finalmenuend;
	theContent[1][subMenu] += finalmenuend;
	//if (subMenu == 1840) document.write('<xmp>' + theContent[0][subMenu] + '</xmp>');
	//document.write('<xmp>' + theContent[0][subMenu] + '</xmp>');
	//document.write(theContent[0][subMenu]);

}
//alert();

function itemOver(tr) {
	if (is.ie4up) {
		tr.style.backgroundColor = bgColorHover;
		tr.style.cursor = 'hand';
	}
}

function itemOut(tr,kleur) {
	if (is.ie4up) {
		tr.style.backgroundColor = bgColorNormal;
		if(kleur) tr.style.backgroundColor = kleur;
	}
}

function buildMenu(id) {
	//var theMenu = mainmenuStart;/
	var nextLevel = 1;
	var theMenu = "";
	var theoptionheight;
	var first = true;
	var defaultRoot = 78;

	var theClass  = 'nav'; // make this one dependent on the menuid to get blue
	if (specialnavigation) {
		id = defaultRoot;
		menus[defaultRoot][specialsub][3] = 1;
	}

	// Menu items in the Root

	for(var i in menus[id]) {
		if (menus[id][i][2]) {
			if (menus[id][i][3]) {
				//alert (menus[id][i][0] + '\n' + nextLevel);
				buildContent(menus[id][i][2],nextLevel-1,menus[id][i][1],menus[id][i][0]);
			} else {
				//alert (menus[id][i][0]);
				buildContent(menus[id][i][2],nextLevel,menus[id][i][1],menus[id][i][0]);
			}
		}
	}

	document.write(theMenu);
	//document.write("<textarea>" + theMenu + "</textarea>");
	document.close();
}

function getRealTop(imgElem) {
	yPos = eval(imgElem).offsetTop;
	tempEl = eval(imgElem).offsetParent;
	while (tempEl != null) {
		yPos += tempEl.offsetTop;
		tempEl = tempEl.offsetParent;
	}
	//alert(yPos);
	return yPos;
}

function prepareLayer (theLayer, subMenu, level, e, position) {
	var correction = 11;
	var y = e.y;
	if (is.nav4up) {
		if (position) {
			y = position;
			if (level == 2) y = y + document.layers['level1'].top - 5;
			if (level == 3) y = y + document.layers['level1'].top - 5;
			if (level == 4) y = y + document.layers['level1'].top - 5;
			theLayer.top = y-1;
		} else {
			if (level == 2) y = y + document.layers['level1'].top;
			if (level == 3) y = y + document.layers['level1'].top;
			if (level == 4) y = y + document.layers['level1'].top;
			theLayer.top = y - (y % correction);
		}

		if (subMenu <2000000) {
			theLayer.left = lefts[level] - 2;
		} else {
			// global menu
			//var pos = getAbsolutePosition(
			if (subMenu == '2000000' || subMenu == '2000010' || subMenu == '2000020') {
				theLayer.left = document.header01.x
				theLayer.top = document.header01.y + document.header01.height -1 ;
				//alert(theLayer.top);
			}
			if (subMenu >2000000 && subMenu <=2000010) {
				theLayer.left = document.header01.x + document.header01.width;
			}
			if (subMenu >2000010 && subMenu <=2000020) {
				theLayer.left = document.header02.x + document.header02.width;
			}
			if (subMenu >2000020) {
				theLayer.left = document.header03.x + document.header03.width;
			}
		}
		theLayer.document.open();
		if (levelCount < 2) {
			theLayer.document.write(theContent[0][subMenu]);
		} else {
			theLayer.document.write(theContent[1][subMenu]);
		}
		theLayer.document.close();
	} else {
		if (e.toElement) {
			theLayer.style.top = (navigator.userAgent.indexOf("Mac")==-1) ? getRealTop(e.toElement)-1 : e.y - (e.y % 8);
		} else {
			theLayer.style.top = e.y - (e.y % correction);
		}
		if (subMenu <2000000) {
			theLayer.style.left = lefts[level]+6;
		} else {
			// global menu
			// var pos = getAbsolutePosition(
			if (subMenu == 2000000 || subMenu == 2000010 || subMenu == 2000020) {
				theLayer.style.left = pointLeft;
				theLayer.style.top = 78;
			}
			if (subMenu >2000000 && subMenu <=2000010) {
				theLayer.style.left = pointLeft +150;
			}
			if (subMenu >2000010 && subMenu <=2000020) {
				theLayer.style.left = pointLeft +380;
			}
			if (subMenu >2000020) {
				theLayer.style.left = pointLeft +610;
			}
		}

		if (levelCount < 2) {
			theLayer.innerHTML = theContent[0][subMenu];
		} else {
			theLayer.innerHTML = theContent[1][subMenu];
		}
	}
	lastPopped[level] = subMenu;
}

function isVisible(theObject) {
	if (is.nav4up) {
		if (theObject) return theObject.visibility == 'show';
	} else {
		if (theObject) return theObject.style.visibility == 'visible';
	}
}

function popUp(subMenu,level,e,position) {
	if (level == 1) levelCount = 1;
	if (level<=maxLevel) {
		var theLayer = document.layers['level' + level];
		if (lastPopped[level] == subMenu) {
			if (isVisible(theLayer)) return;
		} else {
			//alert('preparing the layer');
			if (level > lastLevel) levelCount++;
			if (level < lastLevel) levelCount--;
			if (levelCount == 0) levelCount = 1;
			prepareLayer(theLayer, subMenu, level, e, position);
		}
		//alert('popping up ' + subMenu);
		showSubMenu(level,1);
	} else {
		hideSubMenu(level);
	}
	lastLevel = level;
}

function removeMeIE(level, e) {
	//alert("this is RemoveMe in IE");
	if (e) {
		if (e.toElement.className.indexOf('nav') == -1) {
			//alert('removing ' + level);
			removeMe(level);
		}
	}
}
function removeMe(level) {
	if (level == 1) {
		//alert ('level is 1');
		if ((!(isVisible(document.layers['level2']))) && (!(isVisible(document.layers['level3']))) && (!(isVisible(document.layers['level4'])))) hideSubMenu(level);
	}
	if (level == 2) {
		//alert ('level is 2');
		if ((!(isVisible(document.layers['level3']))) && (!(isVisible(document.layers['level4'])))) hideSubMenu(level);
	}
	if (level == 3) {
		//alert ('level is 3');
		if ((!(isVisible(document.layers['level4']))) && (!(isVisible(document.layers['level5'])))) hideSubMenu(level);
	}
	if (level == 4) {
		//alert ('level is 4');
		if ((!(isVisible(document.layers['level5'])))) hideSubMenu(level);
	}
	if (level == 5) {
		//alert ('level is 4');
		if ((!(isVisible(document.layers['level6'])))) hideSubMenu(level);
	}
	if (level == 6) {
		//alert ('level is 4');
		if ((!(isVisible(document.layers['level7'])))) hideSubMenu(level);
	}
	if (level > 7) {
		//alert ('level is not 1,2,3,4,5 or 6 but ' + level);
		hideSubMenu(level);
	}
}

function hideSubMenu(level) {
	var nextLevel = (level + 1);
	if (document.layers['level' + nextLevel]) hideSubMenu(nextLevel);
	showSubMenu(level,0);
}

function showSubMenu(level,show) {
	// Check whether the requested layer exists. If not, don't worry, the script won't try to display it.
	if (is.ie4up) {
		if (document.all('level' + level)) document.all('level' + level).style.visibility = (show) ? "visible" : "hidden";
	}
	if (is.nav4up) {
		if (document.layers['level' + level]) document.layers['level' + level].visibility = (show) ? "show" : "hide";
	}

	if (level<maxLevel) {
		level++;
		if (document.layers['level' + level]) hideSubMenu(level);
	}
}

function showallattributes(object) {
	var str = "";
	for (var i in object){
		if (i.indexOf('HTML') == -1) str += i + "=" + object[i] + "; ";
	}
	alert(str);
}