//
// show/hide element functions
//

function getRefToElement(divID) {
    if( document.getElementById ) { //DOM; IE5, NS6, Mozilla, Opera
        return document.getElementById(divID); }
    if( document.all ) { //Proprietary DOM; IE4
        return document.all[divID]; }
    if( document.layers ) { //Netscape layers
        return document.layers[divID]; }
    if( document[divID] ) { //Netscape alternative
        return document[divID]; }
    return false;
}

function set_element_display(obj, style) {
	if (obj.style) {
		obj.style.display = style;
		return obj.style.display;
	} else if (obj.display) {
		obj.display = style;
		return obj.display;
	} else
		return false;
}

function setElementDisplay(obj, on) {
	var res = false;
	var o = getRefToElement(obj);
	if (!o) return false;
	if (o.getAttribute('visibility') != null) {
		if (on) {
			res = set_element_display(o, 'block');
			if (res) o.setAttribute('visibility', 'on');
		} else {
			res = set_element_display(o, 'none');
			if (res) o.setAttribute('visibility', 'off');
		}
	} else {
		res = set_element_display(o, 'none');
		if (res) o.setAttribute('visibility', 'off');
	}
	return (res != false);
}

//
// Menu class
//

function Menu(text) {
	this.init(text);
}

Menu.prototype.init = function(text) {
	if (!Menu.prototype.id) {
		Menu.prototype.id = 0;
	}
	this.id = Menu.prototype.id++;
	if (!Menu.prototype.allMenus) {
		Menu.prototype.allMenus = new Array();
	}
	Menu.prototype.allMenus[this.id] = this;
	this.text = text;
}

Menu.prototype.show = function(style) {
	document.writeln('<div class="' + style + '" id="menu' + this.id + '" onmouseover="showMenu(' + this.id + ')" onmouseout="hideMenu(' + this.id + ')">' + this.text + '</div>');
}

Menu.prototype.getText = function() {
	return this.text;
}

Menu.prototype.toString = function() {
	return "Menu " + this.id + " : " + this.text;
}

//
// TopLevelMenu class
//

TopLevelMenu.superclass = Menu.prototype;

function TopLevelMenu(text, page, hyperlink) {
	TopLevelMenu.superclass.init.call(this, text);
	this.locked = false;
	this.page = page;
	this.hyperlink = (hyperlink!=null)?hyperlink:false;
	this.subMenus = new Array();
	if (!TopLevelMenu.prototype.allTopLevelMenus) {
		TopLevelMenu.prototype.allTopLevelMenus = new Array();
	}
	TopLevelMenu.prototype.allTopLevelMenus[TopLevelMenu.prototype.allTopLevelMenus.length] = this;
	if (!TopLevelMenu.prototype.numTopLevelMenus) {
		TopLevelMenu.prototype.numTopLevelMenus = 0;
	}
	TopLevelMenu.prototype.numTopLevelMenus += 1;
}

TopLevelMenu.prototype.showMenus = function(isIE) {
	for (x = 0; x < TopLevelMenu.prototype.numTopLevelMenus; x++) {
		if (TopLevelMenu.prototype.allTopLevelMenus[x]) TopLevelMenu.prototype.allTopLevelMenus[x].show(isIE);
	}
}

TopLevelMenu.prototype.addSubMenu = function(subMenu) {
	this.subMenus[this.subMenus.length] = subMenu;
}

TopLevelMenu.prototype.addNewSubMenu = function(text, href) {
	newSubMenu = new SubMenu(text, href, this);
	return newSubMenu;
}

TopLevelMenu.prototype.getText = function() {
	TopLevelMenu.superclass.getText.call(this);
}

TopLevelMenu.prototype.show = function(isIE) {
	document.write('<div class="topLevelMenu" id="menu' + this.id + '" onmouseover="showMenu(' + this.id + ', true)" onmouseout="clearMenu(' + this.id + ')">');
	if (isIE) {
		document.write('<a class="page" href="');
		if (this.hyperlink) {
			document.write(this.page);
		} else {
			document.write('#');
		}
		document.write('">' + this.text + '</a>');
	} else {
		if (this.hyperlink) {
			document.write('<a class="page" href="' + this.page + '">');
		}
		document.write(this.text);
		if (this.hyperlink) {
			document.write('</a>');
		}
	}
	thisElement = this.getElement();
	for (i = 0; i < this.subMenus.length; i++) {
		this.subMenus[i].show();
		setElementDisplay('menu' + this.subMenus[i].id, false);
	}
	document.write('</div>');
}

TopLevelMenu.prototype.showMenu = function(hover) {
	for (z = 0; z < TopLevelMenu.prototype.numTopLevelMenus; z++) {
		if (TopLevelMenu.prototype.allTopLevelMenus[z] != this) {
			TopLevelMenu.prototype.allTopLevelMenus[z].hideMenu(true, true);
		}
	}
	this.highlight();
	if (!this.locked) {
		thisElement = this.getElement();
		hrefElement = thisElement.firstChild;
		if (hrefElement) {
			if (hrefElement.style) {
				hrefElement.style.color = 'red';
			} else {
				hrefElement.color = 'red';
			}
		}
	}
	if (this.subMenus.length == 0) {
		this.locked = true;
	} else {
		y = 21;
		for (i = 0; i < this.subMenus.length; i++) {
			this.subMenus[i].showMenu(y);
			y += 21;
		}
	}
}

TopLevelMenu.prototype.highlight = function () {
	thisElement = this.getElement();
	if (thisElement.style) {
		thisElement.style.borderLeft = '1px solid red';
		thisElement.style.borderRight = '1px solid red';
	} else {
		thisElement.borderLeft = '1px solid red';
		thisElement.borderRight = '1px solid red';
	}
	if (this.hyperlink) {
		hrefElement = thisElement.firstChild;
		if (hrefElement) {
			if (hrefElement.style) {
				hrefElement.style.color = 'magenta';
			} else {
				hrefElement.color = 'magenta';
			}
		}
	} else {
		if (thisElement.style) {
			thisElement.style.color = 'magenta';
		} else {
			thisElement.color = 'magenta';
		}
	}
}

TopLevelMenu.prototype.hideMenu = function(really, override) {
	if (really == true) {
		if ((override == true) || (!this.locked)) {
			for (i = 0; i < this.subMenus.length; i++) {
				this.subMenus[i].hideMenu();
			}
			thisElement = this.getElement();
			if (thisElement.style) {
				thisElement.style.borderLeft = '1px solid yellow';
				thisElement.style.borderRight = '1px solid yellow';
				thisElement.style.color = 'black';
			} else {
				thisElement.borderLeft = '1px solid yellow';
				thisElement.borderRight = '1px solid yellow';
				thisElement.color = 'black';
			}
			if (this.hyperlink) {
				hrefElement = thisElement.firstChild;
				if (hrefElement) {
					if (hrefElement.style) {
						hrefElement.style.color = 'blue';
					} else {
						hrefElement.color = 'blue';
					}
				}
			}
		}
	} else {
		setTimeout('hideMenu(' + this.id + ', true)', 1500);
	}
}

TopLevelMenu.prototype.clearMenu = function() {
	this.locked = false;
	this.hideMenu();
}

TopLevelMenu.prototype.getElement = function() {
	return getRefToElement('menu' + this.id);
}

TopLevelMenu.prototype.toString = function() {
	return "TopLevelMenu " + this.id + " : " + this.text;
}

//
// SubMenu class
//

SubMenu.superclass = Menu.prototype;

function SubMenu(text, href, parent) {
	SubMenu.superclass.init.call(this, text);
	this.href = href;
	this.parent = parent;
	parent.addSubMenu(this);
}

SubMenu.prototype.show = function() {
	document.write('<div class="subMenu" id="menu' + this.id + '" onmouseover="showMenu(' + this.id + ',true)" onmouseout="clearMenu(' + this.id + ')">' + this.getText() + '</div>');
}

SubMenu.prototype.getText = function() {
	return '<a class="page" href="' + this.parent.page + ((this.href!='')?'?':'') + this.href + '">' + this.text + '</a>';
}

SubMenu.prototype.toString = function() {
	return "SubMenu " + this.id + " : " + this.text;
}

SubMenu.prototype.showMenu = function(y) {
	this.parent.locked = true;
	thisElement = this.getElement();
	if (y != true) {
		thisElement.style.left = 5;
		thisElement.style.top = y;
		thisElement.style.position = 'absolute';
	} else {
		this.parent.highlight();
		hrefElement = thisElement.firstChild;
		if (hrefElement) {
			if (hrefElement.style) {
				hrefElement.style.color = 'red';
			} else {
				hrefElement.color = 'red';
			}
		}
	}
	setElementDisplay('menu' + this.id, true);
}

SubMenu.prototype.clearMenu = function() {
	this.parent.locked = false;
	this.parent.hideMenu(false);
}

SubMenu.prototype.hideMenu = function() {
	setElementDisplay('menu' + this.id, false);
}

SubMenu.prototype.getElement = function() {
	return getRefToElement('menu' + this.id);
}

//
// access functions
//

function showMenu(id, hilit) {
	Menu.prototype.allMenus[id].showMenu(hilit);
}

function hideMenu(id, really) {
	Menu.prototype.allMenus[id].hideMenu(really);
}

function clearMenu(id) {
	Menu.prototype.allMenus[id].clearMenu();
}

function insertMenus(isIE) {
	document.writeln('<div class="menuBar"><span class="menuTitle">site menu&gt;</span>');
	TopLevelMenu.prototype.showMenus(isIE);
	document.writeln('</div>');
}