window.onload = DoOnloadStuff;

function DoOnloadStuff() {

	CreateSubmenus();

	var element = GetElementWithId('menu');
	if (element) {
		var menuNodes = element.childNodes;
		var done = false;
		for (var j = 0; j < menuNodes.length && !done; ++j) {
			if (menuNodes[j].nodeType == 1) {
				var listNodes = menuNodes[j].childNodes;
				for (var i = 0; i < listNodes.length; ++i) {
					var node = listNodes[i];
					if (node.nodeType == 1) {
						node.onmouseover = MenuItem_mouseover;
						node.onmouseout = MenuItem_mouseout;
						node.onclick = MenuItem_click;
						}
					}
				done = true;
				}
			}
		}

	var popups = document.getElementsByTagName('a');
	for (i = 0; i < popups.length; ++i) {
		if (popups[i].rel.indexOf('popup') != -1) {
			popups[i].onclick = DoPopup;
			}
		}
	}

var submenus = [[],
	['Movies', 'Fun', 'Combat tips', 'Engineering tips', 'Weapon hot spots', 'Annoyances', 'Level preferences', 'Level talk-throughs'],
	['Fun', 'Suicide reversion', 'Creating multiple saves', 'Halo 3 - Game on!', 'Floodgate play-through'],
	['Fun'],
	['Fun'],
	['Frontline with Suzie', 'Spotlight with Suzie', 'Halo in a nutshell', 'Unleash your inner Grunt', 'Xbox avatar hellspawn'],
	['Soapbox', 'Halo 2 disappointment', 'Forum posts', 'Tomb Raider Underworld'],
	[],[]];

var pathStems = ['','Halo','Halo3','ODST','Reach', 'Humour','Other','',''];

var pathEndings = [[],
	['/Movies/Index.html', '/Fun/Index.html', '/CombatTips/Index.html', '/EngineeringTips/Index.html', '/WeaponHotSpots.html', '/Annoyances.html', '/LevelPreferences.html', '/TalkThroughs/Index.html'],
	['/Fun/Index.html', '/SuicideReversion.html', '/CreatingMultipleSaves.html', '/Halo3GameOn.html', '/FloodgatePlayThrough.html'],
	['/Fun/Index.html'],
	['/Fun/Index.html'],
	['/Frontline/Index.html', '/Spotlight/Index.html', '/Nutshell.html', '/InnerGrunt.html', '/AvatarHellspawn.html'],
	['/Soapbox.html', '/Halo2Disappointment.html', '/ForumPosts.html', '/TRU/Index.html'],
	[],[]];

function CreateSubmenus() {

	var element = GetElementWithId('menu');
	if (element) {
		
		var menuItems = [];
		{
			var elements = element.getElementsByTagName('li');
			for (var i = 0; i < elements.length; ++i) {
				menuItems[i] = elements[i];
				}
			}

		if (menuItems.length == submenus.length) {

			var pagePath = window.location.pathname;
			if (window.location.protocol == 'file:') {

				var n = 1;
				{
					var s = menuItems[0].innerHTML;
					var startString = 'href="';
					var i = s.indexOf(startString);
					if (i != -1) {
						i += startString.length;
						while (s.indexOf('../', i) == i) {
							++n;
							i += 3;
							}
						}
					}

				var i = pagePath.length;
				for (var k = 0; k < n; ++k) {
					i = pagePath.lastIndexOf('/', i - 1);
					}
				pagePath = pagePath.slice(i);
				}
			if (pagePath.length > 0 && pagePath.charAt(0) == '/') {
				pagePath = pagePath.slice(1);
				}
			var pagePathSplit = pagePath.split('/');

			for (var i = 0; i < menuItems.length; ++i) {

				if (submenus[i].length > 0) {

					var code = menuItems[i].innerHTML;
				 	code += '<div class="sw1"><div class="sw2"><div class="sw3"><div class="sw4"><ul>';

					for (var j = 0; j < submenus[i].length; ++j) {

						var relativePath = '';
						{
							var itemPath = pathStems[i] + pathEndings[i][j];
							itemPathSplit = itemPath.split('/');

							var d = 0;
							while (d < itemPathSplit.length && d < pagePathSplit.length && itemPathSplit[d] == pagePathSplit[d]) {
								++d;
								}

							for (var k = d + 1; k < pagePathSplit.length; ++k) {
								relativePath += "../";
								}
							for (var k = d; k < itemPathSplit.length; ++k) {
								if (k > d) {
									relativePath += "/";
									}
								relativePath += itemPathSplit[k];
								}							
							}

						code += '<li';
						if (j == submenus[i].length - 1) {
							code += ' class="lastInSubmenu"';
							}
						code += '><a ';
						if (relativePath.length > 0) {
							code += 'href="' + relativePath;
							}
						else {
							code += 'id="currentPage"';
							}
						code += '">' + submenus[i][j] + '</a></li>';
						}

					code += '</ul></div></div></div></div>';
					menuItems[i].innerHTML = code;
					}
				}
			}
		}
	}

var currentShower = null;
var planningToHide = false;
var t_hide;
var plannedShower = null;
var t_show;

function MenuItem_mouseover() {

	if (currentShower == this) {
		if (planningToHide) {
			clearTimeout(t_hide);
			planningToHide = false;
			}
		}
	else if (plannedShower != this) {
		if (plannedShower != null) {
			clearTimeout(t_show);
			plannedShower = null;
			}
		plannedShower = this;
		t_show = setTimeout(DoPlannedShowing, currentShower == null ? 200 : 1);
		}
	}

function MenuItem_mouseout() {

	if (plannedShower == this) {  // Cancel
		clearTimeout(t_show);
		plannedShower = null;
		}
	else if (currentShower == this  &&  !planningToHide) {
		planningToHide = true;
		t_hide = setTimeout(DoPlannedHiding, 500);
		}
	}

function MenuItem_click() {

	if (plannedShower == this) {
		clearTimeout(t_show);
		plannedShower = null;
		}
	else if (currentShower == this) {
		planningToHide = true;
		DoPlannedHiding();
		}
	}

function DoPlannedShowing() {

	if (plannedShower != null) {
		if (planningToHide) {
			DoPlannedHiding();
			clearTimeout(t_hide);
			}
		var elements = plannedShower.getElementsByTagName('div');
		if (elements.length > 0) {
			elements[0].style.display = 'block';
			}
		currentShower = plannedShower;
		plannedShower = null;
		}
	}

function DoPlannedHiding() {

	if (currentShower != null  &&  planningToHide) {
		 var elements = currentShower.getElementsByTagName('div');
		 if (elements.length > 0) {
			 elements[0].style.display = 'none';
			 }
		currentShower = null;
		planningToHide = false;
		}
	}

function GetElementWithId(id) {

	var obj = null;
	if (document.getElementById) {
		obj = document.getElementById(id);
		}
	else if (document.all) {
		obj = document.all[id];
		}
	else if (document.layers) {
		obj = document.layers[id];
		}

	return obj;
	}

function DoPopup(e) {

	var width = '600';
	var height = '450';

	attribs = this.rel.split(' ');
	if (attribs[1] != null) {
		width = attribs[1];
		}
	if (attribs[2] != null) {
		height = attribs[2];
		}

	ShowInPopup(this.href, width, height);

	if (window.event) {
		window.event.returnValue = false;
		window.event.cancelBubble = true;
		} 
	else if (e) {
		e.stopPropagation();
		e.preventDefault();
		}
	}

var popupViewer = null;

function ShowInPopup(url, strWidth, strHeight) {

	var features = 'left=10, top=30, resizable, toolbar=no, status=no, directories=no, width=' + strWidth + ', height=' + strHeight;
	popupViewer = window.open(url, 'window', features);
	popupViewer.focus();
	}

var numericStemBases = [
	'',
	'',
	'Level',
	'BCM'
	];

var familySizes = [
	20,
	3,
	1,
	45
	];

function DoNumericSiblingLinkage(familyIndex, itemNumber) {

	var element = document.getElementById('siblingLinkage');
	if (element) {

		var code = '';
		{
			var s1 = '<a href="' + numericStemBases[familyIndex];
			var s2 = '.html">';
			var sFirst = 'First</a>';
			var sPrev = 'Prev</a>';
			var sNext = 'Next</a>';
			var sLast = 'Last</a>';
			var sDisabled = '<a class="disabled">';

			if (itemNumber > 1) {
				code += s1 + 1 + s2 + sFirst + s1 + (itemNumber - 1) + s2 + sPrev;
				}
			else {
				code += sDisabled + sFirst + sDisabled + sPrev;
				}

			var maxNumber = familySizes[familyIndex];
			if (itemNumber < maxNumber) {
				code += s1 + (itemNumber + 1) + s2 + sNext + s1 + maxNumber + s2 + sLast;
				}
			else {
				code += sDisabled + sNext + sDisabled + sLast;
				}
			}

		element.innerHTML = code;
		}
	}
