/* David Thompson, 1/10/2002 */
/* Shows, tracks and hides div tags as pop-up menus */
/* Designed to control menu systems created server-side by Co-operative Web Content Management Environment. */
/* Tested on IE 6 and Netscape 7. */
/* Copyright ©1999-2007 Co-operative Web. */

var divTrackMenu = Array(), divTrackActiveItem = Array();
var menuHideTimer = null;
var menuMac = navigator.userAgent.toLowerCase().indexOf("mac")!=-1;

// *******************************************************************************************************************
// FUNCTIONS TO CONTROL INITIALISATION AND CREATION OF MENUS
// *******************************************************************************************************************
function menuInit(pathToImages, cellspacing, structure, root, level) {
	var i=0;
	
	var output = "", appendoutput = "";
	
	if(level==0) {
		// Top-level DIV block.
		output += "<div id=\"dm" + root + "\" class=\"poproot\" onmouseout=\"menuHideTimer=setTimeout('menuHide(0)', 250);\" onmouseover=\"menuClearTimer();\">\n";
		output += "<table cellpadding=\"0\" cellspacing=\"" + cellspacing + "\" width=\"100%\">";
	} else {
		// Pop-up DIV block.	
		output += "<div id=\"dm" + root + "\" class=\"pop\" onmouseout=\"menuHideTimer=setTimeout('menuHide(0)', 250);\" onmouseover=\"menuClearTimer();\">\n";
		output += "<table cellpadding=\"0\" cellspacing=\"" + cellspacing + "\">";
	}
	
	for(i=0;i<structure.length;i++) {
		// structure[i][2] = 0;
		if(structure[i].length==4) {
			// Is submenu
			output += "<tr><td " + (level==0?"":"nowrap ") + "align=\"left\" id=\"di" + root + "_" + i + "\" class=\"popitem" + structure[i][2] + "\" onmouseover=\"menuShow(document.getElementById('dm" + root + "_" + i + "'), ";
			output += "this,";
			output += "this,1," + level + "," + structure[i][2] + ");\" onclick=\"window.location.href='" + pathToImages + "welcome.asp?id=" + structure[i][0] + "';menuHide(0);\"><img src=\"" + pathToImages + "images/popupmenu_arrow.gif\" align=\"right\" width=\"10\" height=\"12\" border=\"0\" style=\"margin-left:-8px;\">" + unescape(structure[i][1]) + "</td></tr>\n";
			appendoutput += menuInit(pathToImages, cellspacing, structure[i][3], root + "_" + i, level+1);
			if(structure[i][2]==1) {
				output += "<tr><td></td></tr><tr><td></td></tr>";
			} else if(structure[i][2]==2) {
				output += "<tr><td></td></tr>";
			}
		} else {	// length == 3
			// Is item
			output += "<tr><td " + (level==0?"":"nowrap ") + "id=\"di" + root + "_" + i + "\" onmouseover=\"menuHide(" + level + ");className='popitemselected" + structure[i][2] + "';\" onmouseout=\"className='popitem" + structure[i][2] + "'\" onclick=\"window.location.href='" + pathToImages + "welcome.asp?id=" + structure[i][0] + "';menuHide(0);\" class=\"popitem" + structure[i][2] + "\">" + unescape(structure[i][1]) + "</td></tr>\n";
			if(structure[i][2]==1) {
				output += "<tr><td></td></tr><tr><td></td></tr>";
			} else if(structure[i][2]==2) {
				output += "<tr><td></td></tr>";
			}
		}
	}

	output += "</table></div>\n";
	
	return output + appendoutput;
}

// *******************************************************************************************************************
// FUNCTIONS TO CONTROL "RUNTIME" MANAGEMENT OF MENUS, SUCH AS SHOWING AND HIDING POP-UPS.
// *******************************************************************************************************************
function menuShow(divMenu, elementAlignTo, elementSelectItem, alignDirection, level, specialClassID) {
	var l=0, t=0, e = elementAlignTo;
	
	// Hide an existing menu if one is visible
	menuClearTimer();
	menuHide(level);
	elementSelectItem.className = "popitemselected" + specialClassID;
	
	if((new String(divMenu.alreadyPositioned))!="done") {
		if(menuMac) {
			t = e.offsetParent.offsetParent.style.pixelTop;
			do {
				if(e.tagName.toLowerCase()=="div") {
					l += e.clientLeft-6;
				}
				if(e.tagName.toLowerCase()=="td") {				
					l += e.clientLeft;
					t += e.clientTop;
				}
				e = e.offsetParent;
			} while(e!=null)
			if(alignDirection==0) {
				// NOT YET IMP
			} else if(alignDirection==1) {
				// Fit menu to the right of the item elementAlignTo
				divMenu.style.left = l + elementAlignTo.offsetWidth;
				divMenu.style.top = t;
			} else {
				// Not yet implemented: fit menu above or to one side of item elementAlignTo
			}
			// Next line is a fudge to avoid having to specify a fixed width for the menu in pixels when the menu DIV is created, but yet
			// still have a right-aligned ">" symbol without menu line wrapping.
			// divMenu.childNodes(0).style.width = divMenu.childNodes(0).offsetWidth + 16;
			divMenu.all(0).style.width = divMenu.all(0).offsetWidth + 16;
		} else {
		do {
			l += e.offsetLeft;
			t += e.offsetTop;
			e = e.offsetParent;
		} while(e!=null)

		if(alignDirection==0) {
			// Fit menu below the item elementAlignTo
			divMenu.style.left = l;
			divMenu.style.top = t + elementAlignTo.offsetHeight + 1;
		} else if(alignDirection==1) {
			// Fit menu to the right of the item elementAlignTo
			divMenu.style.left = l + elementAlignTo.offsetWidth;
			divMenu.style.top = t;
		} else {
			// Not yet implemented: fit menu above or to one side of item elementAlignTo
		}
			// Next line(s) are a fudge to avoid having to specify a fixed width for the menu in pixels when the menu DIV is created, but yet
			// still have a right-aligned ">" symbol without menu line wrapping.
		
			// divMenu.firstChild.style.width = (divMenu.firstChild.offsetWidth + 16);
			if(divMenu.childNodes[0].tagName==undefined) {
				// Netscape
				// Why doesn't "firstChild" work in Netscape 7?
				divMenu.childNodes[1].style.width = divMenu.childNodes[1].offsetWidth+16;
			} else {
				// IE
				divMenu.childNodes[0].style.width = divMenu.childNodes[0].offsetWidth+16;
			}
		}		
		divMenu.alreadyPositioned = "done";
	}

	// Show menu item and start tracking.
	elementSelectItem.menuSpecialClass = specialClassID;
	divTrackMenu[level] = divMenu;
	divTrackActiveItem[level] = elementSelectItem;
	divMenu.style.visibility = "visible";
}


function menuClearTimer()
{
	if(menuHideTimer!=null) {
		clearTimeout(menuHideTimer);
		menuHideTimer = null;
	}
}


function menuHide(level)
{
	for(var i=level;i<divTrackMenu.length;i++) {
		if(divTrackMenu[i]!=null) {
			divTrackMenu[i].style.visibility = "hidden";
			divTrackActiveItem[i].className = "popitem" + divTrackActiveItem[i].menuSpecialClass;
			divTrackMenu[i] = null;
			divTrackActiveItem[i] = null;
		} else break;
	}
}
