/* A popup menu creation engine by Anson Hoyt
 * Beta 1 October 25, 2001
 * Creates a multiple-depth menu from an array containing menu item arrays.
 * The menu is defined by an XML string stored in a named container in the HTML.
 * GDOT_menuparse.js converts the valid XML string to a valid array of menu
 * items.
 *
 * Structure of menu array: [item,item,...];
 * Structure of menu item: [innerHTML, href, class, style, description, [item,...]];
 * (All parameters are optional and may be an empty string or null.)
 *   innerHTML - any valid HTML; displayed inside its corresponding item.
 *   href - any valid URL or JavaScript; executed upon clicking an item.
 *   class - not implemented in this release.
 *   style - any valid W3C CSS inline style definition for which certain
 *           parameters may be set; unrecognized menu parameters are discarded;
 *   description - text description for an item
 */
 
/* Configuration parameters ---------------------------------------------------
 * Refer to www.w3.org for the current CSS Standard.
 */
//DEPRECATED	gdotmenu_XML_source_ID = "gdotmenu_source_XML";
	gdotmenu_width = 150;
	gdotmenu_itemHeight = "0px";
	gdotmenu_margin = "0px";
	gdotmenu_paddingLeft = "5px";
	gdotmenu_paddingRight = "0px";
	gdotmenu_paddingTop = "2px";
	gdotmenu_paddingBottom = "2px";
	gdotmenu_left = 0;
	gdotmenu_top = 70;
	gdotmenu_offset = 3;
	gdotmenu_millisecondsVisible = 300;
	
	gdotmenu_backgroundColor = "#000066";
	gdotmenu_backgroundColorOver = "#5566bb";
	gdotmenu_color = "#CCCC33";
	gdotmenu_colorOver = "ffff00";
	gdotmenu_borderWidth = "2px";
	gdotmenu_borderColor = "#7872d4";
	gdotmenu_borderStyle = "solid";
	gdotmenu_fontFamily = "verdana, helvetica";
	gdotmenu_fontWeight = "normal";
	gdotmenu_fontSize = "9pt";

	/* Unused parameters
	gdotmenu_wordSpacing = "normal";
	gdotmenu_letterSpacing = "normal";
	gdotmenu_textTransform = "none";
	gdotmenu_textAlign = "center";
	gdotmenu_textIndent = "0em";
	gdotmenu_lineHeight = "1em";
	gdotmenu_fontStyle = "normal";
	gdotmenu_fontVariant = "normal";
	gdotmenu_margin = "0px";
	gdotmenu_clear = "none";
	gdotmenu_display = "block";
	*/

/*** Global parameters ***/
	var gdot_timeoutId;
	var gdotmenu_root_id = "gdotMenu";
	var GDOT_menuIsCreated = false;
	var GDOT_menuXMLstring = "";
	gdot_testMenuArray = [
		["Item 0","","","",],
		["Item 1", "javascript:alert('Javascript code executed from href');"],
		["Item 2",,,,[["subitem 0"],["subitem 1"],["subitem 2"],["subitem 3"],]],
		["webmaster", "mailto:webmaster@dot.state.ga.us","","",[]],
	];
/* Browser Detection ----------------------------------------------------------
 * This code largely conforms to DOM standards in manipulation of the document.
 * Some deviations were made to account for the current irregular support of
 * this developing standard.
 * This code checks for MSIE user agents and supplies DOM deviant functionality
 * only to support older, non-compliant browsers.
 */
if (window.navigator.userAgent.indexOf("MSIE") != -1)	var gdot_isMSIE = true;
else var gdot_isMSIE = false;

agt = navigator.userAgent.toLowerCase();
if ( parseInt(navigator.appVersion) >= 4 && (agt.indexOf("msie 6.")!=-1) )
	var gdot_isMSIE6up = true;
else var gdot_isMSIE6up = false;

/* Opera 7 behaves more like IE */
if (window.navigator.userAgent.indexOf("Opera") != -1) var gdot_isMSIE = true;

function gdot_hideWindowedControls() {
	/* this function is only called by MSIE versions between 5 and 6 */
	var iframes = document.getElementsByTagName('IFRAME');
	var selects = document.getElementsByTagName('SELECT');
	var theElement;
	for (elem in iframes) {
		theElement = iframes[elem];
		if (typeof theElement == "object")
			gdot_hideElement(theElement);
	}
	for (elem in selects) {
		theElement = selects[elem];
		if (typeof theElement == "object")
			gdot_hideElement(theElement);
	}
}

function gdot_unhideWindowedControls() {
	var iframes = document.getElementsByTagName('IFRAME');
	var selects = document.getElementsByTagName('SELECT');
	var theElement;
	for (elem in iframes) {
		theElement = iframes[elem];
		if (typeof theElement == "object")
			gdot_showElement(theElement);
	}
	for (elem in selects) {
		theElement = selects[elem];
		if (typeof theElement == "object")
			gdot_showElement(theElement);
	}
}

function gdot_createMenu (menuArray, left, top, id, width) {
	var menu;
	menu = document.createElement("div");
	if (id == null) {
		id = gdotmenu_root_id;	//set id of root menu
		menu.style.zIndex = "1"; //order root menu above default depth
	} else {
		menu.style.zIndex = "2"; //order menu above root menu and default
	}
	menu.id = id;							//id allows menu to be retrieved
	if (width == null) width = gdotmenu_width;
	menu.style.position = "absolute";		//float menu over document
	menu.style.left = parseInt(left) + "px";	//left position: "##px"
	menu.style.top = parseInt(top) + "px";		//right position: "##px"
	left += parseInt(width) + parseInt(gdotmenu_offset);	//increment left
	//account for border width adding to width of <div> in Mozilla
	if (gdot_isMSIE == false) left += 2 * parseInt(gdotmenu_borderWidth);
	menu.itemCount = 0;							//count items for hiding algorithm
	for (var i = 0; i < menuArray.length; i++) {
		/*** Create sub items: ***/
		if (menuArray[i] != null) {
			var item = gdot_createItem (menuArray[i], left, top, id + "_" + i, width);
			if (i == menuArray.length - 1 || menuArray[i+1] == null) {
				//put bottom border on last element
				item.style.borderBottomWidth = item.style.borderTopWidth;
				item.style.borderBottomColor = gdotmenu_borderColor;
				item.style.borderBottomStyle = gdotmenu_borderStyle;
			}
			if (item.description != null) {
				//position description box
				if (menu.id == gdotmenu_root_id) { //position root menu descriptions
					item.description.style.left = left + "px";
					item.description.style.top = top - item.description.offsetHeight - Math.max(gdotmenu_offset, 0) + "px";
				} else {
					item.description.style.left = menu.style.left;
					item.description.style.top = parseInt(menu.style.top) - item.description.offsetHeight - Math.max(gdotmenu_offset, 0) + "px";
				}
			}
			menu.appendChild(item);
			if (gdot_isMSIE)
				top += parseInt(item.style.height) + parseInt(gdotmenu_margin);
			else
				top += parseInt(item.style.height) + parseInt(gdotmenu_margin) + parseInt(gdotmenu_paddingTop) + parseInt(gdotmenu_paddingBottom);
			menu.itemCount++;
		}
	}
	menu.onmouseout = function () {
		// hide menu after the timeout
		gdot_timeoutId = setTimeout("gdot_hideChildren(gdotmenu_root_id)", gdotmenu_millisecondsVisible);
	}
	if (gdot_isMSIE && !gdot_isMSIE6up) {
		menu.onmouseover = function () {
			gdot_hideWindowedControls();
			clearTimeout(gdot_timeoutId); //cancel the hiding timeout
		}
	} else {
		menu.onmouseover = function () {
			clearTimeout(gdot_timeoutId); //cancel the hiding timeout
		}
	}
	document.body.appendChild(menu);
}
function gdot_createItem (itemArray, left, top, id, width) {
	var item;
	item = document.createElement("div");
	item.innerHTML = itemArray[0];
	if (itemArray[1] != null && itemArray[1] != "")
		item.href = itemArray[1];
	else item.href = null;
	if (itemArray[4] != null) {
		/*** create description popup over menu ***/
		item.description = document.createElement("div");
		item.description = document.createElement("div");
		item.description.style.position = "absolute";
		item.description.style.zIndex = "3"; //order above menu, root menu, and default
		item.description.style.borderWidth = "1px";//gdotmenu_borderWidth;
		item.description.style.borderColor = gdotmenu_borderColor;
		item.description.style.borderStyle = gdotmenu_borderStyle;
		item.description.style.width = parseInt(gdotmenu_width) * 1.3 + "px";
		item.description.style.padding = 2 + "px";
		item.description.style.backgroundColor = "#DDDDFF";
		item.description.style.visibility = "hidden";
		titleSpan = document.createElement("div");
		titleSpan.style.color = "#333399";
		titleSpan.style.fontFamily = gdotmenu_fontFamily;
		titleSpan.style.fontWeight = "bold";
		titleSpan.style.fontSize = "8pt";
		titleSpan.innerHTML = itemArray[0];
		item.description.appendChild(titleSpan);
		descriptionSpan = document.createElement("div");
		descriptionSpan.style.fontFamily = gdotmenu_fontFamily;
		descriptionSpan.style.fontSize = "8pt";
		descriptionSpan.innerHTML = itemArray[4];
		item.description.appendChild(descriptionSpan);
		document.body.appendChild(item.description);
	}
	if (item.href == null || item.href == "") item.style.cursor = "default";
	else item.style.cursor = "hand";
	if (width == null) width = parseInt(gdotmenu_width) + "px";
	if (gdot_isMSIE) item.style.width = parseInt(width) + "px";
	else item.style.width = parseInt(width) + "px";
	item.style.marginTop = gdotmenu_margin;
	item.style.marginBottom = gdotmenu_margin;
	item.style.paddingLeft = parseInt(gdotmenu_paddingLeft) + "px";
	item.style.paddingRight = parseInt(gdotmenu_paddingRight) + "px";
	item.style.paddingTop = parseInt(gdotmenu_paddingTop) + "px";
	item.style.paddingBottom = parseInt(gdotmenu_paddingBottom) + "px";
	
	item.style.overflow = "hidden"; //stop big content from resizing container (MSIE)
	item.style.borderWidth = parseInt(gdotmenu_borderWidth) + "px";
	item.style.borderBottomWidth = "0px";
	item.style.borderColor = gdotmenu_borderColor;
	item.style.borderStyle = gdotmenu_borderStyle;	
	item.style.color = item.color = gdotmenu_color;
	item.colorOver = gdotmenu_colorOver;
	item.style.backgroundColor = item.backgroundColor = gdotmenu_backgroundColor;
	item.backgroundColorOver = gdotmenu_backgroundColorOver;
	item.style.fontFamily = gdotmenu_fontFamily;
	item.style.fontSize = parseInt(gdotmenu_fontSize) + "pt";
	item.style.fontWeight = gdotmenu_fontWeight;
	item.childId = id;

	/*** apply user defined style properties ***/
	if (itemArray[3] != null && itemArray[3] != "") {
		styleText = itemArray[3].replace(/\s+/g,"");
		var styleArray = styleText.split(/[:;]/);
		for (var i = 0; i < styleArray.length; i++) {
			switch(styleArray[i]) {
				case "color": item.style.color = styleArray[++i];
					break;
				case "background-color": item.style.backgroundColor = styleArray[++i];
					item.backgroundColor = styleArray[i];
					break;
				case "background-color-over": item.backgroundColorOver = styleArray[++i];
					break;
				case "font-weight": item.style.fontWeight = styleArray[++i];
					break;
				case "font-size": item.style.fontSize = styleArray[++i];
					break;
				case "text-align": item.style.textAlign = styleArray[++i];
					break;
				case "border-color": item.style.borderColor = styleArray[++i];
					break;
				case "border-width": item.style.borderWidth = styleArray[++i];
					break;
				case "border-style": item.style.borderStyle = styleArray[++i];
					break;
				case "width": item.style.width = styleArray[++i];
					break;
				case "height": item.style.height = styleArray[++i];
					break;
				case "padding": item.style.padding = styleArray[++i];
					break;
				case "paddingLeft": item.style.paddingLeft = styleArray[++i];
					break;
				case "paddingRight": item.style.paddingRight = styleArray[++i];
					break;
				case "paddingTop": item.style.paddingTop = styleArray[++i];
					break;
				case "paddingBottom": item.style.paddingBottom = styleArray[++i];
					break;
				case "margin": item.style.margin = styleArray[++i];
					break;
			}
		}
	}
	/* dimension adjustments */
	if (!gdot_isMSIE) {
		item.style.width = parseInt(item.style.width) - parseInt(item.style.paddingLeft) - parseInt(item.style.paddingRight) + "px";
		item.style.height = parseInt(item.style.height) - parseInt(item.style.paddingTop) - parseInt(item.style.paddingBottom) + "px";
	}
	/* determine variable height */
	document.body.appendChild(item); //append item to determine actual height
	item.style.height = Math.max(parseInt(gdotmenu_itemHeight), item.offsetHeight) + "px";

	
	/* Create submenus */
	if (itemArray[5] != null) {
		gdot_createMenu(itemArray[5], left, top, id);
		document.getElementById(item.childId).style.visibility = "hidden";
	}
	/* Set event properties */
	item.onmouseover = function() {
		if (item.href != null) {
			item.style.backgroundColor = item.backgroundColorOver;
			item.style.color = item.colorOver;
			window.status = item.href;
			if (window.setCursor != null) window.setCursor("pointer");
		}
		idArray = item.childId.split("_");
		var parentId = idArray[0];
		for (var i = 1; i < idArray.length - 1; i++)
			parentId += "_" + idArray[i];
		gdot_hideChildren(parentId); //hide all submenus of this item's parent menu
		if (document.getElementById(item.childId) != null) {
			// show this item's child menu
			document.getElementById(item.childId).style.visibility = "visible";
		}
		if (item.description != null)
			item.description.style.visibility = "visible";
	};
	item.onmouseout = function() {
		if (item.href != null) {
			item.style.backgroundColor = item.backgroundColor;
			item.style.color = item.color;
			window.status = "";
			if (window.setCursor != null) window.setCursor("auto");
		}
		if (item.description != null)
			item.description.style.visibility = "hidden";
		};
	item.onclick = function() {
		if (item.href != null && item.href != "") {
			if (item.href.search(/JavaScript:/i) == 0)
				eval(item.href.replace(/JavaScript:/i, ""));
			else
				document.location = item.href;
		}
	};
	return item;
}
function gdot_hideChildren (parentId) {
	var itemCount = document.getElementById(parentId).itemCount;
	var childMenu;
	for (var i = 0; i < itemCount; i++) {
		childMenu = (document.getElementById(parentId + "_" + i));
		if (childMenu != null && childMenu.style.visibility != "hidden") {
			gdot_hideChildren (parentId + "_" + i); //hide children's children
			childMenu.style.visibility = "hidden"; //hide children
		}
	}
	if (gdot_isMSIE && !gdot_isMSIE6up) {
		gdot_unhideWindowedControls();
	}
}

/* Executed code **************************************************************
 * Create menu and write it to the document.
 */

function GDOT_menuFromString (XMLstring) {
	GDOT_menuXMLstring = XMLstring;
	var existingOnLoad = (window.onload) ? window.onload : new Function;
	window.onload = function(){setTimeout("GDOT_doMenuFromString()", 10)};
}

function GDOT_doMenuFromString () {
	if (GDOT_menuIsCreated) return;
		var time = [];
		time[0] = (new Date()).getTime();
	var GDOT_menuArray = GDOT_parseMenu( GDOT_menuXMLstring, false );
		time[1] = (new Date()).getTime();
	gdot_createMenu(GDOT_menuArray, gdotmenu_left, gdotmenu_top);
	GDOT_menuIsCreated = true;
		time[2] = (new Date()).getTime();
		s = "MMMM Time Profile:\n";
		s += "XML parse:\t" + (time[1] - time[0]) + "ms\n";
		s += "Rendering:\t" + (time[2] - time[1]) + "ms\n";
		//alert(s);
}