var dhtmlMenuAlerts = 0

//DHTML MENU CONSTRUCTOR
function DHTMLMenu(){
	//default values
	this.timeout = false;
	this.delay = 500;
	this.position = 'absolute';
	this.bgOff = '#666666';
	this.bgOn = '#666666';
	this.fontOff = '#000000';
	this.fontOn = '#000000';
	this.layout = 'horizontal';
	this.inherit = true;
	this.padding = 3;
	this.spacing = 1;
	this.border = 0;
	this.menus = new Object();
	this.actives = new Array();
	this.maxActiveIndex = null;
}
DHTMLMenu.prototype.addMenu = function(id,d,w,x,y){
	this.menus[id] = new Menu(id,d,w,x,y);
}
DHTMLMenu.prototype.write = function (){
	if (dhtmlMenuAlerts) alert('Creating DHTML Layers...');
	var dhtml_menu = this;
	var z = 100;
	var m = this.menus;
	for (var o in m){
		var i = m[o].items;
		var visibility = (m[o].depth!=0)? 'hidden' : 'visible';
		var vert1 = '<tr>';
		var vert2 = '</tr>';
		var hor1 = '';
		var hor2 = '';
		if (dhtml_menu.layout=='horizontal' && m[o].depth==0){
			vert1 = "";
			vert2 = "";
			hor1 = "<tr>"
			hor2 = "</tr>"
		}

		var s = '<div id="'+ m[o].id +'Layer" style="position:'+this.position+'; z-index:'+z+'; left:'+m[o].x+'; top:'+m[o].y+'; visibility:'+visibility+'">\n';
		s += '<table width="'+m[o].w+'" cellpadding="'+dhtml_menu.padding+'" cellspacing="'+dhtml_menu.spacing+'" border="'+dhtml_menu.border+'">'+hor1+'\n';
		for (var p in i){ s += vert1+'<td id="'+i[p].id+'" style="font-size:11px; font-family:verdana" bgcolor="'+dhtml_menu.bgOff+'">'+i[p].html+'</td>'+vert2+'\n'; }
		s += hor2+'</table>\n</div>\n';

		if (dhtmlMenuAlerts) alert('Menu child layer:\n'+s);
		document.writeln(s);
		z++;
	}
}
DHTMLMenu.prototype.assign = function (){
	var m = this.menus;
	for (var o in m){
		m[o].elmt = new DynLayer(m[o].id+'Layer');
		this.actives[m[o].depth] = null;
		var n = m[o].items;
		for (var p in n){
			n[p].dhtmlmenu = this;
			n[p].menu = m[o];
			n[p].elmt = getObjRef(n[p].id);
			n[p].elmt.dhtmlmenu = this;
			n[p].elmt.menu = m[o];
			n[p].elmt.item = n[p];
			n[p].elmt.onmouseover = n[p].on;
			n[p].elmt.onmouseout = n[p].off;
			n[p].elmt.onclick = n[p].changeURL;
			n[p].child = (this.menus[n[p].id])? this.menus[n[p].id] : false;
			if (dhtmlMenuAlerts) alert(n[p].id + ' child: ' + n[p].child);
		}
	}
}
DHTMLMenu.prototype.create = function (){
	this.write();
	this.assign();
}


//MENU CONSTRUCTOR
function Menu(menuID,menuDepth,width,xCord,yCord){
	this.id = menuID;
	this.depth = menuDepth;
	this.w = width;
	this.x = xCord;
	this.y = yCord;
	this.elmt = null;
	this.items = new Object();
}
Menu.prototype.addItem = function (itemID,html,url){ this.items[itemID] = new MenuItem(itemID,html,url); }


//MENU ITEM CONSTRUCTOR
function MenuItem(itemID,html,url){
	this.id = itemID;
	this.html = html;
	this.url = url;
	this.child = false;
	this.elmt = null;
	this.obj = this.id + "MenuItem";
	eval(this.obj + " = this");
}
MenuItem.prototype.clearActives = function(x){
	var a = this.dhtmlmenu.actives;
	var x = (x!=undefined)? x : 0;
	for (var i=a.length-1; i>=x; i--){ if (a[i]!=null) a[i].turnOff(); }
	this.assignMaxActiveIndex();
}
MenuItem.prototype.assignMaxActiveIndex = function(){
	for (var i=0; i<this.dhtmlmenu.actives.length; i++){ if (this.dhtmlmenu.actives[i]!=null) this.dhtmlmenu.maxActiveIndex = i; }

}
MenuItem.prototype.changeURL = function(){
	document.location.href = 'http://'+this.item.url;
}
MenuItem.prototype.on = function (){
	clearTimeout(this.dhtmlmenu.timeout);

	var i = this.item
	var h = this.dhtmlmenu
	var a = this.dhtmlmenu.actives
	var x = this.dhtmlmenu.maxActiveIndex
	var d = this.menu.depth

	if (a[0]!=null){
		if (d < x){
			if (i == a[d]){ i.clearActives(d+1) } else { i.clearActives(d) }
		} else if (d==x){
			if (i!=a[d]) a[d].turnOff();
		}
	}

	i.turnOn();
	i.assignMaxActiveIndex()
}
MenuItem.prototype.off = function (){
	this.dhtmlmenu.timeout = setTimeout(this.item.obj + ".clearActives();",1000);
}
MenuItem.prototype.turnOn = function (){
	this.elmt.style.backgroundColor = this.dhtmlmenu.bgOn;
	this.elmt.style.color = this.dhtmlmenu.fontOn;
	this.elmt.style.cursor = (browser.ns5)? 'pointer' : 'hand';
	if (this.child) this.child.elmt.show();
	this.dhtmlmenu.actives[this.menu.depth] = this;
	window.status = 'http://'+this.url;
}
MenuItem.prototype.turnOff = function (){
	this.elmt.style.backgroundColor = this.dhtmlmenu.bgOff;
	this.elmt.style.color = this.dhtmlmenu.fontOff;
	if (this.child) this.child.elmt.hide();
	this.dhtmlmenu.actives[this.menu.depth] = null;
	window.status = 'http://'+this.url;
}

