var menuSpot;
var startArrayName;
var topMenuWidth;
var itemMenuWidth;
var bckColor;
var bckColorMouse;
var arrayNames;
var topItemMenuDivs = new Array();
var subMenuDivs = new Array();
var leftItemMenuDivs = new Array();
var topMenuX;
var topMenuY;
var rowHeight = 16;
var pushX = 156;
var pushY = -14;
var iLastRow;

function startMenu(startArray, strTopLeft, topMW, iMW, bckC, bckCM) {
	startArrayName = startArray;
	topMenuWidth = topMW;
	itemMenuWidth = iMW;
	bckColor = bckC;
	bckColorMouse = bckCM;
	arrayNames = new Array();
	var aName;
	menuSpot = (strTopLeft == 'topMenu') ? a1 : a2;
//	topMenuX = centerElement() - (topMenuWidth/2);
	topMenuX = 0;
	topMenuY = (strTopLeft == 'topMenu') ? 16 : 80;

	getArrayNames(startArray);
	if (strTopLeft == 'topMenu') {
		doTopMenu(startArray);
	} else {
		doLeftMenu(startArray);
	}
	var j = arrayNames.length;
	for (var i = 0; i < j; i++) {
		aName = arrayNames[i];
		if (aName.length > 7) {
			if (strTopLeft == 'topMenu')
				doTopSubMenu(aName);
			else
			 doLeftSubMenu(aName);
		}
	}
}

function getArrayNames(aName) {
	arrayNames[arrayNames.length] = aName;
	var theName = aName;
	var theArray = window[aName];

	var j = theArray.length;
	for (var i = 1; i < j; i++) {
		if (theArray[i][2] == 1) {
			var newName = theName + '_' + i;
			if (confirmArray(newName)) getArrayNames(newName);
		}
	}
}

function confirmArray(aName) {
	if ((typeof window[aName] == 'object') && (aName.length > 1))
		return true;
	return false;
}

function doTopMenu(aName) {
	var theDiv = document.createElement('div');
	theDiv.style.position = 'absolute';
	theDiv.style.left = topMenuX + 'px';
	theDiv.style.top = topMenuY + 'px';
	theDiv.style.width = topMenuWidth + 'px';
	theDiv.id = aName;
	theDiv.className = 'topMenu';
	menuSpot.appendChild(theDiv);

	var iLeft = 0;
	iLastRow = 0;
	var theArray = window[aName];
	var j = theArray.length;
	for (var i = 1; i < j; i++) {
		var theItemDiv = document.createElement('div');
		theItemDiv.style.position = 'absolute';
		theItemDiv.style.left = iLeft + 'px';
		theItemDiv.style.top = '0px';
		theItemDiv.style.width = itemMenuWidth - 2 + 'px';
		theItemDiv.style.paddingLeft = '1px';
		theItemDiv.style.height = rowHeight + 'px';
		theItemDiv.style.cursor = (theArray[i][1] == '') ? 'default' : 'pointer';
		theItemDiv.style.backgroundImage = 'url(Images/Menu17.jpg)';
		theItemDiv.style.backgroundPosition = 'top left';
		theItemDiv.style.backgroundAttachment = 'scroll';
		theItemDiv.style.border = '1px solid #000';
		theItemDiv.style.overflow = 'hidden';
		theItemDiv.style.visibility = 'visible';
		theItemDiv.id = aName + '.' + i;
		theItemDiv.className = 'topItemMenu';
		topItemMenuDivs[topItemMenuDivs.length] = aName + '.' + i;
		var theTextNode = document.createTextNode(theArray[i][0]);
		theItemDiv.appendChild(theTextNode);
		theDiv.appendChild(theItemDiv);
		iLeft += itemMenuWidth;
	}
}

function doTopSubMenu(aName) {
	subMenuDivs[subMenuDivs.length] = aName;
	var theID = changeLastUnderscore(aName);
	var theE = getEl(theID);
	var subMenuX = findPosX(theE);
	var subMenuY = findPosY(theE) + theE.offsetHeight - 1;
	var theArray = window[aName];
	var theHeight = (((theArray.length - 1) * rowHeight) - 1);

	var theDiv = document.createElement('div');
	theDiv.style.position = 'absolute';
	if (aName.length == startArrayName.length + 2) {
		theDiv.style.left = subMenuX + 'px';
		theDiv.style.top = subMenuY + 'px';
	} else {
		theDiv.style.left = subMenuX + pushX + 'px';
		theDiv.style.top = subMenuY + pushY + 'px';
	}
	theDiv.style.height = theHeight + 2 + 'px';
	theDiv.style.visibility = 'hidden';
	theDiv.id = aName;
	theDiv.className = 'topSubMenu';
	menuSpot.appendChild(theDiv);

	var iTop = 0;
	iLastRow = 0;
	var j = theArray.length;
	for (var i = 1; i < j ; i++) {
		if (i == (theArray.length - 1)) iLastRow = 1;
		var theItemDiv = getTheItemDiv('topSubMenu', iTop, aName, i);
		theItemDiv.style.cursor = (theArray[i][1] == '') ? 'default' : 'pointer';
		var theTextNode = document.createTextNode(theArray[i][0]);
		theItemDiv.appendChild(theTextNode);
		theDiv.appendChild(theItemDiv);
		if (theArray[i][2] == '1') {
			var thePointerImage = document.createElement('img');
			thePointerImage.src = 'Images/RPointer.gif';
			thePointerImage.width = 5;
			thePointerImage.height = 9;
			thePointerImage.style.position = 'absolute';
			thePointerImage.style.left = '159px';
			thePointerImage.style.top = '3px';
			theItemDiv.appendChild(thePointerImage);
		}
		iTop += rowHeight;
	}
}

function doLeftMenu(aName) {
	var theDiv = document.createElement('div');
	theDiv.style.position = 'absolute';
	theDiv.style.left = ((winWidth() / 2) + 320) + 'px';
	theDiv.style.top = (hatRight.offsetTop + 106) + 'px';
	theDiv.style.width = itemMenuWidth + 'px';
	theDiv.id = aName;
	theDiv.className = 'leftMenu';
	theDiv.style.visibility = 'hidden';
	var theArray = window[aName];
	var theHeight = ((theArray.length - 1) * rowHeight);
	theDiv.style.height = theHeight + 2 + 'px';
	menuSpot.appendChild(theDiv);

	var iTop = 0;
	iLastRow = 0;
	var j = theArray.length;
	for (var i = 1; i < j; i++) {
		if (i == (theArray.length - 1)) iLastRow = 1;
		var theItemDiv = document.createElement('div');
		var theItemDiv = getTheItemDiv('leftMenu', iTop, aName, i);
		theItemDiv.style.cursor = (theArray[i][1] == '') ? 'default' : 'pointer';
		var theTextNode = document.createTextNode(theArray[i][0]);
		theItemDiv.appendChild(theTextNode);
		theDiv.appendChild(theItemDiv);
		iTop += rowHeight;
	}
}

function getTheItemDiv(strType, iTop, aName, i) {
	var theItemDiv = document.createElement('div');
	theItemDiv.style.position = 'absolute';
	theItemDiv.style.left = 0 + 'px';
	theItemDiv.style.top = iTop + 'px';
	theItemDiv.style.width = itemMenuWidth - 2 + 'px';
	theItemDiv.style.paddingLeft = '1px';
	theItemDiv.style.height = rowHeight - iLastRow + 'px';
	theItemDiv.style.visibility = 'inherit';
	theItemDiv.style.backgroundImage = 'url(Images/Menu16.jpg)';
	theItemDiv.style.backgroundPosition = 'top left';
	theItemDiv.style.backgroundAttachment = 'scroll';
	theItemDiv.style.border = '1px solid #000';
	theItemDiv.style.overflow = 'hidden';
	theItemDiv.id = aName + '.' + i;
	if (strType == 'topSubMenu') {
		theItemDiv.className = 'topItemMenu';
		topItemMenuDivs[topItemMenuDivs.length] = aName + '.' + i;
	} else {
		theItemDiv.className = 'leftItemMenu';
		leftItemMenuDivs[leftItemMenuDivs.length] = aName + '.' + i;
	}
	return theItemDiv;
}

function changeLastUnderscore(strName) {
	var thePos = getLastUnderscore(strName);
	return strName.substring(0,thePos) + '.' + strName.substring(thePos+1,strName.length)
}

function getLastUnderscore(theString) {
	for (var i = theString.length; i > 1; i--) {
		if (theString.charAt(i) == '_') return i;
	}
	return 0;
}

