// ------------------------------------------------------------------------
//
// All code is copyright © Pixeltech Design Pty Ltd
//
//    URL: www.pixeltech.com
//
// The use of any part of this code without express written permission
// contravenes international copyright law.
//
// Version: 1.1 - Netscape 6 compliant
//
// ------------------------------------------------------------------------

var defaultParams = 
    'font=Helvetica,fontSize=9,backgroundColor=#C0C0C0,textColor=black,' +
    'borderStyle=outset,borderColor=#FDFDFD,borderWidth=1,fixed=false,' +
    'highlightTextColor=white,highlightColor=#000099,showHeading=false,' +
    'headingColor=#000099,headingFont=Helvetica,headingFontSize=10,' +
    'headingAlign=center,leftMargin=3,rightMargin=3,xPos=10,yPos=10,' +
    'showHighlight=true,xspacerColor=gray';
var menuID = 0;
var browser = navigator.appName == "Netscape" ? "NS" : "IE";
var menuItems = new Array();
var menus = new menuItem("Menus", null, defaultParams);
var menuOver = null;
var menuItemOver = null;
var arrowImage = "icons/arrow.gif";
var arrowOverImage = "icons/arrowOver.gif";
var arrowWidth = 4, arrowHeight = 7;
var netscapeYOffset = 17;	// Mike: netscape renders menus a bit too low.

function createMenu(label, action, params)
{
    var m = this.id == null ? menus : this;

    if (m.children == null)
	m.children = new Array();

    return m.children[m.children.length] =
	new menuItem(label, action, params, m);
}

function genMenus()
{
    for (var i = 0; i < menuItems.length; i++)
	genMenuItem(menuItems[i]);
}

function genMenuItem(menuItem)
{
    var i, h, l, m, s, y;
    var leftSpacer = createSpacer(menuItem.leftMargin);
    var rightSpacer = createSpacer(menuItem.rightMargin);

    if (menuItem.id == 0 || menuItem.children == null)
	return true;

    h = '<DIV ID="Menu' + menuItem.id + '" STYLE="Position: absolute; ' +
	    'Visibility: hidden; cursor: default" ' +
	    'onMouseMove="menuMouseMove(event)" onMouseOut="menuMouseOut();" ' +
	    'onSelectStart="return false">' +
	'<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0><TR><TD BGCOLOR="' +
	    menuItem.backgroundColor + '"' + (menuItem.backgroundImage == null
	    || menuItem.backgroundImage == '' ? '' : 'BACKGROUND="' +
	    menuItem.backgroundImage + '"') + '">' +
	    '<DIV STYLE="Border-style: ' + menuItem.borderStyle +
	    '; Border-width: ' + menuItem.borderWidth + '; Border-color: ' +
	    menuItem.borderColor + '; ' + (menuItem.backgroundImage == null ||
	    menuItem.backgroundImage == "" ? 'Background-color: ' +
	    menuItem.backgroundColor : '') + '">';

    if (menuItem.showHeading)
    {
	l = leftSpacer + menuItem.label.replace(/\n/g, rightSpacer + "<BR>" +
            leftSpacer) + rightSpacer;
	s = 'Font-family: ' + menuItem.headingFont.replace(/ /g, ",") +
	    '; Font-size: ' + menuItem.headingFontSize + 'pt; Color: ' +
	    menuItem.headingColor;

	h +=    '<DIV STYLE="' + s + '; Text-align: ' + menuItem.headingAlign +
		    '">' + l + '</DIV>' +
		'<DIV ID="MenuHeading' + menuItem.id +
		    '" STYLE="Top: 0; Left: 0; Position: absolute; ' +
		    'Visibility: hidden; ' + s + '; Text-align: Left">' + l +
		    '</DIV>';
    }

    for (i = 0; i < menuItem.children.length; i++)
    {
	c = menuItem.children[i];
        if (c.label == "-")
            l = '<HR ' + (c.spacerColor == null ? '' : 'COLOR="' +
            c.spacerColor + '"' ) + '>';
        else
            l = '<NOBR>' + leftSpacer + c.label.replace(/\n/g, rightSpacer + "<BR>" +
                leftSpacer) + rightSpacer + '</NOBR>';
	s = 'Font-family: ' + c.font.replace(/ /g, ",") + '; Font-size: ' +
            c.fontSize + 'pt';
	h +=    '<DIV ID="MenuItem' + c.id + '" STYLE="' + s + '; Color: ' +
                    c.textColor + '; Text-align: left">' + l + '</DIV>';
	if (c.children != null)
	    h += '<DIV ID="Arrow' + c.id +
		'" STYLE="Position: absolute; Left: 0; Top: 0">' +
		'<IMG SRC="' + arrowImage + '"></DIV>';
	if (browser == "NS")
	    s += "; Padding-left: 0";
        h +=   '<DIV ID="MenuItemOver' + c.id +
		    '" STYLE="Text-align: left; Color: ' + c.highlightTextColor
                    + '; ' + s +
                    '; Top: 0; Position: absolute; Visibility: hidden">' +
                    (c.label == "-" && browser == "NS" ? '<BR>' : l);
	if (c.children != null)
	    h += '<DIV ID="ArrowOver' + c.id +
		'" STYLE="Position: absolute; Left: 0; Top: 0">' +
		'<IMG SRC="' + arrowOverImage + '"></DIV>';
        h += '</DIV>';
    }

    h +=    '</DIV>' +
	'</TD></TR></TABLE>' +
	'</DIV>';

    document.writeln(h);

    if (browser == "NS")
    {
	m = document.getElementById("Menu" + menuItem.id);
	menuItem.menuObj = m;
	m.addEventListener("mousemove", menuMouseMove, true);
	m.addEventListener("mouseup", menuMouseSelect, true);
	y = menuItem.borderWidth;
	if (menuItem.showHeading)
	    y += m.document.getElementById("MenuHeading" + menuItem.id).style.clip.height;
	for (i = 0; i < menuItem.children.length; i++)
	{
	    c = menuItem.children[i];
	    s = document.getElementById("MenuItemOver" + c.id);
	    c.itemObj = s;
            s.style.clip.width = m.style.clip.width - 2 - 2 * menuItem.borderWidth;
            s.style.backgroundColor = c.highlightColor;
            s.style.left = menuItem.borderWidth;
	    s.style.top = y;
	    if (c.children != null)
	    {
		a = m.document.getElementById("Arrow" + c.id);
		a.style.left = m.style.clip.width - arrowWidth - 3 - menuItem.borderWidth;
		a.style.top = y + (s.style.clip.height - arrowHeight) / 2;
		a = s.document.getElementById("ArrowOver" + c.id);
		a.style.left = m.style.clip.width - arrowWidth - 4 - 2 *
		    menuItem.borderWidth;
		a.style.top = (s.style.clip.height - arrowHeight) / 2;
	    }
            y += s.offsetHeight + (c.label == "-" ? 4 : 0);
	}
    }
    else
    {
	m = document.all["Menu" + menuItem.id];
	menuItem.menuObj = m;
	y = menuItem.borderWidth;
	if (menuItem.showHeading)
	    y += document.all["MenuHeading" + menuItem.id].offsetHeight;

	for (i = 0; i < menuItem.children.length; i++)
	{
	    c = menuItem.children[i];
	    s = document.all["MenuItemOver" + c.id];
	    c.itemObj = s;
	    s.style.width = m.offsetWidth - 2 * menuItem.borderWidth;
	    s.style.background = c.highlightColor;
	    s.style.pixelLeft = menuItem.borderWidth;
	    s.style.pixelTop = y;
	    if (c.children != null)
	    {
		a = m.document.all["Arrow" + c.id];
		a.style.pixelLeft = m.offsetWidth - arrowWidth - 2 -
		    menuItem.borderWidth;
		a.style.pixelTop = y + (s.offsetHeight - arrowHeight) / 2;
		a = m.document.all["ArrowOver" + c.id];
		a.style.pixelLeft = m.offsetWidth - arrowWidth - 2 -
		    menuItem.borderWidth * 2;
		a.style.pixelTop = (s.offsetHeight - arrowHeight) / 2;
	    }
            y += s.offsetHeight;
	}
    }

    menuItem.moveMenuTo(menuItem.xPos, menuItem.yPos);

    if (menuItem.fixed)
	menuItem.menuShow();

    return true;
}

function createSpacer(size)
{
    var s = '';
    var i;

    while (size-- > 0)
	s += '&nbsp;';

    return '<FONT SIZE=1>' + s + '</FONT>';
}

function menuMouseOver(item)
{
    var b, i, p;

    if (menuItemOver != null)
    {
	if ((p = menuItemOver.parent) != null)
	    for (i = 0; i < p.children.length; i++)
                p.children[i].hide();
        if (menuItemOver.showHighlight)
            menuItemOver.highlight();
	if (menuItemOver.children != null)
	{
	    b = menuItemOver.getMenuItemBounds();
	    menuItemOver.moveMenuTo(b[0] + b[2] - 2, b[1] - 1);
	    menuItemOver.menuShow();
	}
    }

    return true;
}

function menuMouseOut()
{
    if (menuItemOver != null)
	menuItemOver.unHighlight();
    menuItemOver  = null;
}

function menuMouseMove(event)
{
    var over;

    if (browser == "NS"){
		//	over = getMenuItem(event);
		over = event.target;
		if (over.id == "")
			over = over.parentNode;
		if (over != null)
		    over = objToMenuItem(over);	
	}
    else
    {
	over = event.srcElement;
	if (over.id == "")
	    over = over.parentElement;
	if (over != null)
	    over = objToMenuItem(over);
    }

    if (menuItemOver != over)
    {
	menuMouseOut();
	menuItemOver = over;
	menuMouseOver();
    }

    return false;
}

function menuMouseEnter(event)
{
    var target = event.target;

    if (target.id != null && target.id.match(/Menu\d+/))
	menuOver = objToMenuItem(target);
    else if (target.onmouseover != null)
	target.onmouseover();
}

function menuMouseLeave(event)
{
    if (event.target != null && event.target.id != null &&
	event.target.id.match(/^Menu\d+/))
    {
	menuMouseOut();
	menuOver = null;
    }
}

function menuMouseSelect()
{
    if (menuItemOver != null && menuItemOver.children != null)
	return true;

    if (menuItemOver != null)
	menuItemOver.unHighlight();

    menuOver = null;

    hideAllMenus();

    if (menuItemOver != null && menuItemOver.action != null)
	if (menuItemOver.action.match(/^[a-z:_\/.#?-]+$/i))
	    location.href = menuItemOver.action;
	else
	    eval(menuItemOver.action.replace(/%m/g, 'menuItem[' +
		menuItemOver.id + ']'));

    return true;
}

function hideAllMenus()
{
    var i;


    for (i = 0; i < menuItems.length; i++)
	if (!menuItems[i].fixed)
	    menuItems[i].hide();
}

function objToMenuItem(obj)
{
    var id = null;

    if (obj != null && obj.id != null)
	if ((obj = obj.id.match(/^(Menu|MenuItem|MenuItemOver|Arrow|ArrowOver)(\d+)$/)) !=
		null)
	    id = obj[2];

    return id == null ? null : menuItems[id];
}

function getMenuItem(event)
{
    var i, m = null;

    if (menuOver == null || menuOver.menuObj == null || menuOver.children ==
	    null)
	return null;

    for (i = 0; i < menuOver.children.length; i++)
	if (event.pageY - menuOver.menuObj.pageY >
		menuOver.children[i].itemObj.top)
	    m = menuOver.children[i];

    return m;
}

function setMenuBackgroundNS(col)
{
    if (this.itemObj.bgColor != col)
	this.itemObj.bgColor = col;
}

function setMenuBackgroundIE(col)
{
    if (this.itemObj.style.backgroundColor != col)
	this.itemObj.style.backgroundColor = col;
}

function menuShowNS()
{
    if (this.menuObj != null && this.menuObj.style.visibility.indexOf("hid") >= 0)
	this.menuObj.style.visibility = "visible";
}

function menuHideNS()
{
    if (this.menuObj != null && this.menuObj.style.visibility.indexOf("hid") < 0)
	this.menuObj.style.visibility = "hidden";
}

function menuShowIE()
{
    this.menuObj.style.visibility = "visible";
}

function menuHideIE()
{
    if (this.menuObj != null)
	this.menuObj.style.visibility = "hidden";
}

function menuMoveToNS(x, y)
{
    if (x != null)
	this.menuObj.style.left = x;

    // BC: Added magic number here to reposition menu's vertically ... they were
    // displaying  a few pixels too high in FireFox. Hopefully it's fine on everyone elses
    // system!!!! PS: this is a kludgy hack, but I'm not reverse engineering JK's code. Life's too short.
    
    if (y != null)
	this.menuObj.style.top = y + 17;
}

function menuMoveToIE(x, y)
{
    if (x != null)
	this.menuObj.style.pixelLeft = x;

    if (y != null)
	this.menuObj.style.pixelTop = y;
}

function moveMenuTo(x, y)
{
    this.x = x;
    this.y = y;

    this.menuMoveTo(x, y);
}

function showMenu(a, b, c)
{
    if (typeof a == "string")
		this.menuMoveTo(objectX(a) + (b == null ? 0 : b), objectY(a) + (c == null ? 0 : c));
    else if (typeof a == "number")
		this.moveMenuTo(a, b);
    else if (typeof a == "object")
		this.moveMenuTo(getPageX(a) + (b == null ? 0 : b), getPageY(a) + (c == null ? 0 : c));
    hideAllMenus();
    this.visible = true;
    this.menuShow();

    return false;
}

function getPageX(obj)
{
    var x = 0;

    for ( ; obj != null; obj = obj.offsetParent)
	x += obj.offsetLeft;

    return x;
}

function getPageY(obj)
{
    var y = 0;

    for ( ; obj != null; obj = obj.offsetParent)
	y += obj.offsetTop;

    return (browser == "NS" ? y-netscapeYOffset : y);
}

function hideMenu()
{
    if (this.children != null)
	for (var i = 0; i < this.children.length; i++)
            if (!this.children[i].fixed)
		this.children[i].hide();
    this.menuHide();
    this.visible = false;
}

function menuHighlightNS()
{
    this.itemObj.style.visibility = "visible";
}

function menuHighlightIE()
{
    this.itemObj.style.visibility = "visible";
}

function menuUnHighlightNS()
{
    this.itemObj.style.visibility = "hidden";
}

function menuUnHighlightIE()
{
    this.itemObj.style.visibility = "hidden";
}

function getBoundsNS(obj)
{
    return new Array(obj.style.left, obj.style.top, obj.style.clip.width, obj.style.clip.height);
}

function getBoundsIE(obj)
{
    var parent = obj.offsetParent;

    return new Array(parent.offsetLeft, parent.offsetTop + obj.offsetTop,
	parent.offsetWidth, parent.offsetHeight);

}

function getMenuBounds()
{
    return this.getBounds(this.menuObj);
}

function getMenuItemBounds()
{
    return this.getBounds(this.itemObj);
}

function menuItem(label, action, params, parent)
{
    var pos;

    for (var i in parent)
	this[i] = parent[i];

    this.createMenu = createMenu;
//    this.setBackground = eval("setMenuBackground" + browser);
    this.highlight = eval("menuHighlight" + browser);
    this.unHighlight = eval("menuUnHighlight" + browser);
    this.show = showMenu;
    this.menuShow = eval("menuShow" + browser);
    this.hide = hideMenu;
    this.menuHide = eval("menuHide" + browser);
    this.parseParams = parseParams;
    this.getBounds = eval("getBounds" + browser);
    this.getMenuBounds = getMenuBounds;
    this.getMenuItemBounds = getMenuItemBounds;
    this.menuMoveTo = eval("menuMoveTo" + browser);
    this.moveMenuTo = moveMenuTo;

    if (label == "-")
        this.showHighlight = false;

    this.parseParams(params);
    this.label = label;
    this.action = action;
    this.children = null;
    this.id = menuID++;
    this.menuObj = null;
    this.itemObj = null;
    this.parent = parent;
    this.x = -1;
    this.y = -1;
    this.visible = false;
    menuItems[this.id] = this;

    if (this.id > 0)
        document.write('<IMG NAME="Menu' + this.id +
            'Pos" ALIGN=TOP WIDTH=0 HEIGHT=0 SRC="javascript:genPos()">');
}

function genPos()
{
    return "#define i_width 1\n#define i_height 1\nstatic char i_bits[] = { 0x00 };";
}

function genRule()
{
    return "#define i_width 1\n#define i_height 1\nstatic char i_bits[] = { 0xFF };";
}

function parseParams(params)
{
    var i, param;

    if (params != null)
    {
	params = params.split(/\s*,\s*/);

	for (var i = 0; i < params.length; i++)
	{
	    param = params[i].split(/\s*=\s*/);
	    if (!param[1].match(/^(\d+|true|false|'.*'|".*")$/))
		param[1] = '"' + param[1] + '"';
	    eval("this." + param[0] + "=" + unescape(param[1]) + ";");
	}
    }
}

if (browser == "NS")
{
	document.addEventListener("mouseover", menuMouseEnter, true);
	document.addEventListener("mouseout", menuMouseLeave, true);
	document.addEventListener("mouseup", menuMouseSelect, true);
}
else
    document.onmouseup = menuMouseSelect;

// Mike: function for debugging
function alertAllAlpha(obj, regEx){
	myArray = new Array();
	if(regEx)
		myRe = new RegExp(regEx, "i");
	for(i in obj){
		if(regEx && !myRe.test(i))
			continue;
		myArray.push(i +"="+ obj[i]);
	}
	myArray.sort();
	for(i in myArray)
		alert(myArray[i]);
}
