// andyweb drop-down menu system
// with target attributes for menu items
// if you're reading this with the intention of stealing this script
// i should warn you, it's not worth it.
// for all you know i've decided to hunt down everyone who has done it
// and hacked into their system to wreak my revenge.
// is it worth it? is it safe?????
//
//take a chance, make my day!
//
// INITIALISATION //
// globals

var br = null;
var blnOverMenu = false;
var strCSSMenuBox = "";

// detect browser type
if (navigator.appName == "Netscape")
	br = "N";
else if (navigator.appName == "Microsoft Internet Explorer")
	br = "IE";

if (br == "N") window.onLoad = setResize();

// setup 'event' object for Netscape
if (!window.event && window.captureEvents) {
	window.captureEvents(Event.MOUSEMOVE);
	window.onmousemove = getCursorHandler;
	window.onclick = getCursorHandler;
	window.event = new Object;
}

// write styles into document
strCSSMenuBox += ".ddmMenuBox {";
strCSSMenuBox += "   background-color: #cccccc;";
strCSSMenuBox += "   padding-left: 5px;";
strCSSMenuBox += "   padding-right: 2px;";
strCSSMenuBox += "   border-style: solid;";
strCSSMenuBox += "   border-width: 1px 1px 1px 1px;";
strCSSMenuBox += "   border-color: #00ff00;";
if (br == "IE") {
	strCSSMenuBox += "   position: absolute;";
	strCSSMenuBox += "   visibility: hidden;";
}
strCSSMenuBox += "}";

document.writeln("<style type=text/css>");
document.writeln(strCSSMenuBox);
document.writeln("</style>");

// FUNCTION DEFINITIONS /////
function reDo() { window.location.reload(); }

function setResize() { setTimeout("window.onResize=reDo", 5000); }

function getCursorHandler(e) {
	window.event.offsetX = e.pageX;
	window.event.offsetY = e.pageY;
	window.event.srcElement = e.target;
	// route the event back to the intended function
	if (routeEvent(e) == false) return false;
	else return true;
}

function MenuItem(strLabel, strLink, strTarget) {
	this.strLabel = strLabel;
	this.strLink = strLink;
	this.strTarget = strTarget;
}

function Menu(strID, intLeft, intTop, objMenuItems) {
	this.objMenuItems = objMenuItems;
	this.strID = strID;
	this.intLeft = intLeft;
	this.intTop = intTop;

	buildBoxStartTag(strID);
	for (var i=0; i < objMenuItems.length; i++) {
		if (objMenuItems[i].strTarget == "") {
			strTargetAttr = "";
		} else {
			strTargetAttr = "target=\"" + objMenuItems[i].strTarget + "\"";
		}
		document.write("<a class=\"ddmMenuItem\" " + strTargetAttr + " href=\"" + objMenuItems[i].strLink + "\">" + objMenuItems[i].strLabel + "</a><br>");
	}
	buildBoxEndTag();
}

function buildBoxStartTag(strID) {
	if (br == "IE")
		document.write("<div id=\"" + strID + "\" onMouseOver=\"blnOverMenu = true;\" onMouseOut=\"moveOffMenu(\'" + strID + "\');\" class=\"ddmMenuBox\">");
	else
		document.write("<layer id=\"" + strID + "\" bgcolor=#FFFFFF clip=\"auto auto auto auto\" visibility=hidden onMouseOver=\"blnOverMenu = true;\" onMouseOut=\"moveOffMenu(\'" + strID + "\');\" class=\"ddmMenuBox\">");
}

function buildBoxEndTag() {
	(br == "IE") ? document.write("</div>") : document.write("</layer>");
}

function findXPos(objEl) {
	var intX = objEl.offsetLeft;
	while (objEl.offsetParent) {
		objEl = objEl.offsetParent;
		intX += objEl.offsetLeft;
	}
	return intX;
}

function findYPos(objEl) {
	var intY = objEl.offsetTop;
	while (objEl.offsetParent) {
		objEl = objEl.offsetParent;
		intY += objEl.offsetTop;
	}
	return intY;
}

function showMenu(strID) {
	var intLnkTop, intLnkLeft, intLnkWidth, intLnkHeight;
	var objLink = window.event.srcElement;
	
	blnOverMenu = true;	
	if (br == "IE") {
		objBox = window.document.all[strID].style;
		intLnkTop = findYPos(objLink);
		intLnkLeft = findXPos(objLink);
		intLnkWidth = objLink.offsetWidth;
		intLnkHeight = objLink.offsetHeight;
	}
	else {
		for (var i=0; i < menuTree.length; i++)
			if (menuTree[i].strID == strID) {
				intLnkTop = menuTree[i].intTop;
				intLnkLeft = menuTree[i].intLeft;
			}
		objBox = window.document.layers[strID];
	}
	objBox.top = (br == "IE") ? (intLnkTop + intLnkHeight + 2) : intLnkTop;
	objBox.left = (intLnkLeft > 9) ? (intLnkLeft - 5) : 0;
	objBox.width = (intLnkLeft > 9) ? (intLnkWidth + 10) : (intLnkWidth + 5);
	hideAll();
	objBox.visibility = "visible";
}

function hideAll() {
	if (!blnOverMenu)
		for (var i=0; i < menuTree.length; i++)
			(br == "IE") ? document.all[menuTree[i].strID].style.visibility = "hidden" : window.document[menuTree[i].strID].visibility = "hidden";			
}

function hideMenu() {
	blnOverMenu = false;
	setTimeout("hideAll()", 100);
}

function moveOffMenu(strID) {
	var intTop, intLeft, intBott, intRight;
	
	if (br == "N") {
		objDiv = window.document.layers[strID];
		intTop = objDiv.y + 2;
		intLeft = objDiv.x + 2;
		intBott = intTop + objDiv.clip.height - 4;
		intRight = intLeft + objDiv.clip.width - 4;
	}
	else if (br == "IE") {
		objDiv = document.all[strID];
		intTop = objDiv.offsetTop + 2;
		intLeft = objDiv.offsetLeft + 2;
		intBott = intTop + objDiv.clientHeight - 4;
		intRight = intLeft + objDiv.clientWidth - 4;
	}
	if (window.event.offsetY < intTop || window.event.offsetY > intBott ||
		window.event.offsetX < intLeft || window.event.offsetX > intRight)
		hideMenu();
}
