var dom = (document.getElementById) ? true : false;
var ie = (document.all) ? true : false;
var ns = (document.layers) ? true : false;

var nsSuperLayerArr = new Array();

function showNSLayers() {
	if(!ns) return false;
	for(x=0; x<nsSuperLayerArr.length; x++) {
		eval(nsSuperLayerArr[x]+"._showInit()");
	}
}

/*********************************/
/* Start of AgencyMenuBar Object */
/*********************************/
var currentAgencyMenu = null;

function hideAgencyMenuBar(menubar) { menubar.hideChildren(); }

function AgencyMenuBar(id, left, top, timeout, zindex) {

	this.id = id;
	this.left = left;
	this.top = top;
	this.timeout = timeout || 500;
	this.zindex = zindex || "50";

	this.timer = 0;
	this.children = new Array();
}

AgencyMenuBar.prototype.addChild = function(child) {
	if(ns) { child.left += this.left; child.top += this.top; }
	child.parent = null;  // cannot have parent above this area
	child.menubar = this;
	this.children[this.children.length] = child;
}

AgencyMenuBar.prototype.initialize = function() {
var doc = window.document;
   for(i=0; i < this.children.length; i++) { this.children[i].initialize(); }
}

AgencyMenuBar.prototype.initialize2 = function() {
	for(i=0; i < this.children.length; i++) { this.children[i].initialize2(); }
}

AgencyMenuBar.prototype.hideChildren = function() {
	for(i=0; i < this.children.length; i++) { this.children[i].hideSubMenu(); }
}

AgencyMenuBar.prototype.hideAllExcept = function(child) {
	while(child.parent) { child = child.parent; }
	for(i=0; i < this.children.length; i++) {
		if(this.children[i].id != child.id) { this.children[i].hideSubMenu(); }
	}
}

AgencyMenuBar.prototype.startTimer = function() {
	currentAgencyMenu = this;
	this.timer = setTimeout("hideAgencyMenuBar(currentAgencyMenu)", this.timeout);
	this.timer2 = setTimeout("selectShow()", 500);
}

AgencyMenuBar.prototype.stopTimer = function() { clearTimeout(this.timer); clearTimeout(this.timer2); }

/******************************/
/* Start of AgencyMenu Object */
/******************************/

// adjLeft and adjTop are optional parameters for eaily adjusting sub-menu positioning
function AgencyMenu(parentmenu, id, name, url, left, top, width, height, bgcolor, abgcolor, stylesheet, showsub, adjLeft, adjTop) {

	this.id = id;
	this.name = name;
	this.url = url || "javascript:void(0)";
	this.doc = window.document;

	this.parentMenuId = eval(parentmenu);

	this.left = left;
	this.top = top;
	this.width = width;
	this.height = height;

	// Properties added for easily adjusting sub-menu positioning
	// These values will be added to child adjustment values when addChild() is called
	this.adjLeft = adjLeft || 0;
	this.adjTop = adjTop || 0;

	this.showsub = showsub || false;

	this.children = new Array();
	this.menubar = null;
	this.parent = null;

	this.layer = null;
	this.sublayer = null;

	this.timer = 0;

	this.bgcolor_normal = bgcolor;
	this.bgcolor_active = abgcolor;

	this.style = stylesheet;

	if(ns && parentmenu.menubar) { // If not the menubar
		this.left += parentmenu.menubar.left;
		this.top += parentmenu.menubar.top;
	}
	parentmenu.addChild(this);

	if(ns) {
		if(this.bgcolor_normal == "transparent") { this.bgcolor_normal = null; }
		if(this.bgcolor_active == "transparent") { this.bgcolor_active = null; }

		this.doc.writeln("<style type='text/css'>");

		this.doc.writeln("#n" + this.id + " { position:absolute;");
		this.doc.writeln("left: " + this.left + ";");
		this.doc.writeln("top: " + this.top + ";");
		this.doc.writeln("z-index: "+this.menubar.zindex+";");
		this.doc.writeln("visibility: hidden;");
		this.doc.writeln("}");

		this.doc.writeln("#a" + this.id + " { position:absolute;");
		this.doc.writeln("left: " + this.left + ";");
		this.doc.writeln("top: " + this.top + ";");
		this.doc.writeln("z-index: "+this.menubar.zindex+";");
		this.doc.writeln("visibility: hidden;");
		this.doc.writeln("}");

		this.doc.writeln("#s" + this.id + " { position:absolute;");
		this.doc.writeln("left: " + this.left + ";");
		this.doc.writeln("top: " + this.top + ";");
		this.doc.writeln("z-index: "+this.menubar.zindex+";");
		this.doc.writeln("visibility: hidden;");
		this.doc.writeln("}");

		this.doc.writeln("</style>\n");
	}
}



// fix for <select> tag leak
function selectHide() {
selectbox = document.getElementsByTagName("select");
  for (i = 0; i != selectbox.length; i++) {
	selectbox[i].style.visibility = "hidden";}
}


function selectShow() { 
selectbox = document.getElementsByTagName("select");
for (i = 0; i != selectbox.length; i++) {
	selectbox[i].style.visibility = "visible";}
}



AgencyMenu.prototype.addChild = function(child) {
	this.children[this.children.length] = child;
	child.parent = this;
	child.menubar = this.menubar;

	// Adjust left and top attributes of child based on adjLeft and adjTop
	child.left = child.left + this.adjLeft;
	child.top = child.top + this.adjTop;
	// Cascade adjLeft and adjTop values to child
	child.adjLeft = child.adjLeft + this.adjLeft;
	child.adjTop = child.adjTop + this.adjTop;
}

 var _tmpLayerVar = "";
 var _navLayer = "";

AgencyMenu.prototype.show = function() {
	this.menubar.stopTimer();
	if(dom || ie) {
		if(this.parentMenuId.id == "menuBar"){
			if(_tmpLayerVar) _tmpLayerVar.hideSubMenu();
			if(_navLayer) _navLayer.hideSubMenu();
			_navLayer = this;
		} else {
			if( (this.showsub || _tmpLayerVar) && !this.parent.showsub) {
				if(_tmpLayerVar)_tmpLayerVar.hideSubMenu();
				if(this.showsub)_tmpLayerVar = this;
			}
		}
		this.showSubMenu();
	} else {
		this.menubar.hideAllExcept(this);
		this.hideAllExcept();
		this.showSubMenu();
	}
}

AgencyMenu.prototype.hideAllExcept = function() {
	if(this.parent) {
		for(i=0; i < this.parent.children.length; i++) {
			if(this.parent.children[i] != this.id) { this.parent.children[i].hideSubMenu(); }
			else { this.parent.hideAllExcept(); }
		}
	}
}

AgencyMenu.prototype.showSubMenu = function() {
	for(var i=0; i < this.children.length; i++) { this.children[i]._show(); }
}

AgencyMenu.prototype.hideSubMenu = function() {
	for(var i=0; i < this.children.length; i++) {
		this.children[i]._hide();
		this.children[i].hideSubMenu();
	}
}

AgencyMenu.prototype.gotoURL = function() { window.location.href = this.url; }

initHideArray = new Array();

if(dom || ie || bw.ie4mac) {  // W3C compliant browsers and IE 4.x browsers

	AgencyMenu.prototype.initialize = function() {
		var menu = this;
		var style = "";
		var div = "";

		if(this.name && this.name.match(/.*endcap.*/i)) {
			style = style + "z-index:"+this.zindex+"; position:absolute;";
			style = style + " width:"+this.width+"px; height:"+this.height+"px;";
			style = style + " left:"+this.left+"px; top:"+this.top+"px;";
			div = "<div id='"+this.id+"' style='"+style+"' onmouseover='selectHide(); '>"+this.name+"</div>";
			this.doc.writeln( div );
		} else if(this.id && this.id.match(/Spacer.*/i)) {
			style = style + "z-index:"+this.zindex+"; position:absolute; background-color:#CCCCCC;";
			style = style + " width:"+this.width+"px; height:"+this.height+"px;";
			style = style + " left:"+this.left+"px; top:"+this.top+"px;";
			div = "<div id='"+this.id+"' style='"+style+"' class='menuSpacer' onmouseover='selectHide(); '><br></div>";
			this.doc.writeln( div );
		} else {
			// In order to keep the position style attribute as absolute, the parent div of this div
			// must not have top and left style attributes.
			style = "" +
				" z-index: "+this.zindex+"; position: absolute;" +
				" cursor: hand; font-weight: bold; color: "+this.fgcolor_normal+"; background-color: "+this.bgcolor_normal+";" +
				" width: "+this.width+"px; height: "+this.height+"px;" +
				" left: "+this.left+"px; top: "+this.top+"px;" +
				" font-family: "+this.family+"; font-size: "+this.size+"px; "+((this.parentMenuId.id == "menuBar")?"":"") +
				"";
			div = "<div id='"+this.id+"'" +
				" style='"+style+"'" +
				" onmouseover='"+this.id+".hideSubMenu(); "+this.id+".show(); selectHide();  "+this.id+".activeColor();'" +
				" onmouseout='"+this.id+".menubar.startTimer();  "+this.id+".normalColor();'" +
				">";
			this.doc.writeln( div );

			tmpstyle = "";
			if(bw.pc) tmpstyle = this.style;
			else tmpstyle = this.style+"MAC";
			this.doc.writeln("<a href='"+this.url+"' class='"+tmpstyle+"'><nowrap><span class='"+tmpstyle+"'>"+this.name+"</span></nowrap></a>");

			if(this.showsub) {
				var newleft = this.width - 20;
				// Initial visibility of this element set to hidden so it will not default to being show in Netscape/Mozilla.
				var substyle = "" +
					" z-index: 50; position: absolute;" +
					" cursor: hand; font-weight: bold; color: "+this.fgcolor_normal+"; background-color: "+this.bgcolor_normal+"; " +
					" width: 20px; height: "+this.height+"px; " +
					" left: "+newleft+"px; top: 0px; " +
					" font-family: "+this.family+"; font-size: "+this.size+"px;" +
					"";
				var subdiv = "" +
					"<div id='showsub"+this.id+"' style='"+substyle+"'>" +
						"<a href='"+this.url+"' class='"+this.style+"'><img src='/caas/images/spacer.gif' height='3' width='1' border='0' alt=''><br>&raquo;</a>" +
					"</div>";
				this.doc.writeln( subdiv );

				// not popluating for Netscape/Mozilla
				this.sublayer = (dom) ? this.doc.getElementById("showsub" + this.id) : this.doc.all["showsub" + this.id];
			}

			this.doc.writeln("</div>");
		}

		for(var i=0; i < this.children.length; i++) {
			this.children[i].initialize();
		}

	}

	AgencyMenu.prototype.initialize2 = function() {
		// this.layer and this.sublayer tested and set here for Netscape/Mozilla
		this.layer = (dom) ? this.doc.getElementById(this.id) : this.doc.all[this.id];
		this.sublayer = (dom) ? this.doc.getElementById("showsub"+this.id) : this.doc.all["showsub"+this.id];

		for(var i=0; i < this.children.length; i++) {
			this.children[i].initialize2();
			this.children[i]._hide();
		}
	}

	AgencyMenu.prototype._show = function() {
		this.layer.style.visibility = "visible";
		if(this.showsub) { this.sublayer.style.visibility = "visible"; }
	}

	AgencyMenu.prototype._hide = function() {
		this.layer.style.visibility = "hidden";
		if(this.showsub) { this.sublayer.style.visibility = "hidden"; }
	}

	AgencyMenu.prototype.addEventHandler = function(eventName, handler) {
		var menu = this;
		this.layer[eventName] = function(event) {
			if(event != null) {  // For netscape based event model
				return handler(menu, event.type, event.x, event.y, event.which, event.which,
				((event.modifiers & Event.SHIFT_MASK) != 0),
				((event.modifiers & Event.CTRL_MASK) != 0),
				((event.modifiers & Event.ALT_MASK) != 0));
			} else {  // For IE based event model
				event = window.event;
				return handler(menu, event.type, event.x, event.y, event.button, event.keyCode, event.shiftKey, event.ctrlKey, event.altKey);
			}
		}
	}

	AgencyMenu.prototype.activeColor = function() {
		this.layer.style.color = this.fgcolor_active;
		this.layer.style.backgroundColor = this.bgcolor_active;
		if(this.showsub) {
			this.sublayer.style.color = this.fgcolor_active;
			this.sublayer.style.backgroundColor = this.bgcolor_active;
		}
	}

	AgencyMenu.prototype.normalColor = function() {
		this.layer.style.color = this.fgcolor_normal;
		this.layer.style.backgroundColor = this.bgcolor_normal;
		if(this.showsub) {
			this.sublayer.style.color = this.fgcolor_normal;
			this.sublayer.style.backgroundColor = this.bgcolor_normal;
		}
	}
}


else {  // Pray that it is Netscape compliant browser

	AgencyMenu._eventmasks = {
		onmouseover:Event.MOUSEOVER, onmouseout:Event.MOUSEOUT, onmousedown:Event.MOUSEDOWN, onclick:Event.CLICK, onmouseup:Event.MOUSEUP
	};

	AgencyMenu.prototype.initialize = function() {
		var menu = this;
		if(this.parentMenuId.id == "menuBar") {
			nsSuperLayerArr[nsSuperLayerArr.length] = this.id;
		}

		tmpstyle = "";
		if(bw.pc) tmpstyle = this.style;
		else tmpstyle = this.style+"MAC";

		var newwidth = this.width - ((this.showsub) ? 20 : 0);
		this.doc.writeln("<div id='n"+ this.id + "' class='n' visibility='hide'>");
		this.doc.writeln("<table cellpadding='0' cellspacing='0' border='0' width='" + this.width + "' height='" + this.height + "'" + ((this.bgcolor_normal) ? " bgcolor='"+ this.bgcolor_normal +"'>" : ">") + "<tr><td width='"+newwidth+"'><a href='"+this.url+"' class='"+tmpstyle+"'>" + this.name + "</a></td>");
		if(this.showsub) {
			this.doc.writeln("<td width='20'><a href='"+this.url+"' class='"+tmpstyle+"'>&raquo;</a></td>");
		}
		this.doc.writeln("</tr></table></div>");
		this.normal = this.doc['n' + this.id];

		this.doc.writeln("<div id='a"+ this.id + "' class='a' visibility='hide'>");
		this.doc.writeln("<table cellpadding='0' cellspacing='0' border='0' width='" + this.width + "' height='" + this.height + "'" + ((this.bgcolor_active) ? " bgcolor='"+ this.bgcolor_active +"'>" : ">") + "<tr><td width='"+newwidth+"'><a href='"+this.url+"' class='"+tmpstyle+"'>" + this.name + "</a></td>");
		if(this.showsub) {
			this.doc.writeln("<td width='20'><a href='"+this.url+"' class='"+tmpstyle+"'>&raquo;</a></td>");
		}
		this.doc.writeln("</tr></table></div>");
		this.active = this.doc['a' + this.id];

		this.doc.writeln("<div id='s"+ this.id + "' class='s'><table cellpadding='0' cellspacing='0' border='0' width='" + this.width + "' height='" + this.height + "'><tr><td>&nbsp;</td></tr></table></div>");
		this.superlayer = this.doc['s' + this.id];

		for(var i=0; i < this.children.length; i++) { this.children[i].initialize(); }
		if(!this.parent) { this._show(); }

		this.addEventHandler("onmouseover", function() { menu.show(); menu.activeColor(); });
		this.addEventHandler("onmouseout", function() { menu.menubar.startTimer(); menu.normalColor(); });
		this.addEventHandler("onmouseup", function() { menu.gotoURL(); });
		this._hideInit();
	}

	AgencyMenu.prototype._show = function() {
		this.normal.visibility = "show";
		this.active.visibility = "hide";
		this.superlayer.visibility = "show";
	}

	AgencyMenu.prototype._hide = function() {
		this.normal.visibility = "hide";
		this.active.visibility = "hide";
		this.superlayer.visibility = "hide";
	}
	AgencyMenu.prototype._hideInit = function() {
		this.normal.visibility = "hide";
		this.active.visibility = "hide";
	}

	AgencyMenu.prototype._showInit = function() {
		this.normal.visibility = "show";
		this.superlayer.visibility = "show";
	}

	AgencyMenu.prototype.addEventHandler = function(eventName, handler) {
		var layer = this.superlayer;
		layer.captureEvents(AgencyMenu._eventmasks[eventName]);
		var menu = this;
		layer[eventName] = function(event) {
			return handler(menu, event.type, event.x, event.y, event.which, event.which,
				((event.modifiers & Event.SHIFT_MASK) != 0),
				((event.modifiers & Event.CTRL_MASK) != 0),
				((event.modifiers & Event.ALT_MASK) != 0));
		}
	}

	AgencyMenu.prototype.activeColor = function() {
		this.normal.visibility = "hide";
		this.active.visibility = "show";
	}

	AgencyMenu.prototype.normalColor = function() {
		this.normal.visibility = "show";
		this.active.visibility = "hide";
	}
}
