// ***********************************************************************************************
// Tree Menu
//
// Author: Paul Stevens
// Created: Nov 2001
// Company: Netpaver
//
// Copyright (c) 2001 Netpaver
//
// Modified: Shane Cook (SBC) Feb 2002
//
// ***********************************************************************************************

var menuDefined = false;
var menuInitialised = false;

var menuToScroll = null;
var scrollBy = 0;

var menuToBuild = null;
var rebuilding = false;

var NL = "\n";

// Menu

function Menu(graphicsDir, contractedGraphic, expandedGraphic, leafGraphic) {
	this.topItems = [];
	this.allItems = [];
	this.graphicsDir = graphicsDir;
	this.contractedImage = new Image;
	this.contractedImage.src = graphicsDir + "/" + contractedGraphic;
	this.expandedImage = new Image;
	this.expandedImage.src = graphicsDir + "/" + expandedGraphic;
	this.leafImage = new Image;
	this.leafImage.src = graphicsDir + "/" + leafGraphic;
	this.layer = null;
	this.top = 0;
	this.y = 0;
	this.width = 500;
	this.height = 0;
	this.indent = 15;
	this.onMouseOver = null;
	this.onMouseOut = null;
	this.endItemHTML = "";
}

Menu.prototype.add = function(child) {
	child.menu = this;
	child.ID = this.allItems.length;
	child.level = 0;
	if (child.graphic != null) {
		child.image = new Image;
		child.image.src = this.graphicsDir + "/" + child.graphic;
	}
	child.contractedImage = this.contractedImage;
	child.expandedImage = this.expandedImage;
	child.leafImage = this.leafImage;
	this.topItems[this.topItems.length] = child;
	this.allItems[child.ID] = child;
}

Menu.prototype.useImages = function(contractedGraphic, expandedGraphic, leafGraphic) {
	this.contractedImage = null;
	this.expandedImage = null;
	this.leafImage = null;
	if (contractedGraphic != null) {
		this.contractedImage = new Image;
		this.contractedImage.src = this.graphicsDir + "/" + contractedGraphic;
	}
	if (expandedGraphic != null) {
		this.expandedImage = new Image;
		this.expandedImage.src = this.graphicsDir + "/" + expandedGraphic;
	}
	if (leafGraphic != null) {
		this.leafImage = new Image;
		this.leafImage.src = this.graphicsDir + "/" + leafGraphic;
	}
}

Menu.prototype.addHeading = function(graphic) {
	child = new MenuItem(null, graphic, null, null);
	child.menu = this;
	child.ID = this.allItems.length;
	child.level = 0;
	child.heading = true;
	if (child.graphic != null) {
		child.image = new Image;
		if (child.graphic.substring(0, 1) == '/') {
			child.image.src = child.graphic;
		}
		else {
			child.image.src = this.graphicsDir + "/" + child.graphic;
		}
	}
	this.topItems[this.topItems.length] = child;
	this.allItems[child.ID] = child;
}

Menu.prototype.expand = function(ID) {
	this.allItems[ID].expand();
}

Menu.prototype.contract = function(ID) {
	this.allItems[ID].contract();
}

// Menu Item

function MenuItem(text, graphic, href, target, style, cellStyle, title) {
	this.ID = -1;
	this.level = -1;
	this.text = text;
	this.graphic = graphic;
	this.image = null;
	this.href = href;
	this.target = target;
	this.expanded = false;
	this.heading = false;
	this.parent = null;
	this.children = [];
	this.gapBefore = 0;
	this.gapAfter = 0;
	this.contractedImage = null;
	this.expandedImage = null;
	this.leafImage = null;
	this.style = style;
	this.cellStyle = cellStyle;
	this.title = title;
}

MenuItem.prototype.add = function(child) {
	if (this.menu == null) {
		alert("ERROR: Parent of '" + child.text + "' menu item not linked to menu.");
		return;
	}
	child.ID = this.menu.allItems.length;
	child.level = this.level + 1;
	child.parent = this;
	child.menu = this.menu;
	if (child.graphic != null) {
		child.image = new Image;
		child.image.src = this.menu.graphicsDir + "/" + child.graphic;
	}
	child.contractedImage = this.menu.contractedImage;
	child.expandedImage = this.menu.expandedImage;
	child.leafImage = this.menu.leafImage;
	this.children[this.children.length] = child;
	this.menu.allItems[child.ID] = child;
}

MenuItem.prototype.expand = function() {
	this.expanded = true;
}

MenuItem.prototype.contract = function() {
	this.expanded = false;
}

function buildMenu(menu) {
 	menuToBuild = menu;
	_buildMenu();
}

function _buildMenu() {
	if (rebuilding) {
		return;
	}
	rebuilding = true;
	menuInitialised = false;
	var menu = menuToBuild;
	var html = "";
	html += "<TABLE BORDER='0' CELLSPACING='0' CELLPADDING='0' WIDTH='" + menu.width + "'>" + NL;
	var i = 0;
	for (; i < menu.topItems.length; i++) {
		var item = menu.topItems[i];
		html += buildMenuItem(item);
		if (item.expanded) {
			html += buildMenuChildren(item);
		}
	}
	html += "</TABLE>" + NL;
	if (document.layers) {
		html += "<DIV ID='menubottom' style='position: absolute;'></DIV>";
		var doc = document.menulayer.document;
		menu.layer = document.menulayer;
		doc.writeln(html);
	    doc.close();
	}
	else {
		// alert(html);
		// menu.layer = document.all.menulayer;
		menu.layer = document.getElementById("menulayer");
		menu.layer.innerHTML = html;
		initMenu();
		menu.layer.style.top = menu.y;
	}
	rebuilding = false;
}

function initMenu() {
	if (document.layers) {
		menu.layer = document.menulayer;
		menu.height = menu.layer.document.menubottom.pageY;
	}
	else {
		menu.layer = document.getElementById("menulayer");
		menu.height = menu.layer.offsetHeight;
	}
	menu.height -= 50;
	if (menu.height < 0) {
		menu.height = 0;
	}
	menuInitialised = true;
}

function buildMenuChildren(menuItem) {
	var i = 0;
	var html ="";
	for (; i < menuItem.children.length; i++) {
		var item = menuItem.children[i];
		html += buildMenuItem(item);
		if (item.expanded) {
			html += buildMenuChildren(item);
		}
	}
	return html;
}

function buildMenuItem(menuItem) {
	var html = "";
	if (menuItem.gapBefore > 0) {
		html += "    <TR><TD HEIGHT='" + menuItem.gapBefore + "'></TD></TR>" + NL;
	}

	html += "    <TR>";

	if (menuItem.heading) {
		html += "<TD></TD><TD><IMG SRC='" + menuItem.image.src + "' BORDER='0'></TD>";
	}
	else {
		var href = menuItem.href;
		var target = menuItem.target;
		var onClick = "";
		if (menuItem.children.length > 0) {
			if (menuItem.expanded) {
				if (href == null) {
					href = "#";
					onClick = " ONCLICK='contract(" + menuItem.ID + "); return false;'";
					target = null;
				}
				var img = "<IMG SRC='" + menuItem.expandedImage.src + "' BORDER='0'>";
				html += "<TD><A HREF='#' ONCLICK='contract(" + menuItem.ID + "); return false;'>" + img + "</A>&nbsp;&nbsp;</TD>";
			}
			else {
				if (href == null) {
					href = "#";
					onClick = " ONCLICK='expand(" + menuItem.ID + "); return false;'";
					target = null;
				}
				var img = "<IMG SRC='" + menuItem.contractedImage.src + "' BORDER='0'>";
				html += "<TD><A HREF='#' ONCLICK='expand(" + menuItem.ID + "); return false;'>" + img + "</A>&nbsp;&nbsp;</TD>";
			}
		}
		else {
			if (menuItem.leafImage != null) {
				html += "<TD><IMG SRC='" + menuItem.leafImage.src + "' BORDER='0'>&nbsp;&nbsp;</TD>";
			}
			else {
				html += "<TD></TD>";
			}
		}
        if (href == null) {
			href = "#";
        } else if (href.toLowerCase().search("javascript:") == 0) {
			var js = href.substring(11);
			js = js.replace("'", "\"");
			onClick = " ONCLICK='" + js + "; return false;'";
			href = "#";
		}
		var targetOption = "";
		if (target != null) {
			targetOption = " TARGET='" + target + "'";
		}
		var onMouseOver = ((menuItem.menu.onMouseOver != null) ? " onMouseOver='" + menuItem.menu.onMouseOver + "'" : "");
		var onMouseOut = ((menuItem.menu.onMouseOut != null) ? " onMouseOut='" + menuItem.menu.onMouseOut + "'" : "");
		onMouseOver = onMouseOver.replace(/&menu-item&/, "MenuItem" + menuItem.ID);
		onMouseOut = onMouseOut.replace(/&menu-item&/, "MenuItem" + menuItem.ID);
		var title = ((menuItem.title != null) ? " title='" + menuItem.title + "'" : "");
		if (menuItem.image == null) {
			var itemText = menuItem.text;
			if (menuItem.style != null) {
				itemText = "<SPAN CLASS=\"" + menuItem.style + "\">" + itemText + "</SPAN>";
			}
			var cellStyle = "";
			if (menuItem.cellStyle != null) {
				cellStyle = " CLASS=\"" + menuItem.cellStyle + "\"";
			}
			if (href != null) {
				html += "<TD" + cellStyle + "><A HREF='" + href + "' " + targetOption + onClick + onMouseOver + onMouseOut + title + ">";
				html += itemText + "</A></TD>";
			}
			else {
				html += "<TD" + cellStyle + ">" + itemText + "</TD>";
			}
		}
		else {
			html += "<TD><A HREF='" + href + "'" +  targetOption + onClick + onMouseOver + onMouseOut + ">";
			html += "<IMG SRC='" + menuItem.image.src + "' BORDER='0' ALT='" + menuItem.text + "'></A></TD>";
		}
		var endHTML = menuItem.menu.endItemHTML;
		if (endHTML != null) {
			endHTML = endHTML.replace(/&menu-item&/, "MenuItem" + menuItem.ID);
			html += "<TD>" + endHTML +"</TD>";
		}
	}
	html += "</TR>" + NL;
	if (menuItem.gapAfter > 0) {
		html += "    <TR><TD HEIGHT='" + menuItem.gapAfter + "'></TD></TR>" + NL;
	}
	return html;
}

function scrollMenu(menu, pixels) {
	menuToScroll = menu;
	scrollBy = pixels;
	scrollMenuLoop();
}

function scrollMenuLoop() {
	if (scrollBy != 0) {
		if (scrollLayer(menuToScroll.layer, menuToScroll.top, menuToScroll.height, scrollBy)) {
			if (!document.layers) {
				menuToScroll.y = menuToScroll.layer.style.top;
			}
			else {
				menu.y = menu.layer.top;
			}
			setTimeout("scrollMenuLoop()", 20);
		}
		else {
			scrollBy = 0;
		}
	}
	if (document.layers) {
		menu.y = menu.layer.top;
	}
}

/*
function scrollWindow(window, height, pixels) {
	if (document.layers) {
		pixelTop = window.pageYOffset;
		if ((pixels < 0 && pixelTop < height) ||
			(pixels > 0 && pixelTop > 0))
		{
			window.scrollTo(0, pixelTop - pixels);
			return true;
		}
		return false;
	}
	else {
		if ((pixels < 0 && window.style.pixelTop > -height) ||
			(pixels > 0 && window.style.pixelTop < 0))
		{
			window.style.pixelTop += pixels;
			return true;
		}
		return false;
	}
}
*/

function scrollLayer(layer, top, height, pixels) {
	if (document.layers) {
		if ((pixels < 0 && layer.top > top-height) ||
			(pixels > 0 && layer.top < top))
		{
			layer.top += pixels;
			return true;
		}
		return false;
	}
	else {
		if ((pixels < 0 && (layer.style.pixelTop > top-height)) ||
			(pixels > 0 && layer.style.pixelTop < top))
		{
			layer.style.pixelTop += pixels;
			return true;
		}
		return false;
	}
}

function resetMenu(menu) {
	if (document.layers) {
		menu.layer.top = menu.top;
	}
	else {
		menu.layer.style.pixelTop = menu.top;
	}
	menu.y = menu.top;
}

// On Load

function initOnLoad() {
	if (document.layers) {
		if (!top.document.menuDefined) {
			defineMenu();
			top.document.menu = menu;
			top.document.menuDefined = true;
		}
		else {
			menu = top.document.menu;
		}
	}
	else {
		if (!menuDefined) {
			defineMenu();
			menuDefined = true;
		}
	}
	buildMenu(menu);
	if (document.layers) {
		initMenu();
		menu.layer.top = menu.y;
	}
}

function refresh() {
	if (document.layers) {
		document.location.replace(document.location.href);
	}
	else {
		buildMenu(menu);
	}
}

function expand(ID) {
	menu.expand(ID);
	refresh();
}

function contract(ID) {
	menu.contract(ID);
	refresh();
}

String.prototype.replace = function stringReplace(findText, replaceText) {
	// alert("Here");
	var originalString = new String(this);
	var newString = "";
	var index = 0;
	var pos = originalString.indexOf(findText, index);
	while (pos != -1) {
		newString += originalString.substring(index, pos) + replaceText;
		index = pos + findText.length;
		pos = originalString.indexOf(findText, index);
	}
	newString += originalString.substring(index, originalString.length);
	return newString;
}


