// Variables for External Settings
var itemMenuWidth;
var bckColor;
var bckColorMouse;
var leftMenuY;
var rowHeight;
var pushX;
var pushY;
var color;
var colorMouse;
var leftMenuWidth;
var distance = 0;

// Variables for Internal Settings
var menuSpot;
var leftArrayName;
var leftArrayNames;
var leftItemMenuDivs = new Array();
var leftSubMenuDivs = new Array();
var leftMenuTimeoutID;
var iLastRow;

function vMenu(leftArray) {
	getExternalSettings(leftArray);
//	body = document.getElementsByTagName('body')[0];
	menuSpot = getEl('menuSpot');
	leftArrayName = leftArray + "1";
	leftArrayNames = new Array();
	getArrayNames(leftArrayName);
	// Settings done. Go create horizontal menu!

	doLeftMenu(leftArrayName);
	for (var i = 0; i < leftArrayNames.length; i++) {
		var aName = leftArrayNames[i];
		if (aName.length > leftArrayName.length)
			doLeftSubMenu(aName);
	}
}

function getExternalSettings(aName) {
//	var theArray = eval("window." + aName);
	var theArray = window[aName];

	itemMenuWidth = theArray[0];
	bckColor = theArray[1];
	bckColorMouse = theArray[2];
	leftMenuY = theArray[3];
	rowHeight = theArray[4];
	pushX = theArray[5];
	pushY = theArray[6];
	color = theArray[7];
	colorMouse = theArray[8];
	leftMenuWidth = itemMenuWidth + 1;
}

function getArrayNames(aName) {
	leftArrayNames[leftArrayNames.length] = aName;
	var theName = aName;
	var theArray = window[aName];

	for (var i = 1; i < theArray.length; i++) {
		if (theArray[i][2] == 1) {
			var newName = theName + "_" + i;
			if (confirmArray(newName)) getArrayNames(newName);
		}
	}
}

function doLeftMenu(aName) {
	var theDiv = document.createElement('div');
	theDiv.style.position = 'absolute';
	theDiv.style.width = leftMenuWidth + 'px';
	theDiv.id = aName;
	theDiv.className = 'leftMenu';
	menuSpot.appendChild(theDiv);

	var iTop = 0;
	iLastRow = 0;
	var theArray = window[aName];
	for (var i = 1; i < theArray.length; i++) {
		if (i == (theArray.length - 1)) iLastRow = 1;
		leftItemMenuDivs[leftItemMenuDivs.length] = aName + "." + i;
		var theItemDiv = getTheItemDiv('left', 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 doLeftSubMenu(aName) {
	leftSubMenuDivs[leftSubMenuDivs.length] = aName;
	var theID = changeLastUnderscore(aName);
	var theE = document.getElementById(theID);
	var subMenuX = findPosX(theE) + pushX;
	var subMenuY = findPosY(theE) + theE.offsetHeight + pushY - 1;
	var theArray = window[aName];
	var theHeight = (((theArray.length - 1) * rowHeight) - 1);

	var theDiv = document.createElement('div');
	theDiv.style.position = 'absolute';
	theDiv.style.left = subMenuX + 'px';
	theDiv.style.top = subMenuY + 'px';
	theDiv.style.width = itemMenuWidth + 1 + 'px';
	theDiv.style.backgroundColor = '#a00101'; // Red Error color!
	theDiv.style.height = theHeight + 2 + 'px';
	theDiv.style.borderWidth = '0px';
	theDiv.style.visibility = 'hidden';
	theDiv.id = aName;
	theDiv.className = 'leftSubMenu';
	menuSpot.appendChild(theDiv);

	var iTop = 0;
	iLastRow = 0;
	for (var i = 1; i < theArray.length; i++) {
		if (i == (theArray.length - 1)) iLastRow = 1;
		leftItemMenuDivs[leftItemMenuDivs.length] = aName + "." + i;
		var theItemDiv = getTheItemDiv('sub', 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 = itemMenuWidth - 11 + 'px';
			thePointerImage.style.top = '3px';
			theItemDiv.appendChild(thePointerImage);
		}
		iTop += rowHeight;
	}
}

function getTheItemDiv(strType, iLeftTop, aName, i) {
	var theItemDiv = document.createElement('div');
	theItemDiv.style.position = 'absolute';
	theItemDiv.style.top = iLeftTop + distance + 'px';
	distance = distance + 9;
	theItemDiv.style.width = itemMenuWidth - 2 + 'px';
	theItemDiv.style.paddingLeft = '5px';
	theItemDiv.style.height = rowHeight - iLastRow + 'px';
	theItemDiv.style.color = color;
	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.style.visibility = 'Inherit';
	theItemDiv.id = aName + "." + i;
	theItemDiv.className = 'leftItemMenu';
	return theItemDiv;
}

function confirmArray(aName) {
	if ((typeof window[aName] == 'object') && (aName.length > 1))
		return true;
	return false;
}

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;
}
