// Variables for External Settings
var itemMenuWidth;
var bckColor;
var bckColorMouse;
var topMenuY;
var rowHeight;
var pushX;
var pushY;
var color;
var colorMouse;

// Variables for Internal Settings
var menuSpot;
var topArrayName;
var topArrayNames;
var topItemMenuDivs = new Array();
var topSubMenuDivs = new Array();
var topMenuTimeoutID;
var topMenuWidth; // Calculated on external settings
var topMenuX; // Calculated on external settings
var iLastRow;

function hMenu(topArray) {
	getExternalSettings(topArray);
	menuSpot = getEl('menuSpot');
	topArrayName = topArray + '1';
	topArrayNames = new Array();
	getArrayNames(topArrayName);
	var theArray = window[topArrayName];
	topMenuWidth = (itemMenuWidth * 5);
	topMenuX = 0;
	// Settings done. Go create horizontal menu!

	doTopMenu(topArrayName);
	for (var i = 0; i < topArrayNames.length; i++) {
		var aName = topArrayNames[i];
		if (aName.length > topArrayName.length)
			doTopSubMenu(aName);
	}
}

function getExternalSettings(aName) {
	var theArray = window[aName];

	itemMenuWidth = theArray[0];
	bckColor = theArray[1];
	bckColorMouse = theArray[2];
	topMenuY = theArray[3];
	rowHeight = theArray[4];
	pushX = theArray[5];
	pushY = theArray[6];
	color = theArray[7];
	colorMouse = theArray[8];
}

function getArrayNames(aName) {
	topArrayNames[topArrayNames.length] = aName;
	var theName = aName;
	var theArray = window[aName];

	for (var i = 1; i < theArray.length; i++) {
		if (theArray[i][3] == 1) {
			var newName = theName + '_' + i;
			if (confirmArray(newName)) getArrayNames(newName);
		}
	}
}

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];
	for (var i = 1; i < theArray.length; i++) {
		topItemMenuDivs[topItemMenuDivs.length] = aName + '.' + i;
		var theItemDiv = getTheItemDiv('top', iLeft, aName, theArray[i][2], i);
		theItemDiv.style.cursor = (theArray[i][1] == '') ? 'default' : 'pointer';
		var theTextNode = document.createTextNode(theArray[i][0]);
		theItemDiv.appendChild(theTextNode);
		theDiv.appendChild(theItemDiv);
		iLeft += itemMenuWidth;
	}
}

function doTopSubMenu(aName) {
	topSubMenuDivs[topSubMenuDivs.length] = aName;
	var theID = changeLastUnderscore(aName);
	var theE = getEl(theID);
	var subMenuX = findPosX(theE) - getEl('wrapper').offsetLeft;
	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 == topArrayName.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.width = itemMenuWidth + 1 + 'px';
	theDiv.style.textAlign = 'center';
	theDiv.style.color = color;
	theDiv.style.backgroundColor = '#510d94'; // Blue Overcolor!
	theDiv.style.height = theHeight + 2 + 'px';
	theDiv.style.borderWidth = '0px';
	theDiv.style.overflow = 'hidden';
	theDiv.style.visibility = 'hidden';
	theDiv.id = aName;
	theDiv.className = 'topSubMenu';
	menuSpot.appendChild(theDiv);

	var iTop = 0;
	iLastRow = 0;
	for (var i = 1; i < theArray.length; i++) {
		if (i == (theArray.length - 1)) iLastRow = 1;
		topItemMenuDivs[topItemMenuDivs.length] = aName + '.' + i;
		var theItemDiv = getTheItemDiv('sub', iTop, aName, theArray[i][2], 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][3] == '1') {
			var thePointerImage = document.createElement('img');
			thePointerImage.src = 'Images/Layout/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, strHeadline, i) {
	var theItemDiv = document.createElement('div');
	theItemDiv.style.position = 'absolute';
	theItemDiv.style.left = (strType == 'top') ? iLeftTop + 'px' : 0 + 'px';
	theItemDiv.style.top = (strType == 'top') ? '0px' : iLeftTop + 'px';
	theItemDiv.style.width = itemMenuWidth - 2 + 'px';
	theItemDiv.style.paddingLeft = '1px';
	theItemDiv.style.textAlign = 'center';
	theItemDiv.style.height = rowHeight - iLastRow + 'px';
	theItemDiv.style.color = color;
	theItemDiv.style.backgroundImage = (strType == 'top') ? 'url(Images/Layout/Menu19.jpg)' : 'url(Images/Layout/Menu18.jpg)';
	theItemDiv.style.backgroundPosition = 'top left';
	theItemDiv.style.backgroundAttachment = 'scroll';
	theItemDiv.style.border = '1px solid #340032';
	theItemDiv.style.overflow = 'hidden';
	theItemDiv.style.visibility = (strType == 'top') ? 'visible' : 'inherit';
	theItemDiv.headline = strHeadline;
	theItemDiv.id = aName + '.' + i;
	theItemDiv.className = 'topItemMenu';
	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;
}

