// *******************************************************************************
// This is the script for controlling the menus in the game.
// *******************************************************************************
var diagOn = 0;
var newWin;

var NN = (navigator.appName.indexOf("Netscape") != -1);
var IE = (navigator.appName.indexOf("Microsoft") != -1);
var MC = (navigator.userAgent.indexOf("Mac") < 0);
var UX = (navigator.userAgent.indexOf("X11") < 0);

function AutoGo() {
	if (newWin) {
		if (diagOn == 1) {
			newWin.close();
			diagOn = 0;
		}
	}
	if (IE) menuState.off();
	for (i = 0; i < menuNames.length; i++) {
		(NN) ? document.layers[menuNames[i]].hide() : document.all[menuNames[i]].hide();
	}
	if (this.url && this.url != 'ThisItemIsTurnedOff') {
		location.href = this.url;
	}
	menuState = false;
}

var itemOutColor = "#CCCCCC";
var itemOverColor = "#AAAAAA";
var buttonOverColor = "#FFFFFF";
var buttonOutColor = "#CCCCCC";
var buttonDownColor = "#999999";
var menuLightColor = "#FFFFFF";
var menuDarkColor = "#999999";
var fontOnColor = "#000000";
var fontOffColor = "#999999";
var menuNames = new Array('Game','Help');
var menuState = false;

function makeIE(menuName,menuWidth,isChild,parentMenu,parentItemNum) {
	var menuX,menuY;
	var menuLayer = "";
	var startLeft = 5;
	var startTop = 3;
	if (!isChild) {
		markerImg = eval("document.all." + menuName + "Img");
		container = markerImg.offsetParent;
		while (container) {
			startLeft += container.offsetLeft - 1;
			startTop += container.offsetTop - 1;
			container = container.offsetParent;
		}
	} else {
		parentItem = document.all[parentMenu + parentItemNum];
		parentMenu = document.all[parentMenu];
	}
	if (!isChild) {
		menuLayer += '<DIV ID="' + menuName + 'button" ';
		menuLayer += 'STYLE="position:absolute; left:' + startLeft + '; top:' + startTop + '; width:' + markerImg.width + 'px;';
		menuLayer += 'background-color:' + buttonOutColor + '; visibility:visible;">';
		menuLayer += '<IMG SRC="' + markerImg.src + '" WIDTH=' + markerImg.width + ' ';
		menuLayer += 'HEIGHT=' + markerImg.height + '></DIV>';
	}	
	if (isChild) {
		menuX = parseInt(parentMenu.style.left) + parseInt(parentMenu.style.width);
		menuY = parentItem.offsetTop + parseInt(parentMenu.style.top);
		menuY = parentItem.offsetTop + parseInt(parentMenu.style.top) + (4 * parentItemNum);
	} else {
		menuX = startLeft - 2;
		menuY = startTop + 24;
	}
	menuLayer += '<DIV ID="' + menuName + '" STYLE="position:absolute; visibility:hidden; width:' + menuWidth + '; ';
	menuLayer += 'background-color:' + itemOutColor + '; left:' + menuX + '; top:' + menuY + ';">';
	var itemCount = 0;
	while (menuData[menuName][itemCount][0]) {
		var item = menuData[menuName][itemCount];
		var itemWidth = menuWidth -4;
		var itemLayer = "";
		if (item[0] == 'spacer') {
			itemLayer += '<DIV ID="' + menuName + itemCount + '" STYLE="position:relative; ';
			itemLayer += 'visibility:inherit; width:' + itemWidth + '; ';
			itemLayer += 'background-color:' + itemOutColor + ';">';
			itemLayer += '<img src="c.gif" WIDTH=' + itemWidth + ' HEIGHT=2></DIV>';
		} else {
			var itemFontColor = (item[2] || (item[1] != 'ThisItemIsTurnedOff')) ? fontOnColor : fontOffColor;
			var childArrow = (item[2]) ? '<IMG SRC="/childarrow.gif" WIDTH=14 HEIGHT=14 ALIGN=RIGHT>' : '';
			itemLayer += '<DIV ID="' + menuName + itemCount + '" STYLE="position:relative; ';
			itemLayer += 'left:2; visibility:inherit; width:' + itemWidth + '; ';
			itemLayer += 'background-color:' + itemOutColor + '; ';
			itemLayer += 'font-family: arial,helvetica,sans-serif; font-size:10pt;">';
			itemLayer += childArrow + '<FONT SIZE=2 COLOR="' + itemFontColor + '">' + item[0] + '</FONT></DIV>';
		}
		itemCount++;
		menuLayer += itemLayer;
	}
	menuLayer += '<DIV ID="' + menuName + 'pad" HEIGHT=2></DIV>';
	menuLayer += '</DIV>';
	document.write(menuLayer);
}

function makeNN(menuName,menuWidth,isChild,parentMenu,parentItemNum) {
	var menuLayer = "";
	if (isChild) {
		parentItem = document.layers[parentMenu].document.layers[parentMenu + parentItemNum];
		parentMenu = document.layers[parentMenu];
	} else {
		markerImg = eval("document.images." + menuName + "Img");
	}
	if (!isChild) {
		menuLayer += '<LAYER ID="' + menuName + 'button" LEFT=' + markerImg.x + ' TOP=' + markerImg.y + ' ';
		menuLayer += 'BGCOLOR="' + buttonOutColor + '" WIDTH=' + markerImg.width + ' HEIGHT=' + markerImg.height + ' ';
		menuLayer += 'CLIP="-2,-2,' + (markerImg.width + 2) + ',' + (markerImg.height + 2) + '">';
		menuLayer += '<IMG SRC="' + markerImg.src + '" WIDTH=' + markerImg.width + ' ';
		menuLayer += 'HEIGHT=' + markerImg.height + '></LAYER>';
	}
	var menuX = (!isChild) ? markerImg.x - 2 : parentMenu.left + parentMenu.clip.right;
	var menuY = (!isChild) ? markerImg.y + markerImg.height + 3: parentMenu.top + parentItem.top;
	menuLayer += '<LAYER ID="' + menuName + '" BGCOLOR="' + itemOverColor + '" ';
	menuLayer += 'WIDTH=' + menuWidth + ' LEFT=' + menuX + ' TOP=' + menuY + ' VISIBILITY=HIDDEN>';
	var itemTop = 2;
	var itemCount = 0;
	while (menuData[menuName][itemCount][0]) {
		var item = menuData[menuName][itemCount];
		var itemWidth = menuWidth - 4;
		var clipVals;
		var itemLayer = "";
		if (item[0] == 'spacer') {
			clipVals = '0,0,' + itemWidth + ',2';
			itemLayer += '<LAYER ID="' + menuName + itemCount + '" TOP=' + itemTop + ' ';
			itemLayer += 'CLIP="' + clipVals + '" HEIGHT=2 LEFT=2 BGCOLOR="' + itemOutColor + '">';
			itemLayer += '<img src="c.gif" WIDTH=' + itemWidth + ' HEIGHT=2></LAYER>';
			itemTop += 2;
		} else {
			var itemFontColor = (item[2] || (item[1] != 'ThisItemIsTurnedOff')) ? fontOnColor : fontOffColor;
			var childArrow = (item[2]) ? '<IMG SRC="/childarrow.gif" WIDTH=14 HEIGHT=14 ALIGN=RIGHT>' : '';
			clipVals = '0,0,' + itemWidth + ',20';
			itemLayer += '<LAYER ID="' + menuName + itemCount + '" BGCOLOR="' + itemOutColor + '" ';
			itemLayer += 'WIDTH=' + itemWidth + ' TOP=' + itemTop + ' CLIP="' + clipVals + '" ';
			itemLayer += 'LEFT=2 HEIGHT=20 CLASS="item">' + childArrow;
			itemLayer += '<FONT SIZE=2 COLOR="' + itemFontColor + '">&nbsp;' + item[0] + '</FONT></LAYER>';
			itemLayer += '<LAYER ID="' + menuName + itemCount + 'cover" HEIGHT=20 LEFT=3';
			itemLayer += 'WIDTH=' + menuWidth + ' TOP=' + itemTop + ' CLIP="' + clipVals + '"></LAYER>';
			itemTop += 20;
		}
		itemCount++;
		menuLayer += itemLayer
	}
	menuLayer += '<LAYER ID="' + menuName + 'pad" TOP=' + itemTop + ' HEIGHT=2></LAYER>';
	menuLayer += '</LAYER>';
	document.write(menuLayer);
}

function captureClicks() {
	if (NN) {
		window.captureEvents(Event.MOUSEDOWN);
		window.onmousedown = menusOff;
	} else if (IE) {
		document.onmousedown = doNothingIE;
		document.onmousemove = doNothingIE;
		document.onmouseup = redirIEClick;
	}
}

function doNothingIE() {
	return false;
}

function redirIEClick() {
	document.onmousedown = menusOff;
	document.onmouseup = "default";
	document.onmousemove = "default";
}

function menusOff(evt) {
	var buttonClick = false;
	if (NN) {
		var buttonL = menuState.left;
		var buttonT = menuState.top;
		var buttonR = menuState.left + menuState.clip.right;
		var buttonB = menuState.top + menuState.clip.bottom;
		var clickX = evt.pageX;
		var clickY = evt.pageY;
		if ((clickX > buttonL) && (clickX < buttonR) && (clickY > buttonT) && (clickY < buttonB)) {
			buttonClick++;
		}
	} else {
	//	document.onmousedown = "default";
		if (event.srcElement.id.indexOf('button') > 0) {
			buttonClick++;
		}
	}
	if (buttonClick && menuState) {
		menuState.menu.hide();
		(NN) ? menuState.bgColor = buttonOverColor : menuState.up();
	} else if (menuState) {
		(NN) ? menuState.bgColor = buttonOutColor : menuState.off();
		for (i = 0; i < menuNames.length; i++) {
			(NN) ? document.layers[menuNames[i]].hide() : document.all[menuNames[i]].hide();
		}
	}
	(NN) ? window.releaseEvents(Event.MOUSEDOWN) : document.onmousedown = "default";
	menuState = false;
}

function buttonOver() {
	if (menuState) {
		(NN) ? this.bgColor = buttonDownColor : this.down();
		this.menu.show();
		menuState = this;
		for (name in menuNames) {
			if (menuNames[name] == this.menu.id) continue;
			hideIt(menuNames[name]);
		}
		captureClicks();
	} else {
		(NN) ? this.bgColor = buttonOverColor : this.up();
	}
	if (menuState) {
		for (name in menuNames) {
			if (menuNames[name].charAt(0) != 'c') {
				button = (NN) ? document.layers[menuNames[name] + 'button'] : document.all[menuNames[name] + 'button'];
				if (button == menuState) continue;
				(NN) ? button.bgColor = buttonOutColor : button.off();
			}
		}
	}
}

function buttonOut() {
	if (!menuState) {
		(NN) ? this.bgColor = buttonOutColor : this.off();
	}
}

function buttonClick() {
	if (!menuState) {
		(NN) ? this.bgColor = buttonDownColor : this.down();
		this.menu.show();
		menuState = this;
		captureClicks();
	} else if (menuState == this) {
		(NN) ? this.bgColor = buttonOverColor : this.up();
		this.menu.hide();
		menuState = false;
	}
}

function itemOver() {
	var item = (NN) ? this.item : this;
	(NN) ? item.bgColor = itemOverColor : item.style.backgroundColor = itemOverColor;
//	if (IE) item.style.cursor = "default";
	if (this.menu.activeChild) {
		this.menu.activeChild.hide();
	}
	if (item.child) {
		this.menu.activeChild = item.child;
		item.child.show();
	}
}

function itemOut() {
	(NN) ? this.item.bgColor = itemOutColor : this.style.backgroundColor = itemOutColor;
}

function showIt(lyr) {
	var obj = (this.id) ? this : ((NN) ? document.layers[lyr] : document.all[lyr]);
	(NN) ? obj.visibility = "visible" : obj.style.visibility = "visible";
}

function hideIt(lyr) {
	var obj = (this.id) ? this : ((NN) ? document.layers[lyr] : document.all[lyr]);
	(NN) ? obj.visibility = "hidden" : obj.style.visibility = "hidden";
}

function borderUp() {
	with (this.style) {
		borderLeftColor = buttonOverColor;
		borderTopColor = buttonOverColor;
		borderRightColor = buttonDownColor;
		borderBottomColor = buttonDownColor;
	}
}

function borderDown() {
	with (this.style) {
		borderLeftColor = buttonDownColor;
		borderTopColor = buttonDownColor;
		borderRightColor = buttonOverColor;
		borderBottomColor = buttonOverColor;
	}
}
	
function borderOff() {
	with (this.style) {
		borderLeftColor = buttonOutColor;
		borderTopColor = buttonOutColor;
		borderRightColor = buttonOutColor;
		borderBottomColor = buttonOutColor;
	}
}

menuData = new Array()
for (i=0; i < menuNames.length; i++) {
	menuData[menuNames[i]] = new Array(20)
	for (j=0; j < 20; j++) {
		menuData[menuNames[i]][j] = new Array(2);
	}
}

menuData['Game'][0][0]="New";
menuData['Game'][0][1]="javascript:parent.field.location.reload()";
menuData['Game'][0][2]=0;
menuData['Game'][1][0]="spacer";
menuData['Game'][1][1]="";
menuData['Game'][1][2]=0;
menuData['Game'][2][0]="Beginner";
menuData['Game'][2][1]="javascript:lvlB()";
menuData['Game'][2][2]=0;
menuData['Game'][3][0]="Intermediate";
menuData['Game'][3][1]="javascript:lvlI()";
menuData['Game'][3][2]=0;
menuData['Game'][4][0]="Expert";
menuData['Game'][4][1]="javascript:lvlE()";
menuData['Game'][4][2]=0;
menuData['Game'][5][0]="Custom...";
menuData['Game'][5][1]="javascript:openCustom()";
menuData['Game'][5][2]=0;
menuData['Game'][6][0]="spacer";
menuData['Game'][6][1]="";
menuData['Game'][6][2]=0;
menuData['Game'][7][0]="Color On/Off";
menuData['Game'][7][1]="javascript:parent.cfg.clron()";
menuData['Game'][7][2]=0;
menuData['Game'][8][0]="spacer";
menuData['Game'][8][1]="";
menuData['Game'][8][2]=0;
menuData['Game'][9][0]="Exit";
menuData['Game'][9][1]="javascript:window.close()";
menuData['Game'][9][2]=0;

menuData['Help'][0][0]="Help Topics";
menuData['Help'][0][1]="javascript:openHelp()";
menuData['Help'][0][2]=0;
menuData['Help'][1][0]="spacer";
menuData['Help'][1][1]="";
menuData['Help'][1][2]=0;
menuData['Help'][2][0]="About Minesweeper";
menuData['Help'][2][1]="javascript:openAbout()";
menuData['Help'][2][2]=0;

// Initialization.


if (NN) {
	makeNN('Game',100);
	makeNN('Help',180);
} else if (IE) {
	makeIE('Game',100);
	makeIE('Help',180);
}

for (i = 0; i < menuNames.length; i++) {
	var name = menuNames[i];
	var menu = (NN) ? document.layers[name] : document.all[name];
	if (IE) {
		menu.style.borderStyle = "solid";
		menu.style.borderWidth = 2;
		menu.style.borderLeftColor = menuLightColor;
		menu.style.borderTopColor = menuLightColor;
		menu.style.borderRightColor = menuDarkColor;
		menu.style.borderBottomColor = menuDarkColor;
	}
	var count = 0;
	menu.show = showIt;
	menu.hide = hideIt;
	while (menuData[name][count][0]) {
		if (menuData[name][count][0] != 'spacer') {
			if (NN) {
				var cover = document.layers[name].document.layers[name + count + 'cover'];
				cover.item = document.layers[name].document.layers[name + count];
				cover.menu = menu;
				cover.url = menuData[name][count][1];
				cover.captureEvents(Event.MOUSEUP);
				cover.onmouseup = AutoGo;
				cover.onmouseover = itemOver;
				cover.onmouseout = itemOut;
				if (menuData[name][count][2]) {
					cover.item.child = document.layers[menuData[name][count][2]];
					cover.item.child.parent = cover.item;
				}
			} else if (IE) {
				var item = document.all[name + count];
				item.url = menuData[name][count][1];
				item.menu = menu;
				item.onmousedown = AutoGo;
				item.onmouseover = itemOver;
				item.onmouseout = itemOut;
				item.style.cursor = "default";
				item.style.padding = 2;
				if (menuData[name][count][2]) {
					item.child = document.all[menuData[name][count][2]];
					item.child.parent = item;
				}
			}
		} else {
			if (IE) {
				var item = eval("document.all." + name + count);
				item.style.padding = 2;
			}
		}
		count++;
	}
	if (name.charAt(0) != 'c') {
		var button = (NN) ? document.layers[name + 'button'] : document.all[name + 'button'];
		if (IE) {
			with (button.style) {
				borderStyle = "solid";
				borderWidth = 2;
				borderLeftColor = buttonOutColor;
				borderTopColor = buttonOutColor;
				borderRightColor = buttonOutColor;
				borderBottomColor = buttonOutColor;
			}
		}
		button.menu = menu;
		menu.button = button;
		button.onmouseover = buttonOver;
		button.onmouseout = buttonOut;
		if (NN) {
			button.captureEvents(Event.MOUSEDOWN);
			button.onmousedown = buttonClick;
		} else if (IE) {
			button.up = borderUp;
			button.down = borderDown;
			button.off = borderOff;
			button.onmousedown = buttonClick;
		}
	}
}



// End initialization.
