/******************************************************************* 
* 
* File    : xMenu.js 
* 
* Created : 2001/01/20 
* 
* Author  : Roy Whittle  (Roy@Whittle.com) www.Roy.Whittle.com 
* 
* Purpose : To create a cross browser menu (just like all the others)
* 
* History 
* Date         Version        Description 
* 2001-01-20	1.0		Initial version
***********************************************************************/
var MenuBars = new Array();

var menuBarColor    = "#777777";

var menuBgColor     = "#777777";
var menuOnColor     = "#BBBBBB";
var menuBorderColorH = "#BBBBBB";
var menuBorderColorL = "#333333";
var menuFontFace	  = "Arial";
var menuFontSize	  = "3";
var menuItemBgColor = "#999999";
var menuItemOnColor = "#DDDDDD";

var theMenuTimer = null;
function startMenuCheck()
{
	if(theMenuTimer == null)
		theMenuTimer = setInterval("menuCheck()", 500);
}
function menuCheck()
{
	var i;
	for(i=0 ; i<MenuBars.length ; i++)
		if(MenuBars[i].currOn != null)
			if(!MenuBars[i].mouseIsOver)
				MenuBars[i].currOn.close();

}
/**********************************************************************/
function xMenuBar(verticle)
{
	this.elemType 	= "xMenuBar";

	this.textPane = new xLayer("<FONT FACE='"+menuFontFace+"' SIZE='"+menuFontSize+"'>MENUBAR</FONT>");
	this.x	= 0;
	this.y	= 0 ;
	this.w	= 800; 
	this.h	= this.textPane.getHeight()+2;
	this.verticle = verticle;
	this.currOn	= null;
	this.mouseIsOver = false;

	this.elements = new Array();

	if(document.body)
	{
		if(document.body.clientWidth)
			this.w = document.body.clientWidth;
		else if(innerWidth != null)
			this.w = innerWidth;
	}
	else if(innerWidth != null)
		this.w = innerWidth;

	this.textPane.setBgColor(menuBarColor);
	this.textPane.hide();
	this.textPane.parentElem = this;
	this.textPane.addEventHandler("onmouseover", xMenuBarOver);

}


xMenuBar.prototype.show = function ()
{
	var i;
	this.textPane.show();
	for(i=0 ; i<this.elements.length ; i++)
		this.elements[i].show();
}
xMenuBar.prototype.build = function ()
{
	var i;

	//In netscape 6 we have to delay the building of the menu so the DIV's can get a width & height
	if(navigator.appName.indexOf("Netscape") != -1 && document.getElementById)
	{
		if(!this.timeout)
		{
			window.tmp = this;
			this.timeout = setTimeout("window.tmp.build()",100);
			return;
		}
	}

	//NS6 bug;
	if(this.h==2) this.h=this.textPane.getHeight()+2;

	this.textPane.setContent("<IMG SRC='xmenu.gif' WIDTH='"+this.w+"' HEIGHT='"+this.h+"'>");
	this.textPane.clip(0,0,this.w,this.h);
	this.textPane.show();
	var x=this.x;
	var y=this.y;

	for(i=0 ; i<this.elements.length ; i++)
	{
		var e = this.elements[i];

		//NS6 bug;
		if(e.w==2)
		{
			e.w=e.textPane.getWidth()+2;
			e.h=e.textPane.getHeight()+2;
		}
		e.build(this, x,y,e.w, e.h);
		e.show();		
		x=x+e.w;
	}

	MenuBars[MenuBars.length] = this;
	startMenuCheck();
}
xMenuBar.prototype.setBounds = function (x,y, w, h)
{
	var i;
	for(i=0 ; i<this.elements.length ; i++)
	{
		var menu = this.elements[i];
		var w2 = menu.textPane.getWidth() + 20;

		menu.setBounds(x,y, w2, h);
		x += w2;
	}
}
xMenuBar.prototype.add = function (elem)
{
	var num = this.elements.length;
	this.elements[num] = elem;

	elem.isTopLevel	= true;
	elem.parentElem 	= this;
}
xMenuBar.prototype.setOn = function (elem)
{
	if(this.currOn != null)
		this.currOn.close();

	this.currOn = elem;
}
function xMenuBarOver(pane, ev)
{
	elem = pane.parentElem;
	if(elem.currOn != null)
		elem.currOn.close();
}
/**********************************************************************/
function xMenu(theText)
{
	this.elemType 	= "xMenu";

	this.elements	= new Array();
	this.theText	= theText;
	this.currOn		= null;
	this.bgColor	= menuBgColor;
	this.onColor	= menuOnColor;

	this.borderPaneH = new xLayer(" ");
	this.borderPaneH.setzIndex(1);
	this.borderPaneH.setBgColor(menuBorderColorH);
	this.borderPaneH.hide();

	this.borderPaneL = new xLayer(" ");
	this.borderPaneL.setzIndex(2);
	this.borderPaneL.setBgColor(menuBorderColorL);
	this.borderPaneL.hide();

	this.textPane = new xLayer("<FONT FACE='"+menuFontFace+"' SIZE='"+menuFontSize+"'>&nbsp;"+theText+"&nbsp;</FONT>", 100, 100, 20);
	this.textPane.setzIndex(3);
	this.textPane.setBgColor(this.bgColor);
	this.textPane.hide();

	this.glassPane = new xLayer(" ");
	this.glassPane.setzIndex(4);
	this.glassPane.parentElem = this;
	this.glassPane.addEventHandler("onmouseover", xMenuOver);
	this.glassPane.addEventHandler("onmouseout",  xMenuOut);
	this.glassPane.hide();

	this.w = this.textPane.getWidth()+2;
	this.h = this.textPane.getHeight()+2;
}
xMenu.prototype.add = function (elem, link)
{
	var num = this.elements.length;
	this.elements[num] = elem;

	elem.isTopLevel 	= false;
	elem.parentElem 	= this;
}
xMenu.prototype.show = function()
{
	this.borderPaneL.show();
	this.borderPaneH.show();
	this.textPane.show();
	this.glassPane.show();
}
xMenu.prototype.hide = function()
{
	this.borderPaneL.hide();
	this.borderPaneH.hide();
	this.textPane.hide();
	this.glassPane.hide();
}
xMenu.prototype.open = function()
{
	var i;
	for(i=0 ; i<this.elements.length ; i++)
		this.elements[i].show();
}
xMenu.prototype.close = function()
{
	var i;
	for(i=0 ; i<this.elements.length ; i++)
	{
		var e = this.elements[i];
		if(e.close)
			e.close();
		e.hide();
	}
}
xMenu.prototype.build = function (menuBar, x,y,w,h)
{
	var i;
	var menuItem;
	var hasMenu = false;

	this.x 	= x;
	this.y 	= y;
	this.w 	= w;
	this.h	= h;
	this.menuBar = menuBar;

	this.borderPaneH.clip(0,0,w-1,h-1);
	this.borderPaneH.moveTo(x, y);
	this.borderPaneL.clip(1,1,w,h);
	this.borderPaneL.moveTo(x, y);

	this.textPane.clip(1,1,w-1,h-1);
	this.textPane.moveTo(x, y);
	this.glassPane.clip(0,0,w,h);
	this.glassPane.moveTo(x, y);
	this.glassPane.setContent("<IMG SRC='xmenu.gif' WIDTH='"+w+"' HEIGHT='"+h+"'>");

	if(!this.isTopLevel)
	{
		x+=w;
		y-=h;
		w=0;
	}
	for(i=0 ; i<this.elements.length ; i++)
	{
		e = this.elements[i];

		//NS6 bug;
		if(e.w==2)
		{
			e.w=e.textPane.getWidth()+2;
			e.h=e.textPane.getHeight()+2;
		}
		if(e.w > w)
			w = e.w;
	}	
	for(i=0 ; i<this.elements.length ; i++)
	{
		var e = this.elements[i];
		e.build(menuBar, x, y + h*(i+1), w, h);
	}
}
xMenu.prototype.setOn = function (elem)
{
	if(this.currOn != null)
		this.currOn.close();

	this.currOn = elem;
}
xMenu.prototype.setBgColor = function (c)
{
	this.bgColor = c;
	this.textPane.setBgColor(c);
}
xMenu.prototype.setOnColor = function (c)
{
	this.onColor = c;
}
function xMenuOver(pane, ev)
{
	elem = pane.parentElem;
	elem.textPane.setBgColor(elem.onColor);
	elem.parentElem.setOn(elem);
	elem.menuBar.mouseIsOver=true;
	elem.open();
}
function xMenuOut(pane, ev)
{
	elem = pane.parentElem;
	elem.textPane.setBgColor(elem.bgColor);
	elem.menuBar.mouseIsOver=false;
}
/**********************************************************************/
function xMenuItem(theText, theLink, theTarget)
{
	this.elemType 	= "xMenuItem";

	this.theText	= theText;
	this.theLink 	= theLink;
	this.theTarget 	= theTarget;
	this.bgColor 	= menuItemBgColor;
	this.onColor 	= menuItemOnColor;

	this.borderPaneH = new xLayer(" ");
	this.borderPaneH.setzIndex(1);
	this.borderPaneH.setBgColor(menuBorderColorH);
	this.borderPaneH.hide();

	this.borderPaneL = new xLayer(" ");
	this.borderPaneL.setzIndex(2);
	this.borderPaneL.setBgColor(menuBorderColorL);
	this.borderPaneL.hide();

	this.textPane = new xLayer("<FONT FACE='"+menuFontFace+"' SIZE='"+menuFontSize+"'>&nbsp;"+theText+"&nbsp;</FONT>");
	this.textPane.setzIndex(3);
	this.textPane.setBgColor(this.bgColor);
	this.textPane.hide();

	this.glassPane = new xLayer(" ");
	this.glassPane.setzIndex(4);
	this.glassPane.parentElem = this;
	this.glassPane.addEventHandler("onmouseover", xMenuItemOver);
	this.glassPane.addEventHandler("onmouseout",  xMenuItemOut);
	this.glassPane.addEventHandler("onmouseup",   xMenuItemUp);
	this.glassPane.hide();

	this.w = this.textPane.getWidth()+2;
	this.h = this.textPane.getHeight()+2;
}
xMenuItem.prototype.build = function (menuBar, x,y,w,h)
{

	this.x 	= x;
	this.y 	= y;
	this.width 	= w;
	this.height = h;
	this.menuBar = menuBar;

	this.borderPaneH.clip(0,0,w-1,h-1);
	this.borderPaneH.moveTo(x, y);
	this.borderPaneL.clip(1,1,w,h);
	this.borderPaneL.moveTo(x, y);

	this.textPane.clip(1,1,w-1,h-1);
	this.textPane.moveTo(x, y);
	this.glassPane.clip(0,0,w,h);
	this.glassPane.moveTo(x, y);
	var contentStr = "<A HREF='"+this.theLink+"'";
	if(this.theTarget != null)
		contentStr += " TARGET='"+this.theTarget+"'";
	contentStr += "><IMG SRC='xmenu.gif' WIDTH='"+w+"' HEIGHT='"+h+"' BORDER='0'></A>";

	this.glassPane.setContent(contentStr);
}
xMenuItem.prototype.setBgColor = function (c)
{
	this.bgColor = c;
	this.textPane.setBgColor(c);
}
xMenuItem.prototype.setOnColor = function (c)
{
	this.onColor = c;
}
xMenuItem.prototype.show = function()
{
	this.borderPaneH.show();
	this.borderPaneL.show();
	this.textPane.show();
	this.glassPane.show();
}
xMenuItem.prototype.hide = function()
{
	this.borderPaneH.hide();
	this.borderPaneL.hide();
	this.textPane.hide();
	this.glassPane.hide();
}
function xMenuItemOver(pane, ev)
{
	elem = pane.parentElem;
	elem.textPane.setBgColor(elem.onColor);
	elem.parentElem.setOn(null);
	elem.menuBar.mouseIsOver=true;
}
function xMenuItemOut(pane, ev)
{
	elem = pane.parentElem;
	elem.textPane.setBgColor(elem.bgColor);
	elem.menuBar.mouseIsOver=false;
}
function xMenuItemUp(pane, ev)
{
	elem = pane.parentElem;
	while(elem != null)
	{
		prev = elem;
		elem = elem.parentElem;
	}
	prev.currOn.close();

	return(true);
}

