//Start of menu definition

function Menu (theStructure,theParent) {

	//Menu prototype - sets various initial values

	this.instance = Menu.instances.length;
	this.name = "menu" + this.instance;
	Menu.instances[Menu.instances.length] = this;
	this.structure = theStructure;
	this.childMenus = [];
	this.item = [];

	if(theParent) this.parentMenu = theParent;
}

counter = 0

//Start of instance members

Menu.prototype.name = "";
Menu.prototype.divObject = null;
Menu.prototype.hidden = true;
Menu.prototype.parentMenu = null;
Menu.prototype.instance = null;
Menu.prototype.childMenus = [];
Menu.prototype.item = [];
Menu.prototype.structure = [];
Menu.prototype.width = 0;
Menu.prototype.height = 0;
Menu.prototype.isPositioned = false; // Netscape 6.0 fix


Menu.prototype.show = function(x,y) {

	//Shows current menu instance - o used for initial drop down, x and y for exact co-ords

	if(!this.hidden) return;

	this.divObject.moveTo(x,y);

	if(browserSniffer.isDOM && !browserSniffer.isIE) {

		if(!this.isPositioned) {

			this.hidden=false;
			this.isPositioned=true;
			window.setTimeout("Menu.instances[" + this.instance + "].divObject.show(); Menu.instances[" + this.instance + "].hidden=false",1)

		} else {
			
			this.divObject.show();
			this.hidden = false;
		}

	} else {
		
		this.divObject.show();
		this.hidden = false;
	}
}


Menu.prototype.showTop = function(oLink,iYOffset) {

	//Shows current menu instance - o used for initial drop down, x and y for exact co-ords

	var iX, iY;
	var oCurr;

	if(!Menu.isBuilt) return;
	if(!Menu.isCompatible) return;

	if(browserSniffer.isIE) {

		iX = oLink.offsetLeft;
		iY = oLink.offsetTop;
		
		oCurr = oLink;

		while(oCurr.offsetParent) {

			oCurr = oCurr.offsetParent;

			iX+=oCurr.offsetLeft;
			iY+=oCurr.offsetTop;
		}

	} else {

		if(browserSniffer.isDOM) {

			iX = oLink.offsetLeft;
			iY = oLink.offsetTop;

		} else {

			if(browserSniffer.isNS4) {

				iX = oLink.x;
				iY = oLink.y;
			}
		}
	}

	Menu.hideAll();
	this.divObject.moveTo(iX,iY+iYOffset);
	this.divObject.show();
	this.hidden = false;
}


Menu.prototype.build = function() {

	//Builds current menu and triggers building of children

	var sMenuHTML = "", aCurrMenu, oNewItem


	aCurrMenu = this.structure;
	this.divObject = new Div();

	for(var j=1; j<aCurrMenu.length; j++) {

		oNewItem = new menuItem(aCurrMenu[j],this);

		this.item[this.item.length] = oNewItem;
		sMenuHTML += oNewItem.HTML;
	}

	//this.divObject.write("<table id=\"" + this.name + "Sizer\" class=\"menuBorder\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td colspan=\"2\"><a href=\"void(0)\" name=\"" + this.name + "SizerTop\"><img border=\"0\" src=\"pixT.gif\"></a></td></tr>" + sMenuHTML + "<tr><td colspan=\"2\" align=\"right\"><a href=\"void(0)\" name=\"" + this.name + "SizerBottom\"><img border=\"0\" src=\"pixT.gif\"></a></td></tr></table>");
	//this.divObject.write("<table id=\"" + this.name + "Sizer\" class=\"menuBorder\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td><a href=\"void(0)\" name=\"" + this.name + "SizerTop\"><img border=\"0\" src=\"images/spacer.gif\"></a></td></tr>" + sMenuHTML + "</table>");
	this.divObject.write("<table id=\"" + this.name + "Sizer\" class=\"menuBorder\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">" + sMenuHTML + "</table>");


	if(this.childMenus.length)  for(var i=0; i<this.childMenus.length; i++) this.childMenus[i].build();


	//Work out dimensions of menu

	if(browserSniffer.isDOM) {

		this.width = document.getElementById(this.name + "Sizer").offsetWidth;
		this.height = document.getElementById(this.name + "Sizer").offsetHeight;

	} else {

		if(browserSniffer.isNS4) {

			this.width = this.divObject.theDiv.document.anchors[this.name + "SizerBottom"].x - this.divObject.theDiv.document.anchors[this.name + "SizerTop"].x;
			this.height = this.divObject.theDiv.document.anchors[this.name + "SizerBottom"].y - this.divObject.theDiv.document.anchors[this.name + "SizerTop"].y
		
		} else {

			if(browserSniffer.isIE) {

				this.width = document.all(this.name + "Sizer").offsetWidth;
				this.height = document.all(this.name + "Sizer").offsetHeight;
			}
		}
	}
}


Menu.prototype.hideChildren = function() {

	//Hides all child menus;

	if(this.childMenus.length) for(var i=0; i<this.childMenus.length; i++) this.childMenus[i].hide();
}


Menu.prototype.showChild = function(index,link) {

	var x,y;

	if(!this.childMenus[index].hidden) return;

	this.hideChildren();

	if(browserSniffer.isDOM && !browserSniffer.isMac) {

		x = this.width + this.divObject.offsetLeft();
		y = link.parentNode.offsetTop + this.divObject.offsetTop();

	} else {

		if(browserSniffer.isNS4) {

			x = this.width + this.divObject.offsetLeft();
			y = link.y + this.divObject.offsetTop();
		
		} else {

			if(browserSniffer.isIE) {

				x = this.width + this.divObject.offsetLeft();
				y = link.parentElement.parentElement.offsetTop + this.divObject.offsetTop();
			}
		}
	}

	x-=Menu.popupOverlap;
	y+=Menu.popupOverlap;

	this.childMenus[index].show(x,y);
}


Menu.prototype.hide = function() {

	//Hides current menu instance

	if(this.hidden) return;

	this.hideChildren();
	this.divObject.hide();

	this.hidden = true;
}


//Start of static members

Menu.instances = [];
Menu.popupOverlap = 3;
Menu.isBuilt = false;
//Menu.isCompatible = ((browserSniffer.isOpera) || (browserSniffer.isMac && (browserSniffer.isIE4 || browserSniffer.isIE50))) ? false : true;
Menu.isCompatible = (!browserSniffer.isMac && browserSniffer.isIE) ? true : false;

Menu.initialise = function () {

	//Triggers building of menus

	if(!Menu.isCompatible) return;

	var aCurrMenu,sMenuHTML,iTopMenus

	//Capture document clicks and key presses

	if(browserSniffer.isNS4) {
		
		document.captureEvents(Event.CLICK);
		document.captureEvents(Event.KEYDOWN);
	}
	
	document.onclick = Menu.document_onclick;
	document.onkeydown = Menu.document_onclick;

	//Now trigger build of top-level menus

	iTopMenus = Menu.instances.length;

	for(var i=0; i<iTopMenus; i++) Menu.instances[i].build();

	Menu.isBuilt = true;
}


Menu.hideAll = function() {

	//Static member that hides all menus

	if(!Menu.isCompatible) return;

	for(i=0; i<Menu.instances.length; i++) {
		
		Menu.instances[i].hide();
		Menu.instances[i].isPositioned = false;
	}
}


Menu.document_onclick = function() {

	//Overidden document_onclick
	
	Menu.hideAll();
}

//Other stuff

window.onload = function(){Menu.initialise();};
window.onresize = function(){Menu.hideAll()};



//Start of menuItem definition

function menuItem(data,currentMenu) {

	var oNewMenu;

	if(typeof(data[0])=="string") {
					
		this.HTML = "\t\t<tr class=\"menuItem\"><td nowrap class=\"menuItemText\"onMouseover=\"this.className='menuItemTextOver'\" onMouseout=\"this.className='menuItemText'\"><img src=\"images/spacer.gif\" height\"1\" width=\"12\" border=\"0\"><a onMouseOver=\"Menu.instances[" + currentMenu.instance + "].hideChildren()\" href=\"" + data[1] + "\">&nbsp;" + data[0] + "&nbsp;&nbsp;&nbsp;</a></td></tr>\n"

	} else {

		this.HTML = "\t\t<tr class=\"menuItem\"><td nowrap class=\"menuItemText\"><a onMouseOver=\"Menu.instances[" + currentMenu.instance + "].showChild(" + currentMenu.childMenus.length + ",this)\" href=\"" + data[0][1] + "\">&nbsp;" + data[0][0] + "&nbsp;&nbsp;&nbsp;</a></td><td class=\"menuItemWidget\"><a onMouseOver=\"Menu.instances[" + currentMenu.instance + "].showChild(" + currentMenu.childMenus.length + ",this)\" href=\"" + data[0][1] + "\">&nbsp;&gt;&nbsp;</a></td></tr>\n"
		var oNewMenu = new Menu(data,currentMenu);
		currentMenu.childMenus[currentMenu.childMenus.length] = oNewMenu;
	}
}

menuItem.HTML = "";
