/**
 * Menu 0.8 990602
 * by gary smith, July 1997
 * Copyright (c) 1997-1999 Netscape Communications Corp.
 *
 * Netscape grants you a royalty free license to use or modify this
 * software provided that this copyright notice appears on all copies.
 * This software is provided "AS IS," without a warranty of any kind.
 */

var m_IE = (document.all)? 1: 0;
var m_FFX = (document.getElementById)? 1: 0;
var m_onload = 0;
var m_write_menus;
window.ActiveMenuItem = null;

var EQ_ie4 = (document.all)? 1: 0;
var EQ_opa = ((EQ_ie4)&&(navigator.userAgent.indexOf('Opera') != -1))? 1: 0;
var EQ_ffx = (document.getElementById)? 1: 0;
var EQ_ns4 = (document.layers)? 1: 0;
var EQ_ns6 = ((EQ_ffx)&&(!document.all))? 1: 0;

function Menu(label) {
    this.version = "0.99";
    this.type = "Menu";
    this.childMenuIcon = "/EQ/icons/ar.gif";
    this.childMenuIconHilite = "/EQ/icons/ard.gif";
    this.items = new Array();
    this.actions = new Array();
    this.colors = new Array();
    this.mouseovers = new Array();
    this.mouseouts = new Array();
    this.childMenus = new Array();

    this.addMenuItem = addMenuItem;
    this.addMenuSeparator = addMenuSeparator;
    this.writeMenus = writeMenus;
    this.showMenu = showMenu;
    this.hideMenu = hideMenu;
    this.hideChildMenu = hideChildMenu;
    this.mouseTracker = mouseTracker;
    document.onmousemove = this.mouseTracker;
    document.onmouseup = this.hideMenu;

    if (!window.menus) window.menus = new Array();
    this.label = label || "menuLabel" + window.menus.length;
    window.menus[this.label] = this;
    window.menus[window.menus.length] = this;
    if (!window.activeMenus) window.activeMenus = new Array();
    if (!window.menuContainers) window.menuContainers = new Array();
    if (window.MenuAPI) MenuAPI(this);
}

function addMenuItem(label, action, color, mouseover, mouseout) {
    this.items[this.items.length] = label;
    this.actions[this.actions.length] = action;
    this.colors[this.colors.length] = color;
    this.mouseovers[this.mouseovers.length] = mouseover;
    this.mouseouts[this.mouseouts.length] = mouseout;
}

function addMenuSeparator() {
    this.items[this.items.length] = "separator";
    this.actions[this.actions.length] = "";
}

function writeMenus(container)
{

	if	(!m_onload)
	{
		m_write_menus = 1;
		return;
	}

    if (!container) {
        if (!GetObject ("menuContainer")) 
		{
		    if  (document.body)
		    {
				container = document.createElement('span');
				container.setAttribute('id', 'menuContainer');
       		    document.body.appendChild(container);
		    }
			else
	            document.writeln('<span ID="menuContainer"></span>');
		}
        container = GetObject ("menuContainer");
    }

    if (!container && !window.delayWriteMenus) {
        window.delayWriteMenus = this.writeMenus;
        setTimeout('delayWriteMenus()', 3000);
        return;
    }
    if (!container) return;
    container.isContainer = "menuContainer" + menuContainers.length;
    menuContainers[menuContainers.length] = container;
    container.menus = new Array();
    for (var i=0; i<window.menus.length; i++) 
        container.menus[i] = window.menus[i];
    window.menus.length = 0;
    var countMenus = 0;
    var countItems = 0;
    var top = 0;
    var content = '';
    var proto;
    for (var i=0; i<container.menus.length; i++, countMenus++) {
        var menu = container.menus[i];
        proto = menu.prototypeStyles || this.prototypeStyles || menu;
        content += '<DIV class="m_frame" ID="m_l' + countMenus +
			'" STYLE="position:absolute;left:10;top:' + (i * 100) +
			'; display: none"><DIV ID="m_x' + countMenus + '" class="m_popup" onMouseOut="hideMenuOut(event, this);">\n';
        var x=i;
        for (var i=0; i<menu.items.length; i++) {
            var item = menu.items[i];
            var childMenu = false;
            var defaultHeight = 20;
            var defaultIndent = 15;
            if (item.label) {
                item = item.label;
                childMenu = true;
            } else if (item.indexOf(".gif") != -1 && item.indexOf("<IMG") == -1) {
                item = '<IMG SRC="' + item + '" NAME="m_i'+ countItems +'Img">';
                defaultIndent = 0;
            }
            proto.menuItemHeight = proto.menuItemHeight || defaultHeight;
            proto.menuItemIndent = proto.menuItemIndent || defaultIndent;
			var dTag    = '<DIV ID="m_i' + countItems +
				'" class="m_item" onMouseOver="mIOver(this);" onClick="mIAction(this);">';
            if (item == "separator") {
                content += '<DIV class="m_separator"></DIV>';
            } else if (childMenu) {
                content += ( dTag + item + ' <IMG ID="m_c'+ countItems +'" SRC="'+ proto.childMenuIcon +'"></DIV>');
            } else {
                content += ( dTag + item + '</DIV>');
            }
            countItems++;
        }
        content += '\n</DIV></DIV>\n';
        i=x;
    }

    if ((m_FFX)||(container.innerHTML)) {
        container.innerHTML = content;
    } else {
        container.document.open("text/html");
        container.document.writeln(content);
        container.document.close();
    }

    proto = null;
	var	l_obj;
	var menuCount = 0;
	for (var x=0; x<container.menus.length; x++)
	{
		var menu = GetObject ("m_l" + x);
		var menu_x = container.menus[x];
		menu_x.menuDiv = menu;
		menu_x.menuDiv.Menu = menu_x;
		menu_x.menuDiv.Menu.container = menu;
		proto = menu_x.prototypeStyles || this.prototypeStyles || menu_x;
		proto.menuItemWidth = proto.menuItemWidth || 200;
		for (var i=0; i<menu_x.items.length; i++)
		{
			var l = GetObject ("m_i" + menuCount);
			if	(l == null)  continue;
			if	(i > 0)
				var l_prev = GetObject ("m_i" + (menuCount - 1));
			l.Menu = menu_x;
			proto = menu_x.prototypeStyles || this.prototypeStyles || menu_x;
			l.action = menu_x.actions[i];
			l.mouseover = menu_x.mouseovers[x];
			l.mouseout  = menu_x.mouseouts[x];
			var childItem = GetObject ("m_c" + menuCount);
			if (childItem)
			{
				l.childMenu = menu_x.items[i].menuDiv;
				menu_x.childMenus[menu_x.childMenus.length] = l.childMenu;
			}
			menuCount++;
		}
		if (menu.Menu.enableTracker)
		{
			menu.Menu.disableHide = true;
			setMenuTracker(menu.Menu);
		}
	}
	l_obj = GetObject ("menuContainer");

    window.wroteMenu = true;
}

function mIOver(l, a)
{
    l = l || this;
    a = a || window.ActiveMenuItem;
    if ((l.style)&&(l.Menu)) {
        if (a)	a.className = 'm_item';
		else a = new Object();

        if (l.mouseover && l.id != a.id) {
            if (l.mouseover.length > 4) {
                var ext = l.mouseover.substring(l.mouseover.length-4);
                if (ext == ".gif" || ext == ".jpg") {
                    l.document.images[l.id + "Img"].src = l.mouseover;
                } else {
                    eval("" + l.mouseover);
                }
            }
        }
//		if (l.isSeparator) return;
		l.className = 'm_item_on';
        l.Menu.hideChildMenu(l);
    }
	else
		return;
    window.ActiveMenuItem = l;
}

function mIAction(l) {
    l = window.ActiveMenuItem;
    if (!l) return;
	if ((window.ActiveMenu)&&(!window.ActiveMenu.Menu.disableHide))
		hideActiveMenus(window.ActiveMenu.menuDiv || null);
    if (l.action) {
        eval("" + l.action);
    }
}

function showMenu(menu, x, y, child, dx, dy) {
    if (!window.wroteMenu) return false;

	var l = menu.menuDiv || menu;
	hideActiveMenus(l);
	if (typeof(menu) == "string")
	{
		l = GetObject (menu);
		var len = menuContainers.length;
		for (var n=0; n < len; n++)
		{
			var menus = menuContainers[n].menus;
			l = menus[menu];
			for (var i=0; i < menus.length; i++)
			{
				if (menu == menus[i].label) l = menus[i].menuDiv;
				if (l) break;
			}
		}
	}
	window.ActiveMenu = l;
	l.style.display = 'block';

	// Firefox: We don't have original event object so we need to
	// simulate one
//	var e = new Object;
//	e.pageX = M_ParseCoord(x) || window.pageX;
//	e.pageY = M_ParseCoord(y) || window.pageY;
	if	(dx == null)	dx = 5;
	if	(dy == null)	dy = 5;
//	EQ_DivPosition(window.event || e, l.id, dx, dy, 1);
	x = M_ParseCoord(x) || window.pageX;
	y = M_ParseCoord(y) || window.pageY;
	EQ_DivPosition(null, l.id, x + dx, y + dy, 1);
	
	l.Menu.xOffset = document.body.scrollLeft;
	l.Menu.yOffset = document.body.scrollTop;

    if (menu)
        window.activeMenus[window.activeMenus.length] = l;

	return false;
}

function hideMenuOut (event, element)
{
	if	(event == null)  event = window.event;
	var tgt = event.toElement || event.relatedTarget || null;

	if	(tgt)
	{
		//	Check if it's the same menu or a child of current menu
		var id = element.id.replace(/^m_x(\d+)$/, "$1");
		var obj = document.getElementById('m_l' + id);
		if	((obj.Menu)&&((obj == tgt)||(isChild (obj.Menu, tgt))))
			return true;
	}

	while (tgt != null)
	{
		if	(tgt == element)
			return true;
		tgt = tgt.parentNode;
	}

	return hideMenu(event, element);
}

function isChild (p_element, p_child)
{

	if	(!p_element.childMenus)		return 0;

	var children = p_element.childMenus;
	for (var i = 0; i < children.length; i++)
	{
		if	(children[i] == p_child)
			return 1;
	}

	return 0;
}

function hideMenu(p_event, p_el) {
    var l = p_el || window.ActiveMenu;
    if (!l) return true;
    if (l.menuDiv)
		l = l.menuDiv;
	else
	{
		if ((this.style)&&(this.style.display == 'block'))
			l = this;
	}
    if (l.menuDiv)	l = l.menuDiv;
    var a = window.ActiveMenuItem;
    document.saveMousemove = document.onmousemove;
    document.onmousemove = mouseTracker;
    if (window.ActiveMenu) {
        document.onmousedown=null;
        if (a)	a.style.className = 'm_item';
	}
    if (window.ActiveMenu) {
        if (window.ActiveMenu.Menu) {
            if (window.ActiveMenu.Menu.disableHide) return true;
        }
    }
    hideActiveMenus(l);
    return true;
}

function hideChildMenu(menuDiv)
{
    var l = menuDiv || this;
    for (var i=0; i < l.Menu.childMenus.length; i++) {
		l.Menu.childMenus[i].style.display = 'none';
        l.Menu.childMenus[i].Menu.hideChildMenu(l.Menu.childMenus[i]);
    }
    if (l.childMenu)
	{
		var child = l.childMenu;
		child.style.zIndex = l.Menu.menuDiv.style.zIndex + 1;
		var x = window.pageX || parseInt(child.style.left || child.offsetLeft);
		var y = window.pageY || parseInt(child.style.top || child.offsetTop);
		// Firefox: We don't have original event object so we need to simulate one
//		var e = new Object;
//		e.pageX = x;
//		e.pageY = y;
		child.style.left = (x + 5) + 'px';
		child.style.top = y + 'px';
		child.style.display = 'block';
		// Display the submenu before adjusting its position.
//		EQ_DivPosition(window.event || e, child.id, 5, 1, 1);
		EQ_DivPosition(null, child.id, x + 5, y + 1, 1);

        if (!l.childMenu.disableHide) 
            window.activeMenus[window.activeMenus.length] = child;
    }
}

function hideActiveMenus(l)
{
    if (!window.activeMenus) return;
	for (var i=0; i < window.activeMenus.length; i++)
	{
		if (!activeMenus[i]) return;
		if (activeMenus[i].style)
			activeMenus[i].style.display = 'none';
	}
	document.onmousemove = mouseTracker;
	window.activeMenus.length = 0;
}

function mouseTracker(e)
{
    e = e || window.Event || window.event;
    window.pageX = e.pageX || e.clientX;
    window.pageY = e.pageY || e.clientY;
}

function setMenuTracker(menu)
{
    if (!window.menuTrackers) window.menuTrackers = new Array();
    menuTrackers[menuTrackers.length] = menu;
    window.menuTrackerID = setInterval('menuTracker()',10);
}

function menuTracker()
{
    for (var i=0; i < menuTrackers.length; i++) {
        if (!isNaN(menuTrackers[i].xOffset) && document.body) {
            var off = parseInt((menuTrackers[i].xOffset - document.body.scrollLeft) / 10);
            if (isNaN(off)) off = 0;
            if (off < 0) {
                menuTrackers[i].menuDiv.style.pixelLeft += -off;
                menuTrackers[i].xOffset += -off;
            } else if (off > 0) {
                menuTrackers[i].menuDiv.style.pixelLeft += -off;
                menuTrackers[i].xOffset += -off;
            }
        }
        if (!isNaN(menuTrackers[i].yOffset) && document.body) {
            var off = parseInt((menuTrackers[i].yOffset - document.body.scrollTop) / 10);
            if (isNaN(off)) off = 0;
            if (off < 0) {
                menuTrackers[i].menuDiv.style.pixelTop += -off;
                menuTrackers[i].yOffset += -off;
            } else if (off > 0) {
                menuTrackers[i].menuDiv.style.pixelTop += -off;
                menuTrackers[i].yOffset += -off;
            }
        }
    }
}

function GetObject (p_object)
{
	return (m_FFX)? document.getElementById(p_object):
		(m_IE)? document.all[p_object]: false;
}

function M_ParseCoord (p_coord)
{
	if	(p_coord == null)	return p_coord;
	if	(typeof p_coord == "number")	return	p_coord;
	var cond = p_coord.replace(/^([xy][12])\s+\S.*$/, "$1");
	if	(cond == '')	return p_coord;

	var obj_id = p_coord.replace(/^[xy][12]\s+(\S.*)$/, "$1");
	var obj = document.getElementById (obj_id);
	if	(obj == null)	return 0;

	var offx = 0;
	var offy = 0;
	// Add offsets for parent objects
	var obj_parent = obj.offsetParent;
	while (obj_parent != null)
	{
		offx += obj_parent.offsetLeft;
		offy += obj_parent.offsetTop;
		obj_parent = obj_parent.offsetParent;
	}

	if	(cond == 'x1')	return offx + obj.offsetLeft;
	if	(cond == 'x2')	return offx + obj.offsetLeft + obj.offsetWidth;
	if	(cond == 'y1')	return offy + obj.offsetTop;
	if	(cond == 'y2')	return offy + obj.offsetTop + obj.offsetHeight;
	return 0;
}

function M_Onload ()
{
	if	(m_onload)	return;

	m_onload = 1;
	if	(m_write_menus)
	{
		writeMenus();
		m_write_menus = 0;
	}

	if	(window.onload_menu_js)
		window.onload_menu_js();
}

function EQ_DivPosition(p_event, p_div, p_xoff, p_yoff, p_adjust)
{
	var coords = new Object();
	EQ_MouseXY (p_event, coords);
    var posx = coords.x;
    var posy = coords.y;

    if  (p_xoff != null) posx += p_xoff;
    if  (p_yoff != null) posy += p_yoff;

    var obj;
    if (EQ_ns4)
    {
        obj = document.layers[p_div];
        obj.top = posy;
        obj.left = posx;
    }
    else
    {
        // Do something with this information
        if ((EQ_ffx)||(!EQ_ie4))
        {
            obj = document.getElementById(p_div);
        }
        else
        {
            obj = document.all[p_div];
        }
        var obj_parent = obj.offsetParent;
        while (obj_parent != null)
        {
            posx -= obj_parent.offsetLeft;
            posy -= obj_parent.offsetTop;
            obj_parent = obj_parent.offsetParent;
        }

		// Adjust position so it doesn't go outside of the screen
		if	(p_adjust)
		{
			var maxx = (document.body.clientWidth ||
				document.documentElement.clientWidth || window.innerWidth) -
				obj.offsetWidth - 5 + document.body.scrollLeft;
			var maxy = (document.body.clientHeight ||
				document.documentElement.clientHeight || window.innerHeight) -
				obj.offsetHeight - 5 + document.body.scrollTop;
			if	(p_adjust == 1)
			{
				if	(posx > maxx)	posx = maxx;
				if	(posx < 0)	posx = 0;
				if	(posy > maxy)	posy = maxy;
				if	(posy < 0)	posy = 0;
			}
			else
			{
				var i = posy - maxy;
				if	(i > 0)
				{
					i = obj.offsetHeight - i;
					if	(i < 20)	i = 20;
					obj.style.height = i + 'px';
				}
			}
		}

        obj.style.top = posy + 'px';
        obj.style.left = posx + 'px';
    }
}

function EQ_MouseXY (p_event, p_coord)
{
	var x, y;

	if	(!p_event)
	{
		x = 0;
		y = 0;
	}
	else if  ((!EQ_ie4)&&((p_event.pageX)||(p_event.pageY)))
   	{
        x = p_event.pageX;
   	    y = p_event.pageY;
    }
   	else if (EQ_ns4 || EQ_ns6)
	{
		x = p_event.clientX + window.scrollX;
		y = p_event.clientY + window.scrollY;
	}
	else if (EQ_opa)
	{
		x = window.event.clientX + document.documentElement.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop;
	}
	else
	{
		x = window.event.clientX + document.documentElement.scrollLeft +
			document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop +
			document.body.scrollTop;
	}

	p_coord.x = x;
	p_coord.y = y;
}

if	(window.onload)
    window.onload_menu_js = window.onload;
window.onload = M_Onload;
