/////////////////////////////////////////////////////////////////////////////
//	menuBar
//
//	menubar_dom.js
//
//	Copyright 2000-2002 Isotools, all rights reserved
//	This document is company confidential and the whole property
//	of Isotools.
//
/////////////////////////////////////////////////////////////////////////////
//
//	NOTES :
//	-----
//
//	This file defines objects needed to construct popup menuBar for 
//	NS 6.2 and higher browsers.
//	It's included by menubar.js
//
///////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------
//	variable used for NS6, to create layers inside the menuBar
//	main layer
//
var mainLayerElement = null;
//-----------------------------------------------------------------

///////////////////////////////////////////////////////////////////////////
//	
//	MENUBAR METHODS IMPLEMENTATION 
//
///////////////////////////////////////////////////////////////////////////
MenuBar.prototype.create = function ()
{
	if (this.created == true)
		return;

	// reinit index global variables
	menuBarIndex = this.index;
	itemGroupIndex = 0;
	itemIndex = 0;

	// create firstLevel
	if (this.childGroup != null)
		this.childGroup.create(this.orientation);

	// init first Level
	this.childGroup.init();

	this.created = true;

	// Position and initialize menuBar
	this.moveTo(this.x, this.y);
}

MenuBar.prototype.moveTo = function (x, y)
{
	this.x = x;
	this.y = y;
	
	if (this.created == true)
	{
		var dx = x - this.childGroup.baseLayer.getLeft();
		var dy = y - this.childGroup.baseLayer.getTop();

		this.childGroup.moveBy(dx, dy);
	}
}

MenuBar.prototype.setzIndex = function (z)
{
	this.z = z;
	
	if (this.created == true)
		this.childGroup.setzIndex(z);
}
///////////////////////////////////////////////////////////////////////////
// END MENUBAR OBJECT
///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
//	
//	ITEMGROUP METHODS IMPLEMENTATION
//
///////////////////////////////////////////////////////////////////////////
ItemGroup.prototype.create = function(orientation)
{
	if (this.baseLayer != null)
		return "";

	var baseLayer = null;

	this.orientation = orientation;

	itemGroupIndex++;
	this.index = itemGroupIndex;
	var itemGroupID = "menuBar" + menuBarIndex + "_" + this.index + "_itemGroupBase";

	if (this.parentItem != null)
		this.menuBar = this.parentItem.parentGroup.menuBar;

	// Create group base layer
	baseLayer = document.createElement('div');
	baseLayer.id = itemGroupID;
	baseLayer.style.position = 'absolute';
	
	// append layer
	document.body.appendChild(baseLayer);

	if (baseLayer != null)
		this.baseLayer = new xbStyle(baseLayer);

	// Build HTML for the items.
	var i;
	for (i=0 ; i<this.items.length ; i++)
		this.items[i].create(baseLayer);
}

ItemGroup.prototype.init = function()
{
	var itemGroupID = "menuBar" + menuBarIndex + "_" + this.index + "_itemGroupBase";

	// Position and initialize each item.
	var groupWidth	= (this.width>0) ? this.width : 0;
	var itemWidth	= (this.orientation=="vertical") ? this.width : 0;

	var groupHeight	= (this.height>0) ? this.height : 0;
	var itemHeight	= (this.orientation=="horizontal") ? this.height : 0;

	var maxWidth	= (this.orientation=="horizontal") ? this.border : 0;
	var maxHeight	= (this.orientation=="vertical") ? this.border : 0;

	var maxWidth = this.width;

	var i;
	for (i = 0; i < this.items.length; i++)
	{
		var tableWidth = this.items[i].init(itemWidth);

		if (this.orientation == "vertical")
		{
			maxWidth  = Math.max(maxWidth, tableWidth);
			maxWidth  = Math.max(maxWidth, parseInt(this.items[i].normalLayer.getWidth()+ 2*this.border) );
			if (this.parentItem != null && this.parentItem.parentGroup.orientation == "horizontal")
				maxWidth  = Math.max(this.parentItem.normalLayer.getWidth(), maxWidth);
			maxHeight+= this.items[i].normalLayer.getHeight() + this.separator;
		}
		else 
		{
			maxWidth += Math.max(tableWidth, this.items[i].normalLayer.getWidth()) + this.border;
			maxHeight = Math.max(this.height, parseInt(this.items[i].normalLayer.getHeight()+ 2*this.border) );
		}
	}

	if (this.orientation == "vertical")
	{
		maxHeight -= this.separator;
		maxHeight += this.border;
	}
	else
		maxWidth += this.border;

	// Save resulting width and height
	this.width = maxWidth;
	this.height= maxHeight;

	var x = this.border;
	var y = this.border;
	
	for (i = 0; i < this.items.length; i++)
	{
		var itemTableID	= "menuBar" + menuBarIndex + "_" + this.index + "_" + this.items[i].index + "_itemTable";
		itemWidth = (this.orientation == "vertical") ? (this.width - 2*this.border) : Math.max(new xbStyle(document.getElementById(itemTableID)).getWidth() + 15, this.items[i].normalLayer.getWidth());
		itemHeight= (this.orientation == "horizontal") ? (this.height - 2*this.border) : this.items[i].normalLayer.getHeight();

		this.items[i].initPosition(x, y, itemWidth, itemHeight);

		if (this.orientation == "horizontal")
			x = this.items[i].right;
		else
			y = this.items[i].bottom;
	}

	// reinit layer size to fit items
	var clipStr = "rect(0 " + this.width + "px " + this.height + "px 0 )";
	this.baseLayer.setWidth(this.width);
	this.baseLayer.setHeight(this.height);
	this.baseLayer.setBackgroundColor(this.borderColor);
	this.baseLayer.setClip(clipStr);
	
	// set visibility (visible for the first, hidden for the others)
	if (this.parentItem == null)
		this.baseLayer.setVisibility("visible");
	else
		this.baseLayer.setVisibility("hidden");

	// position group if not the first
	if (this.parentItem != null)
	{
		// look if we fit into the window
		var winWidth  = getWindowWidth();
		var winHeight = getWindowHeight();

		var newX, newY;
		if (this.parentItem.parentGroup.orientation == "horizontal")
		{
			newX = this.parentItem.normalLayer.getLeft() + this.parentItem.parentGroup.baseLayer.getLeft();
			newY = this.parentItem.normalLayer.getTop() + this.parentItem.normalLayer.getHeight() - 2	 + this.parentItem.parentGroup.baseLayer.getTop();
		}
		else
		{
			newX = this.parentItem.normalLayer.getLeft() + this.parentItem.transparentLayer.getClipWidth() - 5 + this.parentItem.parentGroup.baseLayer.getLeft();
			newY = this.parentItem.normalLayer.getTop() + this.parentItem.parentGroup.baseLayer.getTop() + 1;
		}

		// look to the left of the window
		if ( ((newX + this.width) > winWidth) && (this.parentItem.parentGroup.orientation == "vertical") )
			newX = this.parentItem.normalLayer.getLeft() + this.parentItem.parentGroup.baseLayer.getLeft() + 5 - this.width;

		// look to the bottom of the window
		if ( ((newY + this.height) > winHeight) && (this.parentItem.parentGroup.orientation == "vertical") )
			newY = winHeight - this.height;
		
		this.baseLayer.moveTo(newX, newY);
	}
	else
		this.baseLayer.moveTo(this.menuBar.x, this.menuBar.y);
}

ItemGroup.prototype.moveBy = function (dx, dy)
{
	this.baseLayer.moveBy(dx, dy);

	var i;
	for(i=0 ; i<this.items.length ; i++)
	{
		if (this.items[i].childGroup != null)
			if (this.items[i].childGroup.baseLayer != null)
				this.items[i].childGroup.moveBy(dx, dy);
	}
}

ItemGroup.prototype.setzIndex = function (z)
{
	this.baseLayer.setzIndex(z);

	var i;
	for(i=0 ; i<this.items.length ; i++)
	{
		if (this.items[i].childGroup != null)
			if (this.items[i].childGroup.baseLayer != null)
				this.items[i].childGroup.setzIndex(z);
	}
}

ItemGroup.prototype.isMouseOver = function (mouseX, mouseY)
{
	// correction for mouse coord returned by event
	var correction = 3;

	var left, top, right, bottom;
	// we have to substract border from the group range
	left = this.baseLayer.getLeft() + this.border + correction;
	top = this.baseLayer.getTop() + this.border + correction;
	right = left + this.baseLayer.getWidth() - this.border - correction;
	bottom = top + this.baseLayer.getHeight() - this.border - correction;	

	if ( (mouseX<=left || mouseX>right) || (mouseY<=top || mouseY>bottom) )
		return false;
	else
		return true;
}
///////////////////////////////////////////////////////////////////////////
// END ITEMGROUP OBJECT
///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
//	
//	ITEM METHODS IMPLEMENTATION
//
///////////////////////////////////////////////////////////////////////////
Item.prototype.create = function(parentElement)
{
	if ( (this.normalLayer != null) || (this.parentGroup == null) || (!parentElement) )
		return "";

	// increment index global variable
	itemIndex ++;
	this.index = itemIndex;
	var itemNormalID		= "menuBar" + menuBarIndex + "_" + this.parentGroup.index + "_" + this.index + "_itemNormal";
	var itemHighlightedID	= "menuBar" + menuBarIndex + "_" + this.parentGroup.index + "_" + this.index + "_itemHighlighted";
	var itemTransparentID	= "menuBar" + menuBarIndex + "_" + this.parentGroup.index + "_" + this.index + "_itemTransparent";
	var itemTableID			= "menuBar" + menuBarIndex + "_" + this.parentGroup.index + "_" + this.index + "_itemTable";

	// layer content
	var content		= "";
	var hiContent	= "";

	if (this.parentGroup.decoration != "")
	{
		content	  += '<td width="1" align="left" valign="top">' + this.parentGroup.decoration + '</td>';
		hiContent += '<td width="1" align="left" valign="top">' + this.parentGroup.hiDecoration + '</td>';
	}

	content +='<td align="left"' + ( (this.parentGroup.orientation!="vertical" || this.parentGroup.width==0) ? ' nowrap>' : '>')
			+ '<span style="color:' + this.parentGroup.fgColor + ';'
			+ 'font-family:' + this.parentGroup.fontFamily + ';'
			+ 'font-size:' + this.parentGroup.fontSize + ';' 
			+ 'font-weight:' + this.parentGroup.fontWeight + ';' 
			+ 'text-decoration:' + this.parentGroup.textDecoration + ';">'
			+ unescape(this.text) + '</span>' + '</td>';
	hiContent += '<td align="left"' + ( (this.parentGroup.orientation!="vertical" || this.parentGroup.width==0) ? ' nowrap>' : '>')
			+ '<span style="color:' + this.parentGroup.hiFgColor + ';'
			+ 'font-family:' + this.parentGroup.fontFamily + ';'
			+ 'font-size:' + this.parentGroup.fontSize + ';' 
			+ 'font-weight:' + this.parentGroup.fontWeight + ';' 
			+ 'text-decoration:' + this.parentGroup.hiTextDecoration + ';">'
			+ unescape(this.text) + '</span>' + '</td>';
	
	// common innerHTML for the layers
	var norm = "";
	var high = "";
	var endNorm = "";
	var endHigh = "";

	var hasChildren = false;
	var i;
	for (i=0 ; i<this.parentGroup.items.length && hasChildren == false ; i++)
	{
		if (this.parentGroup.items[i].childGroup != null)
			hasChildren = true;
	}

	norm = '<table border=0 cellpadding=' + this.parentGroup.padding + ' cellspacing=0'
		+ ( (this.parentGroup.orientation=="vertical" && this.parentGroup.width>0) ? ' width=' + this.parentGroup.width : ((this.parentGroup.orientation=="vertical" && this.parentGroup.nextImg != "" && hasChildren == true) ? ' width="200"' : ''))
		+ ' id="' + itemTableID + '"><tr>';
	high = '<table border=0 cellpadding=' + this.parentGroup.padding + ' cellspacing=0'
		+ ( (this.parentGroup.orientation=="vertical" && this.parentGroup.width>0) ? ' width=' + this.parentGroup.width : ((this.parentGroup.orientation=="vertical" && this.parentGroup.nextImg != "" && hasChildren == true) ? ' width="200"' : ''))
		+ '><tr>';
	
	if (this.parentGroup.nextImg != "")
	{
		if (this.childGroup != null)
		{
			endNorm += '<td width="1" align="right" valign="top">' + this.parentGroup.nextImg + '</td>';
			endHigh += '<td width="1" align="right" valign="top">' + this.parentGroup.hiNextImg + '</td>';
		}
		else
		{
			endNorm += '<td width="1" align="right" valign="top">&nbsp;</td>';
			endHigh += '<td width="1" align="right" valign="top">&nbsp;</td>';
		}
	}

	endNorm += '</tr></table>';
	endHigh += '</tr></table>';

	var normalLayer = null;
	var highlightedLayer = null;
	var transparentLayer = null;

	normalLayer = document.createElement('div');
	normalLayer.id = itemNormalID;
	normalLayer.style.position = 'absolute';
	normalLayer.innerHTML = norm + content + endNorm;

	highlightedLayer = document.createElement('div');
	highlightedLayer.id = itemHighlightedID;
	highlightedLayer.style.position = 'absolute';
	highlightedLayer.innerHTML = high + hiContent + endHigh;

	transparentLayer = document.createElement('div');
	transparentLayer.id = itemTransparentID;
	transparentLayer.style.position = 'absolute';
	transparentLayer.innerHTML = "&nbsp;";

	parentElement.appendChild(normalLayer);
	parentElement.appendChild(highlightedLayer);
	parentElement.appendChild(transparentLayer);

	this.normalLayer = new xbStyle(normalLayer);
	this.highlightedLayer = new xbStyle(highlightedLayer);
	this.transparentLayer = new xbStyle(transparentLayer);
}

Item.prototype.init = function (width)
{
	var tableWidth = 0;

	var itemNormalID		= "menuBar" + menuBarIndex + "_" + this.parentGroup.index + "_" + this.index + "_itemNormal";
	var itemHighlightedID	= "menuBar" + menuBarIndex + "_" + this.parentGroup.index + "_" + this.index + "_itemHighlighted";
	var itemTransparentID	= "menuBar" + menuBarIndex + "_" + this.parentGroup.index + "_" + this.index + "_itemTransparent";
	var itemTableID			= "menuBar" + menuBarIndex + "_" + this.parentGroup.index + "_" + this.index + "_itemTable";

	// Fix for NS6 to resize items to fit text width.
	if (width == 0)
	{
		var table = new xbStyle(document.getElementById(itemTableID));
		tableWidth = table.getWidth() + 10;
	}
	// to correct fit to text
	this.normalLayer.setWidth(width);
	this.highlightedLayer.setWidth(width);
	this.transparentLayer.setWidth(width);

	// set events handlers
	var layerObject = this.transparentLayer.object;
	layerObject.item= this;

	layerObject.addEventListener("mouseover", ItemOnMouseOver,true);
	layerObject.addEventListener("mouseout", ItemOnMouseOut,true);
	layerObject.addEventListener("click", ItemClick,true);

	return tableWidth;
}

Item.prototype.initPosition = function (x, y, width, height)
{
	var clipStr = "rect(0 " + width + "px " + height + "px 0  )";

	this.normalLayer.moveTo(x, y);
	this.normalLayer.setWidth(width);
	this.normalLayer.setBackgroundColor(this.parentGroup.bgColor);
	this.normalLayer.setClip(clipStr);
	this.normalLayer.setVisibility("inherit");	

	this.highlightedLayer.moveTo(x, y);
	this.highlightedLayer.setBackgroundColor(this.parentGroup.hiBgColor);
	this.highlightedLayer.setWidth(width);
	this.highlightedLayer.setClip(clipStr);
	this.highlightedLayer.setVisibility("hidden");

	this.transparentLayer.moveTo(x, y);
	this.transparentLayer.setWidth(width);
	this.transparentLayer.setHeight(this.normalLayer.getHeight());
	this.transparentLayer.setClip(clipStr);
	this.transparentLayer.setVisibility("inherit");

	this.left	= x;
	this.right	= x + width + this.parentGroup.border;
	this.top	= y;
	this.bottom	= y + height + this.parentGroup.separator;
}
///////////////////////////////////////////////////////////////////////////
// END ITEM OBJECT
///////////////////////////////////////////////////////////////////////////

//*************************************************************************
// EVENT RESPONSE
//*************************************************************************
function ItemOnMouseOver(e)
{
        if (this.item.click == "" || this.item.click == "") {
	    return;
        }

	// highlight menu item
	this.item.highlightedLayer.setVisibility("visible");

	// if there's a child show it
	if (this.item.childGroup != null)
	{
		// first look if it is already created
		if (this.item.childGroup.baseLayer == null)
		{
			// create it
			var str = this.item.childGroup.create("vertical");

			// initialized it
			this.item.childGroup.init();

			// setzIndex
			this.item.childGroup.setzIndex(this.item.parentGroup.baseLayer.getzIndex());
		}

		// then show it
		this.item.childGroup.baseLayer.setVisibility("visible");
	}

	// if there's an action do it
	if (this.item.mouseOver != null && this.item.mouseOver != "")
		eval(unescape(this.item.mouseOver));
}

function ItemOnMouseOut(e)
{
	// get mouse coord
	var mouseX = e.clientX;
	var mouseY = e.clientY;
	
	// if there's an action do it
	if (this.item.mouseOut != null && this.item.mouseOut != "")
		eval(unescape(this.item.mouseOut));
	
	// un-highlight item
	this.item.highlightedLayer.setVisibility("hidden");

	// if there's a child group
	if (this.item.childGroup != null)
	{
		// if we're not on it hide it
		if ( !this.item.childGroup.isMouseOver(mouseX, mouseY) )
			this.item.childGroup.baseLayer.setVisibility("hidden");
		// else show it and return
		else
		{
			this.item.childGroup.baseLayer.setVisibility("visible");
			return;
		}
	}

	// if we have a parent group
	var item = this.item;

	while (item.parentGroup.parentItem != null)
	{
		if ( !item.parentGroup.isMouseOver(mouseX, mouseY) )
		{
			// mouse leave my group hide it
			item.parentGroup.baseLayer.setVisibility("hidden");

			// if mouse is on my parent return else continue
			// my parent group range
			if ( item.parentGroup.parentItem.parentGroup.isMouseOver(mouseX, mouseY) )
				return;
		}
		else 
		{
			item.parentGroup.baseLayer.setVisibility("visible");
			return;
		}

		item = item.parentGroup.parentItem;
	}
}

//*************************************************************************
// Code to handle a window resize.
//*************************************************************************
function menuBarReload() 
{
	if (alreadyReloaded == false)
	{
		window.location.href = window.location.href;
		alreadyReloaded = true;
	}
}
///////////////////////////////////////////////////////////////////////////
// ------------------------ End of File -----------------------------------
///////////////////////////////////////////////////////////////////////////

