/******************************************************************* 
* 
* File    : JSMenu.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
***********************************************************************/
if(!window.JSFX)
	window.JSFX=new Object();

JSFX.MenuBars = new Array();

JSFX.greyLAF = function()
{
	this.bgColor	= "#777777";
	this.bgColorOn	= "#BBBBBB";
	this.borderColorH	= "#BBBBBB";
	this.borderColorL	= "#333333";
	this.fontColor	= "#000000";
	this.fontColorOn	= "#CC0000";
	this.fontFace	= "Arial";
	this.fontSize	= "3";

	//Turn off hi color for NS (until I find a better way)
	if(ns4)
		this.fontColorOn	= null;
}

JSFX.greyLAF2 = function()
{
	this.bgColor	= "#999999";
	this.bgColorOn	= "#DDDDDD";
	this.borderColorH	= "#BBBBBB";
	this.borderColorL	= "#333333";
	this.fontColor	= "#000000";
	this.fontColorOn	= "#CC0000";
	this.fontFace	= "Arial";
	this.fontSize	= "3";
	//Turn off hi color for NS (until I find a better way)
	if(ns4)
		this.fontColorOn	= null;
}
JSFX.util = new Object();
JSFX.util.setDefaultLAF = function(theLAF)
{
	JSFX.util.defaultLAF = theLAF;
}
JSFX.util.getDefaultLAF = function()
{
	return JSFX.util.defaultLAF;
}

JSFX.util.setDefaultLAF( new JSFX.greyLAF() );

JSFX.theMenuTimer = null;
JSFX.startMenuCheck = function()
{
	if(JSFX.theMenuTimer == null)
		JSFX.theMenuTimer = setInterval("JSFX.menuCheck()", 500);
}
JSFX.menuCheck = function()
{
	var i;
	for(i=0 ; i<JSFX.MenuBars.length ; i++)
	{
		mb = JSFX.MenuBars[i];
		if(mb.currOn != null && !mb.isActive)
		{
			mb.currOn.close();
			mb.currOn = null;
		}
	}

}

/**********************************************************************/
/*
 * JSMenuItem - extends Object
 */
JSFX.JSMenuItem = function (theText, theLink, theTarget, theLAF)
{
	if(!theText)
		return;

	if(theLAF == null)
		theLAF = JSFX.util.getDefaultLAF()

	this.elemType 	= "JSMenuItem";

	this.theText	= theText;
	this.theLink 	= theLink;
	this.theTarget 	= theTarget;
	this.LAF		= theLAF;
	this.currOn		= null;
	this.parentElem   = null;
	this.isVerticle 	= false;

	this.textPane = new JSFX.BorderedLayer("<FONT FACE='"+this.LAF.fontFace+"' SIZE='"+this.LAF.fontSize+"'>&nbsp;"+theText+"&nbsp;</FONT>", 2);
	this.textPane.setzIndex(4);
	this.textPane.setBgColor(this.LAF.bgColor);
	this.textPane.setColor(this.LAF.fontColor);
	this.textPane.setBorderHighColor(this.LAF.borderColorH);
	this.textPane.setBorderLowColor(this.LAF.borderColorL);

	this.textPane.hide();

	this.glassPane = new JSFX.Layer(" ");
	this.glassPane.setzIndex(100);
	this.glassPane.parentElem = this;
	this.glassPane.addEventHandler("onmouseover", JSFX.JSMenuItemOver);
	this.glassPane.addEventHandler("onmouseout",  JSFX.JSMenuItemOut);
	this.glassPane.addEventHandler("onmouseup",   JSFX.JSMenuItemUp);
	this.glassPane.hide();

	this.w = this.textPane.getWidth();
	this.h = this.textPane.getHeight();
}
JSFX.JSMenuItem.prototype.getRealWidth = function ()
{
	//NS6 bug;
	if(this.w==0)
		this.w=this.textPane.getWidth();

	return this.w;
}
JSFX.JSMenuItem.prototype.getRealHeight = function ()
{
	//NS6 bug;
	if(this.h==0)
		this.h=this.textPane.getHeight();

	return this.h;
}

JSFX.JSMenuItem.prototype.build = function (x,y,w,h)
{
	this.textPane.resizeTo(w,h);
	this.textPane.moveTo(x, y);
	this.glassPane.resizeTo(w,h);
	this.glassPane.moveTo(x, y);

	var glassPaneStr = "<IMG SRC='javascript/jsmenu.gif' WIDTH='"+w+"' HEIGHT='"+h+"' BORDER='0'>"
	var contentStr   = glassPaneStr;
	if(this.theLink != null)
	{
		contentStr = "<A HREF='"+this.theLink+"'";
		if(this.theTarget != null)
			contentStr += " TARGET='"+this.theTarget+"'";
		contentStr += ">"+glassPaneStr+"</A>";
	}
	this.glassPane.setContent(contentStr);
}
JSFX.JSMenuItem.prototype.show = function()
{
	this.textPane.show();
	this.glassPane.show();
}
JSFX.JSMenuItem.prototype.hide = function()
{
	this.textPane.hide();
	this.glassPane.hide();
}
JSFX.JSMenuItem.prototype.open = function() { }
JSFX.JSMenuItem.prototype.close = function() { }

JSFX.JSMenuItem.prototype.setActive = function(elem)
{
	this.parentElem.setActive(this);
	if(this.currOn != elem)
	{
		if(this.currOn != null)
			this.currOn.close();

		this.currOn = elem;
		this.currOn.open();
	}	
	elem.textPane.setBgColor(elem.LAF.bgColorOn);
	if(elem.LAF.fontColorOn)
		elem.textPane.setColor(elem.LAF.fontColorOn);
}
JSFX.JSMenuItem.prototype.setInActive = function(elem)
{
	this.parentElem.setInActive(this);
	elem.textPane.setBgColor(elem.LAF.bgColor);
}
JSFX.JSMenuItemOver = function(pane, ev)
{
	elem = pane.parentElem;
	if(elem.LAF.fontColorOn)
		elem.textPane.setColor(elem.LAF.fontColorOn);
	elem.textPane.setBgColor(elem.LAF.bgColorOn);
	elem.parentElem.setActive(elem);
}
JSFX.JSMenuItemOut = function(pane, ev)
{
	elem = pane.parentElem;
	if(elem.LAF.fontColorOn)
		elem.textPane.setColor(elem.LAF.fontColor);
	elem.textPane.setBgColor(elem.LAF.bgColor);
	elem.parentElem.setInActive(elem);
}
JSFX.JSMenuItemUp = function(pane, ev)
{
	var elem = pane.parentElem;
	var prev = pane.parentElem;

	if(elem.theLink == null)
		return(true);

	while(elem != null)
	{
		prev = elem;
		elem = elem.parentElem;
	}
	if(prev.currOn != null)
		prev.currOn.close();
	prev.currOn=null;

	return(true);
}

/**********************************************************************/
/*
 * JSMenu - extends JSMenuItem
 */
JSFX.JSMenu = function(theText, theLAF)
{

	//Call the super Constructor;
	this.superC = JSFX.JSMenuItem;
	this.superC(theText, null, null, theLAF);

	this.elemType 	= "JSMenu";
	this.elements	= new Array();
}
JSFX.JSMenu.prototype = new JSFX.JSMenuItem;

JSFX.JSMenu.prototype.add = function (elem, link)
{
	var num = this.elements.length;
	this.elements[num] = elem;

	elem.isVerticle 	= true;
	elem.parentElem 	= this;
}
JSFX.JSMenu.prototype.open = function()
{
	var i;
	for(i=0 ; i<this.elements.length ; i++)
		this.elements[i].show();
}
JSFX.JSMenu.prototype.close = function()
{
	var i;
	this.textPane.setBgColor(this.LAF.bgColor);
	if(elem.LAF.fontColorOn)
		this.textPane.setColor(this.LAF.fontColor);
	for(i=0 ; i<this.elements.length ; i++)
	{
		var e = this.elements[i];
		e.close();
		e.hide();
		e.currOn = null;
	}
}
JSFX.JSMenu.prototype.build = function (x,y,w,h)
{
	var i;
	var menuItem;
	var hasMenu = false;

	//Call the superclass function
	this.superF = this.superC.prototype.build;
	this.superF(x,y,w,h);

	if(this.isVerticle)
	{
		x+=w;
		y-=h;
		w=0;
	}

	for(i=0 ; i<this.elements.length ; i++)
	{
		e = this.elements[i];

		if(e.getRealWidth() > w)
			w = e.getRealWidth();
	}	
	for(i=0 ; i<this.elements.length ; i++)
	{
		var e = this.elements[i];
		e.build(x, y + h*(i+1), w, h);
	}
}

/**********************************************************************/
JSFX.JSMenuBar = function(verticle, constWidth, theLAF)
{
	this.elemType 	= "JSMenuBar";

	if(theLAF == null)
		theLAF = JSFX.util.getDefaultLAF();

	this.LAF	= theLAF;
	this.textPane = new JSFX.Layer("<FONT FACE='"+theLAF.fontFace+"' SIZE='"+theLAF.fontSize+"'>MENUBAR</FONT>");
	this.x	= 0;
	this.y	= 0 ;
	this.w	= 800; 
	this.h	= this.textPane.getHeight()+4;
	this.isVerticle	= verticle;
	this.isConstWidth = constWidth;
	this.currOn	= null;
	this.isActive = false;
	this.attached = null;

	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(this.LAF.bgColor);
	this.textPane.hide();
	this.textPane.parentElem = this;
	this.textPane.setzIndex(1);

	this.ns6BuildNo = JSFX.MenuBars.length;
	JSFX.MenuBars[JSFX.MenuBars.length] = this;
}

JSFX.JSMenuBar.prototype.moveTo = function (x,y)
{
	this.x = x;
	this.y = y;
}
JSFX.JSMenuBar.prototype.attachLayer = function (theLayer)
{
	this.attached = new JSFX.Layer(JSFX.findLayer(theLayer));
}
JSFX.JSMenuBarHolder = function(theId, w, h)
{
	var str = "JSMENU";
	var hStr = "";
	if(!w) w = "100%"

	if(document.layers)
	{
		if(h)
			hStr = ' HEIGHT="'+h+'"';
		return('<ILAYER WIDTH="'+w+'"><LAYER name="'+theId+'" WIDTH="'+w+'"'+hStr+'>'+str+'</ILAYER></LAYER>');
	}
	else
	{
		if(h)
			hStr = ' height:'+h;
		return('<div id="'+theId+'" STYLE="width:'+w+hStr+'">'+str+'</div>');
	}
}

JSFX.JSMenuBar.prototype.show = function ()
{
	var i;
	this.textPane.show();
	for(i=0 ; i<this.elements.length ; i++)
		this.elements[i].show();
}
JSFX.JSMenuBar.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)
		{
			this.timeout = setTimeout("JSFX.MenuBars["+this.ns6BuildNo+"].build()",100);
			return;
		}
	}

	//NS6 bug;
	if(this.h==4) this.h=this.textPane.getHeight()+4;

	if(this.attached)
	{
		this.x = this.attached.getPageX();
		this.y = this.attached.getPageY();
		this.w = this.attached.getWidth();
		this.h = this.attached.getHeight();
	}

	var maxW=0;
	var maxH=0;
	var totalH = 0;
	for(i=0 ; i<this.elements.length ; i++)
	{
		var e = this.elements[i];

		if(e.getRealWidth() > maxW)
			maxW = e.getRealWidth();
		if(e.getRealHeight() > maxH)
			maxH = e.getRealHeight();
		totalH += e.getRealHeight();
	}
	if(this.isVerticle)
	{
		this.w = maxW;
		this.h = totalH;
	}
	else
	{
		maxW = ( this.w/(this.elements.length) );
		this.h = maxH;
	}

	this.textPane.setContent("<IMG SRC='javascript/jsmenu.gif' WIDTH='"+this.w+"' HEIGHT='"+this.h+"'>");
	this.textPane.resizeTo(this.w,this.h);
	if(this.attached)
		this.attached.resizeTo(this.w,this.h);
	this.textPane.show();
	this.textPane.moveTo(this.x, this.y);
	var x=this.x;
	var y=this.y;
	
	for(i=0 ; i<this.elements.length ; i++)
	{
		var e = this.elements[i];

		if(this.isVerticle)
		{
			e.build(x,y,maxW, e.h);
			y=y+e.h;
		}
		else if(this.isConstWidth)
		{
			e.build(x,y,maxW, maxH);
			x=x+maxW;
		}
		else
		{
			e.build(x,y,e.w, maxH);
			x=x+e.w;
		}

		e.show();
	}

	JSFX.startMenuCheck();
}
JSFX.JSMenuBar.prototype.add = function (elem)
{
	var num = this.elements.length;
	this.elements[num] = elem;

	elem.isVerticle	= this.isVerticle;
	elem.parentElem 	= this;
}
JSFX.JSMenuBar.prototype.setActive = function (elem)
{
	if(this.currOn != elem)
	{
		if(this.currOn != null)
			this.currOn.close();

		this.currOn = elem;
		this.currOn.open();
	}
	elem.textPane.setBgColor(elem.LAF.bgColorOn);
	if(elem.LAF.fontColorOn)
		elem.textPane.setColor(elem.LAF.fontColorOn);
	this.isActive = true;
}
JSFX.JSMenuBar.prototype.setInActive = function(elem)
{
	this.isActive = false;
	elem.textPane.setBgColor(elem.LAF.bgColor);
}

if(!JSFX.jsmenu_resize)
{
	if(document.layers)
	{
		JSFX.ow = outerWidth;
		JSFX.oh = outerHeight;
		JSFX.jsmenu_resize = function()
		{
			if(outerWidth != JSFX.ow || outerHeight != JSFX.oh )
				location.reload();
		}
	}
	else
		JSFX.jsmenu_resize = function()
		{
			location.reload();
		}
}
window.onresize=JSFX.jsmenu_resize;
