function Menu() {
	var a=arguments;

	this.depth = 0;
	this.open = false;
	this.children = new Array();
	this.index = a[0]||0;
}
Menu.prototype.browse=function(link) {
	window.location = link;
//	alert (link);
}
Menu.prototype.addChild=function(content,link,bgColors,images) {
	var newMenu = new Menu(this.children.length);
	newMenu.parent = this;
	newMenu.top = this.top;
	newMenu.cellHeight = this.cellHeight;
	newMenu.depth=this.depth+1;
	newMenu.width=this.width;
	newMenu.height=this.height;
	newMenu.link=link;
	newMenu.x=this.x;
	newMenu.y=this.y;
	newMenu.z=this.z;
	if (images) {
		newMenu.imageOff = images[0];
		newMenu.imageOn = images[1];
	} else {
		/*	code to handle array of contents for swap, but netscape has a bug with interpreting a single string as an array
			if (content && (content[1] != null)) {
			newMenu.content = this.addClass(content[0]);
			newMenu.contentOver = this.addClass(content[1]);
		} else */ newMenu.content = this.addClass(content);
		if (bgColors) {
			newMenu.swapBgColor=bgColors[1];
			if (bgColors[0] != null) newMenu.bgColor=bgColors[0]; else newMenu.bgColor=this.bgColor;
		}
	}
	this.children[this.children.length] = newMenu;
	return (newMenu);
}
Menu.prototype.render=function() {
	var i,x,y;
	var layerLis;
	var layer2Lis;
	var menuObj;
	
	for (i=0; i<this.children.length; ++i) this.children[i].render();
	if (this.depth == 0) return;

	x = 1;
	if (this.imageOn == null) {
		this.layer = new DynLayer(null,x,0,this.width-2,this.height-2,this.bgColor,'visible',this.z);
		this.layer.setHTML(this.content);
	} else {
		var normal = DynImage.getImage(this.imageOff);
		var mouseover = DynImage.getImage(this.imageOn);
		this.layer=new ButtonImage();
		this.layer.moveTo(x,0);
		this.layer.setImages(normal,mouseover);
		this.layer.setZIndex(this.z);
	}
	this.layer.menuObj = this;
	
	layerLis = new EventListener(DynAPI.document)
	if ((this.swapBgColor != null) || (this.contentOver != null)) {
		layerLis.onmouseover=function(e) {
			menuObj = e.getSource().menuObj;
			if (menuObj.swapBgColor != null) e.getSource().setBgColor(menuObj.swapBgColor);
			if (menuObj.contentOver != null) e.getSource().setHTML(menuObj.contentOver);
		}
	}
	if ((this.swapBgColor != null) || (this.contentOver != null)) {
		layerLis.onmouseout=function(e) {
			menuObj = e.getSource().menuObj;
			if (menuObj.swapBgColor != null) e.getSource().setBgColor(menuObj.bgColor);
			if (menuObj.contentOver != null) e.getSource().setHTML(menuObj.content);
		}
	}
	layerLis.onmousedown=function(e) {
		menuObj = e.getSource().menuObj;
		var link = null;
		if ((menuObj.link != '') && (menuObj.link != null)) link = menuObj.link;
		if (menuObj.hasChildren() && menuObj.link) {
			if (link && !menuObj.open) menuObj.top.goToLink = link;
			menuObj.openClose();
		} else if (link) menuObj.browse(link);
	}
	layerLis.onmousemove=function(e) { e.cancelBrowserEvent(); } // non selectable text
	this.layer.addEventListener(layerLis);

	var prev = this.getPrevSibling();
	var makeBgLayer = true;
	if (prev && prev.bgLayer) {
		if (prev.hasChildren() || this.maskLayer) {
			this.y = prev.bgLayer.y+prev.bgLayer.h;
		} else {
			this.bgLayer = prev.bgLayer;		
			makeBgLayer = false;
		}
	}
	if (makeBgLayer) {
		if (this.maskLayer)
			this.bgLayer = new DynLayer(null,this.x+1,this.y+1,this.width-2,0,this.bgColor,'hidden',this.z);
		else
			this.bgLayer = new DynLayer(null,this.x,this.y,this.width,0,'#000000','hidden',this.z);
		this.bgLayer.menuObj = this;
		layer2Lis = new EventListener(DynAPI.document)
		layer2Lis.onpathstop=function(e) {
			e.getSource().inMotion = false;
			menuObj = e.getSource().menuObj;
			if (menuObj.top.moving.parent.open == false) menuObj.top.moving.setVisible(false);
			if (--menuObj.top.numInMotion == 0) {
				menuObj.top.inMotion = false;
				menuObj.popQueue();
			}
			if (menuObj.top.initOpen) { menuObj.top.initOpen=false; menuObj.initVisible(); }
		}
		layer2Lis.onpathrun=function(e) {
			menuObj = e.getSource().menuObj;
			if (menuObj.maskLayer) {
				obj = e.getSource();
				var newH;
				if ((newH = menuObj.top.highest-obj.y+25) > 0)
					menuObj.bgLayer.setHeight(newH);
				else 
					menuObj.bgLayer.setHeight(0);
			}
		}
		this.bgLayer.addEventListener(layer2Lis);
		this.bgLayer.path = new PathAnimation(this.bgLayer);
		this.bgLayer.path.sleep(20);
		DynAPI.document.addChild(this.bgLayer);
		this.bgLayer.css.cursor = "hand";
	}

	this.layer.setY(this.bgLayer.h+1);

	this.bgLayer.addChild(this.layer);
	this.bgLayer.setHeight(this.bgLayer.h+this.layer.h+1);
	this.setHighest(this.bgLayer.y+this.bgLayer.h);
	
	this.layer.css.cursor = "hand"; // 'wait', 'hand', 'crosshair', 'move'
}
Menu.prototype.initMask=function() {
	var maskLayer = this.addChild(null,null,new Array(bottomColor,bottomColor));
	maskLayer.maskLayer = true;
}	
Menu.prototype.init=function() {
	this.initMask();
	this.render();
	var top = this.top;
	var i;
	
	var lastBgLayer = top.children[top.children.length-2].bgLayer;
	lastBgLayer.setHeight(lastBgLayer.h+1);

	var maskLayer = top.children[top.children.length-1].bgLayer;
	maskLayer.setHeight(this.top.highest-maskLayer.y+15);

	top.speed = speed;
	if (openMenu != 0) {
		var openObj = top.children[openMenu-1];
		top.openMenuLink = top.children[openMenu-1].link;
		top.initOpen = true;
		speed = 999;
		if (openObj.hasChildren()) if (openObj.link) openObj.openClose();
	} else this.initVisible();
}
Menu.prototype.initVisible=function() {
	var top = this.top;
	speed = top.speed;
	for (i=0; i<top.children.length; i++) {
		if (top.children[i].bgLayer.visible == false) {
			top.children[i].bgLayer.setVisible(true);
		}
	}
}
Menu.prototype.setVisible=function(visibility) {
	this.bgLayer.setVisible(visibility);
}
Menu.prototype.move=function(direction,num) {
	if (this.bgLayer.inMotion != true) {
		var newY = ((direction?1:-1)*((this.height-1)*(num?num:1)))+this.bgLayer.y;
		++this.top.numInMotion;
		this.bgLayer.path.playAnimation(PathAnimation.line(this.bgLayer.x, this.bgLayer.y, this.bgLayer.x, newY, speed));
		this.bgLayer.inMotion = true;
	}
}
Menu.prototype.setCellHeight=function(height) {
	this.cellHeight = height;
	this.height = height;
}
Menu.prototype.getSiblings=function() {
	var s = new Array();
	var i;
	if (this.parent == null) return (s);
	var kids = this.parent.children;
	for (i=0; i<kids.length; ++i) {
		if (i != this.index) s[s.length] = kids[i];
	}
	return (s);
}
Menu.prototype.getPrevSibling=function() {
	if (this.parent == null) return (null);
	var kids = this.parent.children;
	if (this.index > 0) return (kids[this.index-1]); else return (null);
}
Menu.prototype.moveLowerSiblings=function(direction) {
	if (direction == null) direction = this.open;
	var siblings = this.getSiblings();
	for (i=this.index; i<siblings.length; ++i) {
		siblings[i].move(direction,this.children.length);
	}
	if (this.parent) this.parent.moveLowerSiblings(direction);
}
Menu.prototype.hasChildren=function() {
	return (this.children.length>0);
}
Menu.prototype.setHighest=function(height) {
	if (this.top.highest < height) this.top.highest = height;
}
Menu.prototype.openClose=function(skipSiblings) {
	if (this.open) this.open = false; else this.open = true;
	if (this.open == false) {
		for (i=0; i<this.children.length; ++i) {
			if (this.children[i].hasChildren()) if (this.children[i].open == true) this.children[i].openClose();
		}
	}
	if (skipSiblings != true) {
		var siblings = this.getSiblings();
		for (i=0; i<siblings.length; ++i) {
			if (siblings[i].open) {
				siblings[i].openClose(true);
			}
		}
	}
	this.pushQueue();
	this.popQueue();
}
Menu.prototype.initTop=function() {
	this.top = this;
	this.inMotion = false;
	this.numInMotion = 0;
	this.queue = new Queue();
	this.top.highest = 0;
	this.top.goToLink = null;
}
Menu.prototype.popQueue=function() {
	if (this.top.inMotion == false) {
		var next = this.top.queue.pop();
		if (next) {
			var child = next.children[0];
			this.top.inMotion = true;
			this.top.moving = child;
			child.setVisible(true);
			child.bgLayer.setY(next.bgLayer.y + next.layer.y + next.layer.h);
			next.moveLowerSiblings();
		} else {
			if ((this.top.goToLink) && (this.top.openMenuLink != this.top.goToLink)) this.browse(this.top.goToLink);
		}
	}
}
Menu.prototype.pushQueue=function() {
	this.top.queue.push(this);
}
Menu.prototype.addClass=function(content) {
	if ((content == null) || (content == "")) return (null);
	return ('<span class="menu">'+content+'</span>');
}



function Queue() {
	this.queue = new Array();
	this.front = 0;
	this.back = 0;
}
Queue.prototype.push=function(obj) {
	this.queue[this.front++] = obj;
}
Queue.prototype.pop=function() {
	if (this.front > this.back) {
		return (this.queue[this.back++]);
	}
}
/*Queue.prototype.debug=function(call,cur) {
	var d='';
	var i;
	for (i=this.back; i<this.front; ++i) {
		d+=this.queue[i].content+'\n';
	}
	alert ('Call: '+call+' '+cur.content+'\nFront: '+this.front+'\nBack:  '+this.back+'\nQueue is: \n'+d);
}*/
