var _dmsCount = 0;
var _dmsHidden = (is.ns) ? "hide" : "hidden";
var _dmsVisible = (is.ns) ? "show" : "visible";
var _dmsStyles = {};

// create the global "DynMenu" API holder
function DynMenuStyle(bgcolor, bullet, childIcon, hbgcolor, border, bordercolor, filter, font, fontsize, fontcolor, fontcolorhighlight, styleNormal, styleHighlight, spacing, padding, width){
	this.bgcolor = (!!bgcolor) ? bgcolor : "#ffffff";
	this.bullet = (!!bullet) ? bullet : "";
	this.childIcon = (!!childIcon) ? childIcon : "./lib/arrow.gif";
	this.hbgcolor = (!!hbgcolor) ? hbgcolor : "#ff9900";
	this.border = (!!border) ? border : "1";
	this.bordercolor = (!!bordercolor) ? bordercolor : "#999999";
	this.filter = (!!filter) ? filter : "";
	this.font = (!!font) ? font : "Geneva, Arial, Helvetica, sans-serif";
	this.fontsize = (!!fontsize) ? fontsize : "10";
	this.fontcolor = (!!fontcolor) ? fontcolor : "#333399";
	this.fontcolorhighlight = (!!fontcolorhighlight) ? fontcolorhighlight : this.bgcolor;
	this.styleNormal = (!!styleNormal) ? styleNormal : "";
	this.styleHighlight = (!!styleHighlight) ? styleHighlight : "";
	this.spacing = (!!spacing) ? spacing : "0";
	this.padding = (!!padding) ? padding : "5,0,0,0";
	this.width = (!!width) ? width : "150";
	_dmsCount++;
	this.name = "Style" + _dmsCount;
}

DynMenuStyle.prototype.clone = function(){
	return new DynMenuStyle(this.bgcolor, this.bullet, this.childIcon, this.hbgcolor, this.border, this.bordercolor, this.filter, this.font, this.fontsize, this.fontcolor, this.fontcolorhighlight, this.styleNormal, this.styleHighlight, this.spacing, this.padding, this.width);
}

// define "css" method
DynMenuStyle.prototype.css = function(){
	if( is.v < 4 ) return false;
	// get the padding for the layer
	aPadding = this.padding.split(",");
	sDefaultPadding = (aPadding.length == 1) ? aPadding[0] : 0;
	for( var i=aPadding.length; i < 4; i++ ) aPadding[i] = sDefaultPadding;
	
	var str = "<style type=\"text/css\" media=\"screen\">";
	if( !is.ns ){
		this._dynMenu = "position: absolute; top: 0px; left: 0px; width: " + this.width + "px; background-color: " + this.bgcolor + "; border: " + this.border + "px solid " + this.bordercolor + "; padding: " + aPadding[0] + "px " + aPadding[1] + "px " + aPadding[2] + "px " + aPadding[3] + "px; z-index:4; visibility: " + _dmsHidden + "; " + ((this.filter.length > 0) ? "filter: " + this.filter : "") + ";";
		this._dynMenuFont = "color: " + this.fontcolor + "; font: " + this.fontsize + "px " + this.font + "; text-decoration: none; cursor: hand; " +  this.styleNormal + ";";

		str += ".DynMenu" + this.name + " { " + this._dynMenu + " }";
		str += ".DynMenuFont" + this.name + " { " + this._dynMenuFont + "}";

	} else {
		this._dynMenu = "top: 0px; left: 0px; width: " + this.width + "px; layer-background-color: " + this.bgcolor + "; border: " + this.border + "px solid " + this.bordercolor + "; padding: " + aPadding[0] + "px " + aPadding[1] + "px " + aPadding[2] + "px " + aPadding[3] + "px; z-index:4; visibility: " + _dmsHidden + ";";
		this._dynMenuFont = "color: " + this.fontcolor + "; font-family: " + this.font + "; font-size: " + this.fontsize + "px; text-decoration: none; " +  this.styleNormal + ";";
		this._dynMenuFontHightlight = "color: " + this.fontcolorhighlight + "; "  + ((this.styleHighlight.length > 0) ? this.styleHighlight : "") + ";";

		str += ".DynMenu" + this.name + " { " + this._dynMenu + " }";
		str += ".DynMenuFont" + this.name + " { " + this._dynMenuFont + "}";
		str += ".DynMenuFontHighlight" + this.name + " { " + this._dynMenuFontHightlight + " }";
 	}
	str += "</style>";
	if( !is.ns ){
		str += "<style type=\"text/css\" media=\"print\">";
		str += ".DynMenu" + this.name + " { display:none;visibility:hidden; }";
		str += "</style>";
	}
	document.write(str);
	_dmsStyles[this.name] = true;
	return true;
}

// create the global "DynMenu" API holder
function _DynMenuAPI(){
	this.instances = 0;
	this.obj = new Object();
	this.imagepath = "./images/";
	this.queue = new Object();
	this.queue.init = new Array();
	this.hidden = _dmsHidden;
	this.visible = _dmsVisible;
	this.labelCase = "";
	this.elementsToHide = [];
	this.defaultStyle = new DynMenuStyle();
}
DynMenuAPI = new _DynMenuAPI();

// define "init" method
_DynMenuAPI.prototype.init = function(){
	if( is.v < 4 ) return false;
	// initialize all the queued events
	for( var i=0; i < DynMenuAPI.queue.init.length; i++ ) eval(DynMenuAPI.queue.init[i]);
	if( !is.ns ) this.getFormObjects();
	return true;
}

// get form objects to hide
_DynMenuAPI.prototype.getFormObjects = function(){
	var sHide, oForm, oField;
	sHide = ",select-one,select-multiple,";
	for( var i=0; i < document.forms.length; i++ ){
		oForm = document.forms[i];
		for( var j=0; j < oForm.elements.length; j++ ){
			oField = oForm.elements[j];
			if( sHide.indexOf("," + oField.type + ",") > -1 ) this.elementsToHide[this.elementsToHide.length] = oField;
		}
	}
}

// define "create" method
_DynMenuAPI.prototype.create = function(){
	if( is.v < 4 ) return false;
	// create all the layers
	for( id in this.obj ) this.obj[id].create();

	// add the init function to the onLoad event
	var sOnLoad = "DynMenuAPI.init();";
	if( !!window.onload ){
		sOnLoad += window.onload.toString().substring(window.onload.toString().indexOf("{") + 1, window.onload.toString().lastIndexOf("}"));
	}
	window.onload = new Function(sOnLoad);
	return true;
}
	
// define "rollOver" method
_DynMenuAPI.prototype.rollOver = function(obj, id){
	var oMenu = DynMenuAPI.obj[id.substring(0, id.lastIndexOf("_") + 1)];
	var iChild = parseInt(id.substring(id.lastIndexOf("_")+1, id.length));
	if( !is.ns ) oMenu.show();
	if( oMenu.children[iChild].childmenu != null ){
		if( is.ns ){
			var iTop = parseInt(obj.pageY);
			var iLeft =  parseInt(obj.pageX) - 10;
		} else {
			var iTop = findOffsetValue(obj, "offsetTop") + parseInt(obj.offsetTop);
			var iLeft = findOffsetValue(obj, "offsetLeft") - 10;
		}
		oMenu.children[iChild].childmenu.moveTo(iTop, iLeft + parseInt(oMenu.menuStyle.width));
		oMenu.children[iChild].childmenu.show();
	}
	this.hilite(obj, id);
}
	
// define "rollOut" method
_DynMenuAPI.prototype.rollOut = function(obj, id){
	var oMenu = DynMenuAPI.obj[id.substring(0, id.lastIndexOf("_") + 1)];
	var iChild = parseInt(id.substring(id.lastIndexOf("_")+1, id.length));
	if( !is.ns ) oMenu.hide();
	if( oMenu.children[iChild].childmenu != null ) oMenu.children[iChild].childmenu.hide();
	this.restore(obj, id);
}


// define "hilite" method
_DynMenuAPI.prototype.hilite = function(obj, id){
	var oMenu = DynMenuAPI.obj[id.substring(0, id.lastIndexOf("_") + 1)];
	var iChild = parseInt(id.substring(id.lastIndexOf("_")+1, id.length));
	if( !is.ns ){
		var objB = getObject(id + "_b");
		if( oMenu.menuStyle.styleHighlight.length > 0 ) objB.style.cssText = oMenu.menuStyle.styleHighlight;
		obj.style.backgroundColor = oMenu.menuStyle.hbgcolor;
		objB.style.color = oMenu.menuStyle.fontcolorhighlight;
	} else {
		// get layers
		if( oMenu.menuStyle.bullet.length > 0 ) var objA = obj.parentLayer.parentLayer.document.layers[id + "_a"].layers[0];
		var objB = obj.parentLayer.parentLayer.document.layers[id + "_b"].layers[0];
		var sChildBullet = "";
		if( oMenu.children[iChild].childmenu != null ) sChildBullet = "<img border=\"0\" align=\"Right\" src=\"" + oMenu.menuStyle.childIcon + "\" />";
		var text = sChildBullet + oMenu.children[iChild].label;

		// width/height
		var w = objB.clip.right;
		var h = objB.clip.height;

		// change the height of the layer
		if( oMenu.menuStyle.bullet.length > 0 ){
			objA.clip.height = h;
			objA.parentLayer.clip.height = h;
			// change the background colors
			objA.parentLayer.bgColor = oMenu.menuStyle.hbgcolor;
		}

		// change the background colors
		objB.parentLayer.bgColor = oMenu.menuStyle.hbgcolor;

		// update the text
		objB.document.open();
//		objB.document.write("<a href=\"javascript:void(0);\"><font style=\"" + oMenu.menuStyle._dynMenuFont + "\"><font style=\"" + oMenu.menuStyle._dynMenuFontHighlight + "\">" + text + "</font></font></a>");
		objB.document.write("<a href=\"javascript:void(0);\"><font class=\"DynMenuFont" + oMenu.menuStyle.name + "\"><font class=\"DynMenuFontHighlight" + oMenu.menuStyle.name + "\">" + text + "</font></font></a>");
		objB.document.close();
//		alert(obj.parentLayer.parentLayer.innerHTML);
		objB.clip.right = w;
	}
}

// define "restore" method
_DynMenuAPI.prototype.restore = function(obj, id){
	var oMenu = DynMenuAPI.obj[id.substring(0, id.lastIndexOf("_") + 1)];
	var iChild = parseInt(id.substring(id.lastIndexOf("_")+1, id.length));
	if( !is.ns ){
		obj.style.backgroundColor = oMenu.menuStyle.bgcolor;
		var objB = getObject(id + "_b");
		objB.style.color = oMenu.menuStyle.fontcolor;
		if( oMenu.menuStyle.styleHighlight.length > 0 ) objB.style.cssText = "";
	} else {
		// get layers
		if( oMenu.menuStyle.bullet.length > 0 ) var objA = obj.parentLayer.parentLayer.document.layers[id + "_a"].layers[0];
		var objB = obj.parentLayer.parentLayer.document.layers[id + "_b"].layers[0];
		var sChildBullet = "";
		if( oMenu.children[iChild].childmenu != null ) sChildBullet = "<img border=\"0\" align=\"Right\" src=\"" + oMenu.menuStyle.childIcon + "\">";
		var text = sChildBullet + oMenu.children[iChild].label;
		
		// width/height
		var w = objB.clip.right;
		var h = objB.clip.height;

		// change the background colors
		if( oMenu.menuStyle.bullet.length > 0 ) objA.parentLayer.bgColor = oMenu.menuStyle.bgcolor;
		objB.parentLayer.bgColor = oMenu.menuStyle.bgcolor;
		
		// update the text
		objB.document.open();
//		objB.document.write("<font style=\"" + oMenu.menuStyle._dynMenuFont + "\">" + text + "</font>");
		objB.document.write("<font class=\"DynMenuFont" + oMenu.menuStyle.name + "\">" + text + "</font>");
		objB.document.close();
		objB.clip.right = w;
	}
}


// define "click" method
_DynMenuAPI.prototype.click = function(obj){
	self.location.href = obj.url;
}

// define "DynMenu" constructor
function DynMenu(oParent, oStyle){
	DynMenuAPI.instances++;
	this.top = 0;
	this.left = 0;
	this.children = new Array();
	this.id = "DynMenu_" + DynMenuAPI.instances + "_";
	this.hideTimeout = null;
	this.status = null;
	this.onshow = new Function("");
	this.onhide = new Function("");

	this.menuStyle = (!!oStyle) ? oStyle : DynMenuAPI.defaultStyle.clone();
	// if( typeof _dmsStyles[this.menuStyle.name] == "undefined" ) this.menuStyle.css();

	this.parent = (!!oParent) ? oParent : null;
	if( this.parent != null ) this.parent.childmenu = this;

	DynMenuAPI.obj[this.id] = this;
}

// define "create" method
DynMenu.prototype.create = function(){
	if( is.v < 4 ) return false;
	if( typeof _dmsStyles[this.menuStyle.name] == "undefined" ) this.menuStyle.css();

	var sLayer = "";

	if( !is.ns ){	
		sLayer += "<div id=" + this.id + " class=\"DynMenu" + this.menuStyle.name + "\" onmouseover=\"DynMenuAPI.obj['" + this.id + "'].show();\" onmouseout=\"DynMenuAPI.obj['" + this.id + "'].hide();\">";
		sLayer += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"" + this.menuStyle.spacing + "\" width=\"" + this.menuStyle.width + "\">";
		for( i=0; i < this.children.length; i++ ){
			if( this.children[i].type == "item" ){
				var iW = parseInt(this.menuStyle.width);
				var sChildBullet = "";
				if( this.children[i].childmenu != null ) sChildBullet = "<img border=\"0\" align=\"Right\" src=\"" + this.menuStyle.childIcon + "\" />";
				sLayer += "<tr id=\"" + this.children[i].id + "\" onclick=\"" + this.children[i].click + "\" onmouseover=\"DynMenuAPI.rollOver(this, '" + this.children[i].id + "');\" onmouseout=\"DynMenuAPI.rollOut(this, '" + this.children[i].id + "');\">";
				if( this.menuStyle.bullet.length > 0 ){
					sLayer += "<td width=\"10\" valign=\"Top\" align=\"Center\" class=\"DynMenuFont" + this.menuStyle.name + "\"><font color=\"" + this.menuStyle.bgcolor + "\">&nbsp;" + this.menuStyle.bullet + "&nbsp;</font></td>";
					iW = parseInt(iW - 10);
				}
				sLayer += "<td width=\"" + iW + "\" class=\"DynMenuFont" + this.menuStyle.name + "\" id=\"" + this.children[i].id + "_b\">" + sChildBullet + this.children[i].label + "</td>";
				sLayer += "</tr>";
			} else if( this.children[i].type == "seperator" ){
				sLayer += "<tr>";
				sLayer += "<td width=\"" + this.menuStyle.width + "\" class=\"DynMenuSeperator" + this.menuStyle.name + "\" id=\"" + this.children[i].id + "_b\">" + this.children[i].label + "</td>";
				sLayer += "</tr>";
			}
		}
		sLayer += "</table></div>";
	} else {
		sLayer += "<layer id=\"" + this.id + "\" class=\"DynMenu" + this.menuStyle.name + "\" onmouseover=\"DynMenuAPI.obj['" + this.id + "'].show();\" onmouseout=\"DynMenuAPI.obj['" + this.id + "'].hide();\" visibility=\"" + DynMenuAPI.hidden + "\" bgcolor=\"" + this.menuStyle.bgcolor + "\">";
		sLayer += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"" + this.menuStyle.spacing + "\" width=\"" + this.menuStyle.width + "\">";
		for( i=0; i < this.children.length; i++ ){
			if( this.children[i].type == "item" ){
				var iW = parseInt(this.menuStyle.width);
				var sChildBullet = "";
				if( this.children[i].childmenu != null ) sChildBullet = "<IMG BORDER=0 ALIGN=Right SRC=\"" + this.menuStyle.childIcon + "\">";
				sLayer += "<tr>"
				if( this.menuStyle.bullet.length > 0 ){
					sLayer += "<td width=\"10\" valign=\"Top\" align=\"Center\" class=\"DynMenuFont" + this.menuStyle.name + "\">";
					sLayer += "<ilayer width=\"10\" bgcolor=\"" + this.menuStyle.bgcolor + "\" id=\"" + this.children[i].id + "_a\">";
					sLayer += "<layer width=\"10\" onfocus=\"setTimeout('" + this.children[i].click + "', 150); return false;\" onmouseover=\"DynMenuAPI.rollOver(this, '" + this.children[i].id + "');\" onmouseout=\"DynMenuAPI.rollOut(this, '" + this.children[i].id + "');\"><font color=\"" + this.menuStyle.bgcolor + "\">&nbsp;" + this.menuStyle.bullet + "&nbsp;</font></layer>";
					sLayer += "</ilayer></td>";
					iW = parseInt(iW - 10);
				}
				sLayer += "<td width=\"" + iW + "\" class=\"DynMenuFont" + this.menuStyle.name + "\">";
				sLayer += "<ilayer width=\"" + iW + "\" bgcolor=\"" + this.menuStyle.bgcolor + "\" id=\"" + this.children[i].id + "_b\">";
				sLayer += "<layer id=\"" + this.children[i].id + "\" width=\"" + iW + "\" onfocus=\"setTimeout('" + this.children[i].click + "', 150); self.focus(); return false;\" onmouseover=\"DynMenuAPI.rollOver(this, '" + this.children[i].id + "');\" onmouseout=\"DynMenuAPI.rollOut(this, '" + this.children[i].id + "');\"><font class=\"DynMenuFont" + this.menuStyle.name + "\">" + sChildBullet + this.children[i].label + "</font></layer>";
				sLayer += "</ilayer></td></tr>";
			} else if( this.children[i].type == "seperator" ){
				sLayer += "<tr>";
				sLayer += "<td width=\"" + this.menuStyle.width + "\" class=\"DynMenuSeperator" + this.menuStyle.name + "\" id=\"" + this.children[i].id + "_b\">" + this.children[i].label + "</td>";
				sLayer += "</tr>";
			}
		}
		sLayer += "</table></layer>";
	}
	document.write(sLayer);
	this.moveTo(this.top, this.left);
	DynMenuAPI.queue.init[DynMenuAPI.queue.init.length] = "DynMenuAPI.obj['" + this.id + "'].status = 'initialized'";
	return true;
}

// define "add" method
DynMenu.prototype.add = function(label, url, description){
	var url = (!!url) ? url : "javascript:void(0);";
	var i = this.children.length;
	this.children[i] = new Object();
	this.children[i].type = "item";
	this.children[i].apos = i;
	this.children[i].id = this.id + i;
	this.children[i].label = (DynMenuAPI.labelCase.toLowerCase() == "upper" ) ? label.toUpperCase() : (DynMenuAPI.labelCase.toLowerCase() == "lower" ) ? label.toLowerCase() : label;
	this.children[i].click = (url.substring(0,11) != "javascript:") ? "DynMenuAPI.click(DynMenuAPI.obj." + this.id.substring(0, this.id.lastIndexOf("_") + 1) + ".children[" + i + "]);" : url.substring(11);
	this.children[i].url = url;
	this.children[i].description = (!!description) ? description : label;
	this.children[i].childmenu = null;
	this.children[i].thismenu = this;
	return this.children[i];
}

// define "add" method
DynMenu.prototype.addSeperator = function(){
	var i = this.children.length;

	this.children[i] = new Object();
	this.children[i].type = "seperator";
	this.children[i].apos = i;
	this.children[i].id = this.id + i;
	this.children[i].label = "<hr width=\"" + this.menuStyle.width + "\" />";
	this.children[i].click = "";
	this.children[i].url = "";
	this.children[i].description = "";
	this.children[i].childmenu = null;
	this.children[i].thismenu = this;
	return this.children[i];
}

// define "show" method
DynMenu.prototype.show = function(bHideElements){
	var bHideElements = (arguments.length == 0) ? true : arguments[0];
	if( this.parent != null ){
		this.parent.thismenu.show(false);
		var oParent;
		if( is.w3c ) oParent = document.getElementById(this.parent.id);
		else if( is.ie )  oParent = document.all[this.parent.id];
		else if( is.ns )  oParent = document.layers[this.id].document.layers[this.parent.id + "_b"];
		DynMenuAPI.hilite(oParent, this.parent.id);
	}
	if( this.hideTimeout != null ) clearTimeout(this.hideTimeout);
	if( this.status == null || this.status == "visible" ) return false;
	this.status = "visible";
	this.onshow();
	if( this.children.length == 0 ) return false; // if no children, don't show empty drop down
	obj = getObject(this.id);
	if( !is.ns ) obj.style.visibility = DynMenuAPI.visible;
	else obj.visibility = DynMenuAPI.visible;
	// hide elements that layers can't overlap
	if( bHideElements ){
		DynMenuAPI.hideElements = true;
		if( is.ie )	for( var i=0; i < DynMenuAPI.elementsToHide.length; i++ ) DynMenuAPI.elementsToHide[i].style.visibility = DynMenuAPI.hidden;
		else if( is.ns ){ var oLyr = document.layers["idFormLayer"]; if( oLyr ) oLyr.visibility = DynMenuAPI.hidden; }
	}
	return true;
}

// define "hide" method
DynMenu.prototype.hide = function(bHideElements){
	if( this.parent != null ) this.parent.thismenu.hide();
	if( this.status == null ) return false;
	if( this.hideTimeout != null ) clearTimeout(this.hideTimeout);
	var iDelay = (!is.ns) ? 50 : 1;
	this.hideTimeout = setTimeout("DynMenuAPI.obj['" + this.id + "'].doHide()", iDelay);
	DynMenuAPI.hideElements = false;
	return true;
}

// define "hide" method
DynMenu.prototype.doHide = function(bHideElements){
	var bHideElements = (arguments.length == 0) ? true : arguments[0];
	var obj, oParent;
	if( this.parent != null ){
		if( is.w3c ) oParent = document.getElementById(this.parent.id);
		else if( is.ie )  oParent = document.all[this.parent.id];
		else if( is.ns )  oParent = document.layers[this.parent.id];
		DynMenuAPI.restore(oParent, this.parent.id);
	}
	if( is.w3c ) obj = document.getElementById(this.id).style;
	else if( is.ie )  obj = document.all[this.id].style;
	else if( is.ns )  obj = document.layers[this.id];
	obj.visibility = DynMenuAPI.hidden;
	this.onhide();
	this.status = "hidden";
	// if on the parent menu, then restore all hidden form fields
	if( this.parent == null && bHideElements && !DynMenuAPI.hideElements){
		if( is.ie )	for( var i=0; i < DynMenuAPI.elementsToHide.length; i++ ) DynMenuAPI.elementsToHide[i].style.visibility = DynMenuAPI.visible;
		else if( is.ns ){ var oLyr = document.layers["idFormLayer"]; if( oLyr ) oLyr.visibility = DynMenuAPI.visible; }
	}
	return true;
}

// define "moveTo" method
DynMenu.prototype.moveTo = function(top, left){
	obj = getObject(this.id);
	if( !is.ns ){
		obj.style.top = parseInt(top);
		obj.style.left = parseInt(left);
	} else {
		obj.top = parseInt(top);
		obj.left = parseInt(left);
	}
}

// define "moveToObject" method
DynMenu.prototype.moveToObject = function(oImg, align, valign, iLShift){
	var align = (!align) ? "left" : align.toLowerCase();
	var valign = (!valign) ? "bottom" : valign.toLowerCase();
	var iLShift = (!iLShift)?  0:parseFloat(0+iLShift); // int, shift to left.
	var left = parseInt(getImageLeft(oImg), 10);
	var top = parseInt(getImageTop(oImg), 10);
	
	var w = parseInt(getImageWidth(oImg), 10);
	var h = parseInt(getImageHeight(oImg), 10);
	
	oY = new Object();
	oY.left = left;
	oY.center = (left + w/2 + 1) - parseInt(this.menuStyle.width, 10)/2;
	oY.right = left + w + 1;
	oX = new Object();
	oX.top = top;
	oX.middle = top + h/2 - 1;
	oX.bottom = top + h - 1;
	// moveTo top, left
	this.moveTo(oX[valign], oY[align]-iLShift);
}

// define "attachTo" method
DynMenu.prototype.attachTo = function(oImg, align, valign, iLShift){
	var align = (!align) ? "left" : align;
	var valign = (!valign) ? "bottom" : valign;
	var iLShift = (!iLShift)?  0:parseFloat(0+iLShift); // int, shift to left.
	DynMenuAPI.queue.init[DynMenuAPI.queue.init.length] = "DynMenuAPI.obj['" + this.id + "'].moveToObject('" + oImg + "', '" + align + "', '" + valign + "', " + iLShift + ")";
}
