// SET HEIGHT of MENU ITEMS HERE 

var heightOffset = 18;	//pixels tall for each DIV -- UNIFORM HEIGHT***

var currentNode;
rootNode = -1;

// MENU FUNCTIONS

function Tier(objName,objHandle,status){
	this.name = objName;
	this.open = status || 0;				//1 if open, 0 if closed
	this.ref = objHandle;
	this.setVisibility = tierSetVisibility;
	this.setLocation = tierSetLocation;
	this.next = null;
	this.child = null;
	this.parent = null;
	this.url = null;
	this.toggle = toggleTier;
	this.sideEffect = null;					//use this as a pointer to a function keyed off a tier-click
}

function toggleTier(){
	this.open = (this.open) ? 0 : 1;		//If I'm open, then close me, otherwise open me
	var currPos = this;
	
	if(this.open){
		if((currPos.child)&&(currPos.open)){	
			currPos = currPos.child;
			while(currPos){
				currPos.ref.visibility = "visible";
				currPos = currPos.next;
			}
		}
	}
	else{
		if((currPos.child)&&(!currPos.open)){	
			currPos = currPos.child;
			while(currPos){
				currPos.ref.visibility = "hidden";
				currPos = currPos.next;
			}
		}
	}
	shuffleTree();
}

function tierSetVisibility(what){
	this.ref.visibility = what;
}

function tierSetLocation(dx,dy){
	if(!this.ref) return;	
	this.ref.left = dx;
	this.ref.top = dy;
}

function findTier(objName){
	var currPos = rootNode;
		while(currPos){
			if(currPos.name == objName) return currPos;
			if((currPos.child)&&(currPos.open)){	
				tempPos = currPos;
				currPos = currPos.child;
				while(currPos){
					if(currPos.name == objName) return currPos;
					currPos = currPos.next;
				}
				currPos = tempPos;
			}
			currPos = currPos.next;
		}
}

function activateTier(objName){
	var theName = objName;
	obj = findTier(theName);
	if(obj) obj.toggle();
}

function shuffleTree(){
	var theX = (window.dynamicX) ? dynamicX : 0;
	var theY = (window.dynamicY) ? dynamicY : 0;
	var currPos = rootNode;
	var currY = 0;

	while(currPos){
		currPos.setLocation(theX,currY+theY);
		currY += heightOffset;
		currPos.setVisibility("visible");
		if((currPos.child)&&(currPos.open)){
			tempPos = currPos;
			currPos = currPos.child;
			while(currPos){
				currPos.setLocation(theX,currY+theY);
				currY += heightOffset;
				currPos = currPos.next;
			}
			currPos = tempPos;
		}
		currPos = currPos.next;
	}
}



function makeLayers(){

	var treeTop = (document.layers) ? document.layers : document.all;
	var node = (document.layers) ? "name" : "i";
	var thing;
	var menu = new Array();
	
	if((document.getElementById)&&(!document.all)){
		if (document.getElementsByTagName("*"))
			treetop = document.getElementsByTagName("*");

		searchables = treetop.length;
		for(var i=0; i<searchables; i++){
			var text = treetop[i].id.substr(0,7);
			if(text=="dynamic"){
				menu[treetop[i].id] = treetop[i];
			}
		}
	}
	else{
		for (i in treeTop){
			if(treeTop[i]){
				if(document.layers){
					var text = treeTop[i].eval(node).substr(0,7);
					if(text=="dynamic"){
						menu[treeTop[i].eval(node)] = treeTop[i];
					}
				}
				else{
					var text = eval(node).substr(0,7);
					if(text=="dynamic"){
						menu[eval(node)] = treeTop[i];
					}				
				}
			}
		}
	}

	currentNode = new Tier("rootPtr",null);
	var once = 0;

	for (i in menu){
		thing = (document.layers) ? menu[i] : eval(menu[i]).style;
		if(i.indexOf(currentNode.name) != -1){ 				//If it's a child, do childish things.  =)
			currentNode.child = new Tier(i,thing,0);		//We don't really give a shit about child open/close status.
			currentNode.child.parent = currentNode;
			currentNode = currentNode.child;
		}
		else if((currentNode.parent) && (i.indexOf(currentNode.parent.name) != -1)){ 		//If I'm a sibling.  =)
			currentNode.next = new Tier(i,thing,0);
			currentNode.next.parent = currentNode.parent;
			currentNode = currentNode.next;
		}
		else{
			if(currentNode.parent) currentNode = currentNode.parent;
			currentNode.next = new Tier(i,thing,0);		//Tier headings are status:closed by default.
			currentNode = currentNode.next;
			if(!once++) rootNode = currentNode;
		}
	}
	shuffleTree();
}

// status text function
function say(what){
	window.status = what;
	return true;
}
