// VERMIN ( Vertically Expanding RAD Menu for Internet Navigation )
// version 2.0
// created by: Robert Gilks
// date: 2/9/01

var loadVermin = true;
var menuCreated = false;
var fileName = null;

// find the filename
var querySplit = String(location).split("/");
var fileName = querySplit[querySplit.length-2] + "/"+ querySplit[querySplit.length-1];

// find pageId
function findPageId(theArr, theFileName) {
	for (var i in theArr) {								
		if (typeof(theArr[i]) == 'object') {
			if (theArr[i][2] == theFileName) {
				pageId = theArr[i][0];
				break;
			}
			findPageId(theArr[i], theFileName);					
		}									
	}						
}

// the menu constructor
function verminMenu( name, top, left, width, zIndex, spacing, contentPath) {
	
	this.name = name;
	this.top = top;
	this.left = left;
	this.width = width;
	this.zIndex = zIndex
	this.spacing = spacing;
	this.arrayOffset = 3;	
	this.contentPath = contentPath;
	
	this.count = 0;
	this.currTop = this.top;
	
	this.menuLayers = new Array(); // this array contains all the menu objects
	this.parentID = new Array(); // holds the parent ID for each menu item
	this.states = new Array(); // states of +/- icons
	
	verminMenu.prototype.createMenu = _createMenu;
	verminMenu.prototype.drawMenu = _drawMenu;
	verminMenu.prototype.findPath = _findPath;
	verminMenu.prototype.switchIndicator = _switchIndicator;
	verminMenu.prototype.switchColor = _switchColor;
	verminMenu.prototype.updateMenu = _updateMenu;	
	
	this.createMenu(menuStructure, 0, 0);
	if (pageId == '') findPageId(menuStructure, fileName);
	this.updateMenu(menuStructure, pageId);
}
	
// verminMenu Methods
	
function _createMenu(theArr, level, theId) {
	for (var i in theArr) {
		if (typeof(theArr[i]) == 'object') {
			var id = theArr[i][0];		
			
			if (isMac && isIE) {
				var str = '<div id="ind'+id+'" class="menuLevel'+(level/this.arrayOffset)+'">&nbsp;</div>';
				this.states[id] = 0;			
				if (level == 0 && hasChildren(theArr[i])) this.states[id] = -1;			
				if (level > 0 ) str += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'; else str += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
				if (theArr[i][2] != 'null') {
					if (theArr[i][2] != 'XXX') {
						str += '<a href="'+this.contentPath+theArr[i][2]+'" id="anchor'+id+'" class="linkLevel'+(level/this.arrayOffset)+'">';						
					} else {
						str += '<a href="javascript: alert(\'Sorry this resource is unavailable.\');" id="anchor'+id+'" class="linkLevel'+(level/this.arrayOffset)+'">';
					}
				} else {
					str += '<a href="javascript: function() {return false;}" id="anchor'+id+'" onMouseUp="'+this.name+'.updateMenu(menuStructure,\''+id+'\')" class="linkLevel'+(level/this.arrayOffset)+'">';						
				}			
				str += theArr[i][1]+'</a>';			
				
			} else {
				if (isNN4) {
					var str = '<table border="0" cellspacing="0" cellpadding="0"><tr><td><img src="/interfaces/html/images/common/spacer.gif" width="1" height="5" border="0"><br><div id="ind'+id+'" class="NN4menuLevel'+(level/this.arrayOffset)+'"><img src="/interfaces/html/images/common/spacer.gif" width="1" height="1" border="0"></div></td><td>';
				} else {
					var str = '<div id="ind'+id+'" class="menuLevel'+(level/this.arrayOffset)+'">&nbsp;</div>';
				}
				str += '<table border="0" cellspacing="0" cellpadding="0" width="'+this.width+'"><tr>';
				if (isNN4) {
					if (level > 0 ) str += '<td width="20">'; else str += '<td width="10">';
				} else {
					if (level > 0 ) str += '<td width="25">'; else str += '<td width="15">';
				}
				str += '</td><td valign="top" width="'+ parseInt(this.width - 15) +'">';
				this.states[id] = 0;			
				if (level == 0 && hasChildren(theArr[i])) this.states[id] = -1;			
				if (theArr[i][2] != 'null') {
					if (theArr[i][2] != 'XXX') {
						str += '<a href="'+this.contentPath+theArr[i][2]+'" id="anchor'+id+'" class="linkLevel'+(level/this.arrayOffset)+'">';						
					} else {
						str += '<a href="javascript: alert(\'Sorry this resource is unavailable.\');" id="anchor'+id+'" class="linkLevel'+(level/this.arrayOffset)+'">';
					}
				} else {
					str += '<a href="javascript: function() {return false;}" id="anchor'+id+'" onMouseUp="'+this.name+'.updateMenu(menuStructure,\''+id+'\')" class="linkLevel'+(level/this.arrayOffset)+'">';						
				}			
				str += theArr[i][1]+'</a></td></tr></table>';
				if (isNN4) {
					str += '</td></tr></table>';
				}	
			
			}		
			
			var currentLayer = makeLayer(id);
			currentLayer.st.visibility = "hidden";
			writeToLayer(currentLayer.el, str);
			currentLayer.st.top = this.currTop;
			currentLayer.st.left = this.left;
			currentLayer.st.width = this.width;
			currentLayer.st.zIndex = this.zIndex;
			currentLayer.st.padding = 0;
			currentLayer.st.margin = 0;		
			this.menuLayers[id] = currentLayer;
			this.parentID[this.count] = id;
			this.parentID[this.count+1] = theId;
			this.count+=2;			
			this.createMenu(theArr[i], level, id);
		} else level ++
	}	
	return menuCreated = true;		
}

function _findPath(theId, pathPos) {
	for ( var i=0 ; i < this.parentID.length ; i+=2) {
		if (this.parentID[i] == theId) {
			pathUp[pathPos]	= this.parentID[i];
			pathPos++;
			this.findPath(this.parentID[i+1], pathPos);
			break;
		}				
	}		
}

function _switchIndicator(theId, state) {	
		if (state == 1) {
			col = "black";
		} else {
			col = "#B5403F";
		}
		if (isNN4) {
			eval('document.layers.'+this.menuLayers[theId].el.id+'.document.layers.ind'+theId+'.bgColor = "'+col+'"');
		} else if (isDom) {			
			eval('document.getElementById("ind'+theId+'").style.backgroundColor = "'+col+'"');
			eval('document.getElementById("ind'+theId+'").style.border = "1px solid black"');
		} else {
			eval('document.all["ind'+theId+'"].style.backgroundColor = "'+col+'"');
			eval('document.all["ind'+theId+'"].style.border = "1px solid black"');
		}
		this.states[theId] = state;
	return true;	
}
	
	
function _switchColor(theId, theColor) {	
	if (isNN4) {
		// doesnt work
	} else if (isDom) {		
		eval("document.getElementById('anchor"+theId+"').style.color = theColor");
	} else {
		document.all['anchor'+theId].style.color = theColor;
	}
	return true;	
}
	
function _drawMenu(theArr, level, visLevel) {
	for (var i in theArr) {
		if (typeof(theArr[i]) == 'object') {
			var id = theArr[i][0];
			var fn = theArr[i][2];
			if (visLevel) {					
				this.menuLayers[id].st.visibility = "hidden";
				this.menuLayers[id].st.zIndex = this.zIndex;
				this.menuLayers[id].st.top = this.currTop;
				if ( id == pageId ) {
					this.switchColor(id, "#B5403F");
				} else {
					this.switchColor(id, "#000000");
				}
				if (hasChildren(theArr[i])) { 
					if (id == pathUp[pathUp.length - (level/this.arrayOffset) -1]) {
						this.switchIndicator(id, -1);
					} else {
						this.switchIndicator(id, +1);
					}
				}			
				if (isNN4) {
					this.currTop += 15 + this.spacing;
				} else if (isDom) {
					this.currTop += this.menuLayers[id].el.offsetHeight + this.spacing;
				} else {this.currTop += this.menuLayers[id].el.offsetHeight + this.spacing;}
				this.menuLayers[id].st.visibility = "visible";
			} else {
				this.menuLayers[id].st.visibility = 'hidden';
				this.menuLayers[id].st.zIndex = -1;
			}							
			this.drawMenu(theArr[i], level, (pathUp[pathUp.length - (level/this.arrayOffset) -1 ] == id));	
			} else level ++
		}	
	return true;		
}

function _updateMenu(theArr, theId) {
	pathUp = new Array();	
	this.findPath(theId, 0);
	this.currTop = this.top;
	if (theId != 0) {
		if (this.states[theId] == -1) pathUp[0] = 0;
	}
	this.drawMenu(menuStructure, 0, true);	
	eval(this.menuRedraw);			
}
	
// end of verminMenu methods
	

// utility functions below
	
function makeLayer(theId) {	
	var retObj = new Object();	
	if (isNN4) {
		retObj.st = new Layer(350);
		retObj.el = retObj.st;
	} else if (document.body.appendChild) {
		retObj.el = document.createElement("div");
		document.body.appendChild(retObj.el);
		retObj.el.id = theId;
		retObj.st = retObj.el.style;
		retObj.st.position = 'absolute';
		retObj.st.zIndex = 20;
	} else {
		divStr = '<DIV ID="' + theId + '" STYLE="position: absolute; z-index: 20;"></DIV>';
		document.body.insertAdjacentHTML("BeforeEnd", divStr);
		retObj.st = document.all[theId].style;
		retObj.el = document.all[theId];
	}	
	return retObj;
}

function writeToLayer(theLayer, theStr) {
	if (isNN4) {
		theLayer.document.write(theStr);
		theLayer.document.close();
	} else {
		theLayer.innerHTML = theStr;
	}	
}
	
function hasChildren(theArr) {
	for (var i in theArr) {
		if (typeof(theArr[i]) == 'object') return true;
	}
	return false;		
}	