var mS = new Object;
mS.items = new Object;
mS.mainMenuIdList = new Object;
mS.mainMenuIdList.length = 0;
mS.topPopupId = "";
mS.topHighlight = "";

// Fetching the correct position of an HTML element e is problematic.

// The standard method, as suggested on sites such as www.quirksmode.org,
// is to take e.offsetLeft/Top, add it to the offsetLeft/Top of e's 
// offsetParent, then add the offsetLeft/Top of that element's
// offsetParent, and so on up the chain of offsetParents until
// the document body (or an element with no offsetParent) is found.

// However, if any of the element's in this offsetParent chain have
// nonzero border or padding widths, the calculation is potentially
// incorrect. For example, if e0's offsetParent is e1 and e1's offsetParent
// is e2, but e1 has a border, then on Mozilla/IE6 we have
//    e0.offsetLeft = offset from e0 to the INSIDE of e1's border,
// whereas
//    e1.offsetLeft = offset from the OUTSIDE of e1's border to e2,
// so
//    e0.offsetLeft + e1.offsetLeft = offset from e0 to e2 MINUS b
// where b is e1's border width. And, if the document body itself has
// padding or borders, more complications apply.

// We resolve this by ensuring that

// (1) the document body has no padding, border, or margins
// (2) all elements have zero margins (those can be even more problematic
//     than borders and padding)
// (3) any time we have an element e1 with nonzero border or padding, it:
//         (a) is embedded as a statically positioned box within a borderless,
//             paddingless box e2 whose sole content is e1,
//         (b) has as its sole content another borderless, paddingless box e0


// Now, when on a browser such as IE or Mozilla where the offset... properties
// are with respect to the ancestor element that establishes the "containing"
// box in the CSS sense -- i.e., only the document body or a non-statically
// positioned box can occur as an item's offsetParent, then we will never
// see items with borders or padding in the chain of offsetParents, so can
// safely assume the offsetLeft and offsetTop are valid.

// When on a browser that does include intermediate elements in the
// offsetParent chain, any time we see an element e0 whose offsetParent
// e1 is absolutely positioned, we can go up to its parent node e2 and
// we know the offset of e0 relative to e2 is the sum of e1's border and
// padding widths (which I will call b and p respectively).
// As a safety measure, we will check that e0.offsetParent really does
// equal either 0, p, or p+b, and the e1.offsetParent really does equal
// either 0, b, or p+b before going ahead with the assumption that the
// correct offset from e0 to e2 is p+b. If not, we will return a
// "position cannot be calculated" error.

// Another problem is that IE 5.5-6 on Windows have trouble with relatively
// positioned elements. The symptom is that if e1 is a relatively positioned
// offsetParent of e0, then e0.offsetLeft may equal
//   (offset of e0 inside e1) PLUS (offset of e1's original reference point
//                                   inside e1's offsetParent)
// instead of just the offset of e0 inside e1.

// The discussion on www.quirksmode.org seems to imply that it is
// simply e1.offsetLeft that is erroneously added to e0.offsetLeft, and
// that the same problem occurs for offsetHeight as well, but my
// experiments seem to indicate that is is not e1.offsetLeft that gets
// erroneously added, but rather e1.offsetLeft as it would be before
// shifting by e1.left. Also, I have never seen the problem occur for
// offsetHeight.

// The problem seems to go away if e1 has an explicit width set.

// We will resolve this problem by ensuring that, any time we have a
// relatively positioned box A, we have an absolutely positioned box B inside
// it and we never try to take the position of anything inside A that isn't
// also in B. Then, when we see A as the offsetParent of B in our calculation,
// we know that the correct offset to use is B's left or top style property.
// As a safety measure, we will double check that the reported 
// B.offsetLeft/Top does indeed either equal that or that plus
// A.offsetLeft/Top - A.left/top.


function pixelsFromStyle(s) {
    if (s) {
	var i = s.indexOf("px");
	if (i < 0) return 0;
	return 1*s.substring(0,i);
    } else {
	return 0;
    }
}

var elementDimens = new Object;
elementDimens.sizeValid = false;
elementDimens.positionValid = false;

function findElementDimens(element) {
    var e = element;
    elementDimens.sizeValid = false;
    elementDimens.width = 0;
    elementDimens.height = 0;
    if (e.offsetWidth) elementDimens.width = e.offsetWidth;
    if (e.offsetHeight) elementDimens.height = e.offsetHeight;
    if (elementDimens.width && elementDimens.height)
	{ 
	    elementDimens.sizeValid = true;
	}

    elementDimens.positionValid = false;
    elementDimens.x = 0;
    elementDimens.y = 0;

    if (! e.offsetParent) return;
	
    while(e.offsetParent) {
	var p = e.offsetParent;
	var padTop = pixelsFromStyle
	if (! p.style) return;
	var padTop = pixelsFromStyle(p.style.paddingTop);
	var padLeft = pixelsFromStyle(p.style.paddingLeft);
	var borTop = pixelsFromStyle(p.style.borderTopWidth);
	var borLeft = pixelsFromStyle(p.style.borderLeftWidth);
	var oL = 0;
	var oT = 0;
	if (e.offsetLeft) oL = e.offsetLeft;
	if (e.offsetTop) oT = e.offsetTop;

	if (padTop || padLeft || borTop || borLeft) {
	    if (pixelsFromStyle(e.style.paddingTop)) return;
	    if (pixelsFromStyle(e.style.paddingLeft)) return;
	    if (pixelsFromStyle(e.style.borderTopWidth)) return;
	    if (pixelsFromStyle(e.style.borderLeftWidth)) return;
	    if (p.style.position) if (p.style.position == 'relative') return;
	    var gp = p.offsetParent;
	    if (! gp) return;
	    var poL = 0;
	    var poT = 0;	   
	    if (p.offsetLeft) poL = p.offsetLeft;
	    if (p.offsetTop) poL = p.offsetTop;
	    if ((oL != 0) && (oL != padLeft) && (oL != padLeft + borLeft))
		return;
	    if ((oT != 0) && (oT != padTop) && (oT != padTop + borTop))
		return;
	    if ((poL != 0) && (poL != borLeft) && (poL != padLeft + borLeft))
		return;
	    if ((poT != 0) && (poT != borTop) && (poT != padTop + borTop))
		return;
	    elementDimens.x = elementDimens.x + padLeft + borLeft;
	    elementDimens.y = elementDimens.y + padTop + borTop;
	    e = gp;
	} else {
	    if (p.style.position && (p.style.position == "relative")) {
		if (! e.style.position) return;
		if (e.style.position != "absolute") return;
		var roL = pixelsFromStyle(e.style.left);
		var roT = pixelsFromStyle(e.style.top);
		var poL = 0;
		var poT = 0;
		if (p.offsetLeft) poL = p.offsetLeft;
		if (p.offsetTop) poT = p.offsetTop;
		if (p.style.left) poL = poL - pixelsFromStyle(p.style.left);
		if (p.style.top) poT = poT - pixelsFromStyle(p.style.top);
		if (oL != roL && oL !=  roL + poL) return;
		if (oT != roT && oT != roT + poT) return;
		oL = roL;
		oT = roT;
	    }
	    elementDimens.x = elementDimens.x + oL;
	    elementDimens.y = elementDimens.y + oT;
	    e = p;
	}

    }
    elementDimens.positionValid = true;
}

// Finding the visible portion of the document. The suggested algorithm
// on sites such as www.quirksmode.org is to use self.innerWidth/Height when
// available, otherwise to use document.documentElement.clientWidth/Height or
// document.body.clientWidth/Height, whichever one has the nonzero value
// for clientHeight.

// Unfortunately this algorithm is incorrect, because innerWidth/Height
// is NOT the same as clientWidth/Height: the former includes the scrollbar;
// the latter does not. We do not want the scroll bar included.

// Therefore, if innerHeight/Width are available, we actually still look
// for clientWidth/Height on body and documentElement, and if one is
// smaller than innerWidth/Height and the other is larger, we assume that
// the smaller one is (window area minus scrollbars) and use it instead.

// Also, if innerHeight and innerWidth are not available, we verify that
// one of body.clientXXX and documentElement.clientXXX is zero before
// going ahead and using the other, just in case we encounter other
// browsers like mozilla which have nonzero clientXXX values on both
// body and documentElement, with nothing to tell us which one belongs to
// the window and which to the document. If we do find both are nonzero,
// we will use the smaller one -- which will either be the window size,
// OR will represent the entire document size if the document is smaller
// than the window -- and that's okay for our purposes, since parts of the
// window that are beyond the edge of the document may as well be outside
// the window in any of the applications where we use this function.

var visibleRegion = new Object;
function findVisibleRegion() {
    var x,y;
    var docBody, docElement;
    docBody = document.body;
    if (document.documentElement) docElement = document.documentElement;
    var toUse;
    if (self.innerHeight)
	{
	    if (docElement) {
		if ((docElement.clientHeight > self.innerHeight ||
		    docElement.clientWidth > self.innerWidth) &&
		    (docBody.clientHeight && docBody.clientWidth &&
		     docBody.clientHeight <= self.innerHeight &&
		     docBody.clientWidth <= self.innerWidth)) toUse=docBody;
		if ((docBody.clientHeight > self.innerHeight ||
		    docBody.clientWidth > self.innerWidth) &&
		    (docElement.clientHeight && docElement.clientWidth &&
		     docElement.clientHeight <= self.innerHeight &&
		     docElement.clientWidth <= self.innerWidth))
		    toUse=docElement;
	    }
	    if (toUse) {
		x = toUse.clientWidth;
		y = toUse.clientHeight;
	    } else {
		x = self.innerWidth;
		y = self.innerHeight;
	    }
	}
    else if (docElement && docElement.clientWidth && docElement.clientHeight) {
	if (docBody.clientWidth && docBody.clientHeight) {
	    if (docBody.clientWidth < docElement.clientWidth &&
		docBody.clientHeight < docElement.clientHeight) {
		toUse = docBody;
	    } else {
		if (docElement.clientWidth < docBody.clientWidth &&
		    docElement.clientHeight < docBody.clientHeight)
		    toUse = docElement;
	    }
	} else {
	    toUse = docElement;
	}
	if (toUse) {
	    x = toUse.clientWidth;
	    y = toUse.clientHeight;
	}
    } else {
	x = docBody.clientWidth;
	y = docBody.clientHeight;
    }
    if (x && y) {
	visibleRegion.width = x;
	visibleRegion.height = y;
	visibleRegion.sizeValid = true;
    } else {
	visibleRegion.sizeValid = false;
    }
    
    visibleRegion.top = 0;
    visibleRegion.left = 0;
    if (self.pageXOffset || self.pageYOffset) {
	visibleRegion.top = self.pageYOffset;
	visibleRegion.left = self.pageXOffset;
    } else if (docElement && (docElement.scrollTop || docElement.scrollLeft)) {
	visibleRegion.top = docElement.scrollTop;
	visibleRegion.left = docElement.scrollLeft;
    } else if (docBody.scrollTop || docBody.scrollLeft) {
	visibleRegion.top = docBody.scrollTop;
	visibleRegion.left = docBody.scrollLeft;	
    }
}


var coords = new Object;

function registerItem(id, parentId, text, url, classBase, popupClassBase) {
    mS.items[id] = new Object;
    mS.items[id].id = id;
    mS.items[id].parentId = parentId;
    mS.items[id].text = text;
    mS.items[id].url = url;
    mS.items[id].classBase = classBase;
    mS.items[id].popupClassBase = popupClassBase;
    mS.items[id].isHighlit = false;
    mS.items[id].popupItemIdList = new Object;
    mS.items[id].popupItemIdList.length = 0;
    mS.items[id].poppedUp = false;
    var n;
    if (parentId != "") {
	n = mS.items[parentId].popupItemIdList.length;
	mS.items[parentId].popupItemIdList[n] = id;
	mS.items[parentId].popupItemIdList.length = n+1;
    } else {
	n = mS.mainMenuIdList.length;
	mS.mainMenuIdList[n] = id;
	mS.mainMenuIdList.length = n+1;
    }
}

function checkBrowser() {
    mS.cantPop = true;
    mS.cantDoAnything = true;
    if (! document.getElementById) return;  
    var tF = document.getElementById("testFrame");
    if (! tF) return;
    if (! tF.className) return;
    mS.cantDoAnything = false;
    if (! document.body) return;
    if (! document.createElement) return;
    if (! tF.appendChild) return;
    if (! tF.insertBefore) return;
    if (! tF.removeChild) return;
   
    mS.cantPop = false;

}

function checkPixelSize (testClass) {
    var el = document.getElementById("SizeTestingFrame");
    var w = 0;
    if (el) {
	var tel = document.createElement("div");
	tel.className = testClass;
	tel.innerHTML = "Fields Institute";
	el.appendChild(tel);
 	w = tel.width;
	if (tel.offsetWidth) w = tel.offsetWidth;
	el.removeChild(tel);
    }
    return w;
}

function findBaseSize (usersize, base12size) {
	if (12 * usersize < 10 * base12size) return 9;
	else if (12 * usersize < 11 * base12size ) return 10;
	else if (12 * usersize <= 13 * base12size ) return 12;
	else if (12 * usersize <= 16 * base12size ) return 14;
	else if (12 * usersize <= 28 * base12size ) return 18;
	else return 0;
}

function findConstraintLimit (constraintsize, base12size) {
	if (! base12size) return 0;
	if (12 * constraintsize < 9 * base12size) return 0;
	else if (12 * constraintsize < 10 * base12size) return 9;
	else if (12 * constraintsize < 12 * base12size) return 10;
	else if (12 * constraintsize < 14 * base12size) return 12;
	else if (12 * constraintsize < 18 * base12size) return 14;
	else if (12 * constraintsize < 24 * base12size) return 18;
	else return 24;
}


function findSizeClasses (usersize, base12size, prefix) {
	var normalSize = findBaseSize(usersize, base12size);
	var smallSize = normalSize;
	if (normalSize == 10 && 9 * base12size > 12 * 9) smallSize = 9;
	if (normalSize == 12 && 10 * base12size > 12 * 9) smallSize = 10;
	if (normalSize == 14 && 12 * base12size > 12 * 9) smallSize = 12;
	if (normalSize == 18 && 14 * base12size > 12 * 9) smallSize = 14;
        var smallCSize = findConstraintLimit(90, base12size);
        if (smallSize < smallCSize) smallCSize = smallSize;
  
        var normalCSize = findConstraintLimit(105, base12size);
	if (normalSize < normalCSize) normalCSize = normalSize;

        var largeSize = normalSize;
	if (normalSize == 9) largeSize = 10;
	else if (normalSize == 10) largeSize = 12;
	else if (normalSize == 12) largeSize = 14;
	else if (normalSize == 14) largeSize = 18;
	else if (normalSize == 18) largeSize = 24;

        var largeCSize = findConstraintLimit(110, base12size);
	if (largeSize < largeCSize) largeCSize = largeSize;

	var hugeSize = largeSize;
	if (normalSize == 9) hugeSize = 12;
	else if (normalSize == 10) hugeSize = 14;
	else if (normalSize == 12) hugeSize = 18;
	else if (normalSize == 14) hugeSize = 24;

	var hugeCSize = findConstraintLimit(125, base12size);
	if (hugeSize < hugeCSize) hugeCSize = hugeSize;

        return prefix + "Small" + smallSize + " " +
	       prefix + "SmallC" + smallCSize + " " +
	       prefix + "Normal" + normalSize + " " +
	       prefix + "NormalC" + normalCSize + " " +
               prefix + "Large" + largeSize + " " +
               prefix + "LargeC" + largeCSize + " " +
               prefix + "Huge" + hugeSize + " " +
               prefix + "HugeC" + hugeCSize ;

}


function setupMenus() {
    checkBrowser();
    if (mS.cantDoAnything) return;
    var i;
    for (i=0; i < mS.mainMenuIdList.length; i++) {
	var id = mS.mainMenuIdList[i];
	var item = mS.items[id];
	item.frame = document.getElementById(id);
	item.interior = document.getElementById(id + "i");
	item.frame.onmouseover = itemMouseOver;
	item.frame.onclick = itemClick;
	item.frame.onmouseout = handleMouseOut;
	if (item.popupItemIdList.length > 0) {
	    setupPopupMenu(id);
	}
    }

    var usersize = checkPixelSize("TestDefault");
    var ser12size = checkPixelSize("TestSerif");
    var san12size = checkPixelSize("TestSans");
    if (usersize && ser12size && san12size) {
	if (usersize < 50) usersize = 50;
	if (usersize > 150) usersize = 150;
	var serclasses = findSizeClasses(usersize, ser12size, "Ser");
	var sanclasses = findSizeClasses(usersize, san12size, "San");
	var el = document.getElementById("SizeClasses");
	if (el) el.className = serclasses + " " + sanclasses;
	if (el && window.location && window.location.hash &&
	    window.location.hash != '' && window.location.hash != '#') {
		if (window.location.hash.substring(0,1) == '#')
		    window.location.hash = window.location.hash.substring (
			1, window.location.hash.length) ;
	    }
    }
    findVisibleRegion();
    var bsize = document.getElementById("browserSize");
    if (bsize && visibleRegion.sizeValid) {
	bsize.innerHTML = '<img src="/systemfiles/1x1_transparent.gif?' +
		visibleRegion.width + '_' + visibleRegion.height + '">';
    }

}
window.onload = setupMenus;

function setupPopupMenu(parentId) {
    if (mS.cantPop) return;
    var parent = mS.items[parentId];
    var frame = document.createElement("div");
    frame.id = parentId + "m";
    frame.className = mS.items[parentId].popupClassBase + "Frame";
    frame.onmouseover = menuMouseOver;
    frame.onmouseout = handleMouseOut;
    var buffer = document.createElement("div");
    buffer.id = parentId + "b";
    buffer.className = mS.items[parentId].popupClassBase + "Border";
    frame.appendChild(buffer);  
    var interior = document.createElement("div");
    interior.id = parentId + "mi";
    interior.className = mS.items[parentId].popupClassBase + "Interior";
    buffer.appendChild(interior);  
    mS.items[parentId].popupFrame = frame;
    mS.items[parentId].popupBorder = buffer;
    mS.items[parentId].popupInterior = interior;
    mS.items[parentId].frameIsOffset = false;
    var i;
    for (i=0; i < parent.popupItemIdList.length; i++) {
	var itemId = parent.popupItemIdList[i];
	var item = mS.items[itemId];
	item.frame = document.createElement("div");
	item.frame.id = itemId;
	item.frame.className = item.classBase;;
	item.interior = document.createElement("div");
	item.interior.id = itemId + "i";
	item.interior.className = item.classBase + "Interior";
	item.interior.innerHTML =
	    '<a href="' + item.url + '">' + item.text + '</a>';
	item.frame.appendChild(item.interior);
	item.frame.onmouseover = itemMouseOver;
	item.frame.onmouseout = handleMouseOut;
	item.frame.onclick = itemClick;
	interior.appendChild(item.frame);
	if (item.popupItemIdList.length > 0) {
	    setupPopupMenu(itemId);
	}
   }
}

function popDownMenus(stopAtId, stopForMouse, mouseX, mouseY) {
    var stopAtId2 = stopAtId;
    while (stopAtId2 != "" && ! mS.items[stopAtId2].poppedUp) {
	stopAtId2 = mS.items[stopAtId2].parentId;
    }
    while (mS.topPopupId != "" &&
	   mS.topPopupId != stopAtId2 &&
	   ! (stopForMouse && mouseInItem(mouseX, mouseY, mS.topPopupId)) &&
	   ! (stopForMouse && mouseInMenu(mouseX, mouseY, mS.topPopupId))) {
	popDownTopMenu();
    }
}

function popDownTopMenu() {
    var id = mS.topPopupId;
    if (id == "") { return; }
    var item = mS.items[id];
    var i;
    for (i=0; i < item.popupItemIdList.length; i++) {
	unhighlightItem(item.popupItemIdList[i]);
    }
    mS.topPopupId = item.parentId;
    item.frame.removeChild(item.popupFrame);
    item.poppedUp = false;
}

function unhighlightItem(id) {
    var item = mS.items[id];
    item.frame.className = item.classBase;
    item.interior.className = item.classBase + "Interior";   
    item.isHighlit = false;
    mS.topHighlight = ""; 
    window.defaultStatus = "";
    window.status = "";
}

function unhighlightAll() {
    var id = mS.topPopupId;
    var currentItems;
    if (id == "") {
	currentItems = mS.mainMenuIdList;
    } else {
	currentItems = mS.items[id].popupItemIdList;
    }
    for (var i=0; i < currentItems.length; i++) {
	unhighlightItem(currentItems[i]);
    }
}

function highlightItem(id) {
    var item = mS.items[id];
    if (item.isHighlit) {
	if (mS.topHighlight == "") {
    	    window.defaultStatus = item.url;
	    window.status = item.url;
	    mS.topHighlight = id;
	}
	return;
    }
    popDownMenus(id, false, 0, 0);
    var parentItems;
    if (item.parentId == "") {
	parentItems = mS.mainMenuIdList;
    } else {
	parentItems = mS.items[item.parentId].popupItemIdList;
    }
    var i;
    for (i=0; i < parentItems.length; i++) {
	if (parentItems[i] != id) unhighlightItem(parentItems[i]);
    }
    item.frame.className = item.classBase + "Highlit";
    item.interior.className = item.classBase + "InteriorHighlit";
    item.isHighlit = true;
    window.defaultStatus = item.url;
    window.status = item.url;
    mS.topHighlight = id;
}



function mouseInItem(x, y, id) {
    findElementDimens(mS.items[id].frame);
    if (! elementDimens.positionValid) return false;
    if (! elementDimens.sizeValid) return false;

    if (x < elementDimens.x) { return false; }
    if (x >= elementDimens.x + elementDimens.width) { return false; }
    if (y < elementDimens.y) { return false; }
    if (y >= elementDimens.y + elementDimens.height) { return false; }
    return true;
}

function mouseInMenu(x, y, id) {
    findElementDimens(mS.items[id].popupFrame);
    if (! elementDimens.positionValid) return false;
    if (! elementDimens.sizeValid) return false;

    if (x < elementDimens.x) { return false; }
    if (x >= elementDimens.x + elementDimens.width) { return false; }
    if (y < elementDimens.y) { return false; }
    if (y >= elementDimens.y + elementDimens.height) { return false; }
    return true;
}


function itemMouseOver(e) {
    var itemid = this.id;
    var item = mS.items[itemid];
    if (item.poppedUp) {
	var target;
	if (!e) e = window.event;  
	if (e) {
	    if (e.target) target = e.target;
	    else if (e.srcElement) target = e.srcElement;
	}
	while (target) { 
	    if (target.id && target.id == item.popupFrame.id) {
		return true;
	    } else {
		target = target.parentNode;
	    }
	}
	unhighlightAll();	
    } 
    highlightItem(itemid);
    popUpMenu(itemid);
    return true;
}

function itemClick(e) {
    var itemid = this.id;
    var item = mS.items[itemid];
    if (item.poppedUp) {
	var target;
	if (!e) e = window.event;  
	if (e) {
	    if (e.target) target = e.target;
	    else if (e.srcElement) target = e.srcElement;
	}
	while (target) { 
	    if (target.id && target.id == item.popupFrame.id) {
		return;
	    } else {
		target = target.parentNode;
	    }
	}
    }
    window.location = item.url;

}

function menuMouseOver(e) {
    var item = this.parentNode;
    if (item) {
	var itemid = item.id;
	highlightItem(itemid);
	popUpMenu(itemid);
    }
}


function popUpMenu(id) {
    var item = mS.items[id];
    if (item.popupItemIdList.length > 0 && ! item.poppedUp) {
	var parentId = item.parentId;
	if (parentId != "") {
	    popUpMenu(parentId);
	}
	if (item.frameIsOffset) {
	    item.popupFrame.style.top = item.oldFrameTop + "px";
	    item.frameIsOffset = false;
	}
	item.frame.insertBefore(item.popupFrame, item.interior);
	findElementDimens(item.popupFrame);
	if (! elementDimens.positionValid) {
	    item.frame.removeChild(item.popupFrame);
	    mS.cantPop = true;
	    return;
	}
	findVisibleRegion();
	if (visibleRegion.sizeValid && elementDimens.sizeValid) {
	    var bottom = elementDimens.y + elementDimens.height;
	    var visbottom = visibleRegion.top + visibleRegion.height;
	    var offset = 0;
	    if (bottom >= visbottom) {
		offset = bottom - visbottom + 1;
		if (elementDimens.y - offset < visibleRegion.top) {
		    offset = elementDimens.y - visibleRegion.top;
		}
	    }
	    if (offset) {
		item.frameIsOffset = true;
		item.oldFrameTop = pixelsFromStyle(item.popupFrame.style.top);
		item.popupFrame.style.top = (item.oldFrameTop - offset) + "px";
	    }
	}

	item.poppedUp = true;
	mS.topPopupId = id;
    }
}


function handleMouseOut(e) {
    var relTarg;
    if (!e) e = window.event;
    if (!e) {
	mS.cantPop = true;
	popDownMenus("", false, 0, 0);
	return;
    }
    var targ;
    if (e.target) targ = e.target;
    else targ = e.srcElement;
    var relTargValid = true;
    if (e.relatedTarget) relTarg = e.relatedTarget;
    else if (e.toElement) relTarg = e.toElement;
    else relTargValid = false;

    if (relTargValid) {
	relTargValid = false;
	if (relTarg.nodeName) {
	    if (relTarg.nodeName != "BODY") relTargValid = true;
	}
    } 

    while (relTargValid && relTarg) {
	if (relTarg.nodeName)
	    if (relTarg.nodeName == "DIV") 
		if (relTarg.id) 
		    if (mS.items[relTarg.id])
			     return;
 	relTarg = relTarg.parentNode;
    }
    if (relTargValid) {
	popDownMenus("", false, 0, 0);
	unhighlightAll();
	return;
    }

 
    var mousePosFound = false;
    var posx;
    var posy;
    if (e.pageX || e.pageY)
	{
	    posx = e.pageX;
	    posy = e.pageY;
	    mousePosFound = true;
	}
    else if (e.clientX || e.clientY)
	{
	    posx = e.clientX + document.body.scrollLeft;
	    posy = e.clientY + document.body.scrollTop;
	    mousePosFound = true;
	}
    findVisibleRegion();
    if (visibleRegion.sizeValid) {
	if (posx < visibleRegion.x ||
	    posx >= visibleRegion.x + visibleRegion.height ||
	    posy < visibleRegion.y ||
	    posy >= visibleRegion.y + visibleRegion.height)
	    mousePosFound = false;
    }
    popDownMenus("", mousePosFound, posx, posy, 0);
    unhighlightAll();

}  


