/**
 *	Class to encapsulate a kwMenu bar.
 *
 *	@author			Adam Callen
 *	@version		1.0
 *	@copyright	Katana Productions Pty Ltd 2004
 *
 *	@param	top	The top position of the kwMenubar on the page.
*/
function kwMenubar(top,bgcolor)
{
	this.top = top;
	this.style = 'menu';
	this.innerHTML = '';
	this.parentelm = null;
	this.isFirstMenu = true;
	this.menuList = new Array();
	this.bgcolor = bgcolor;	
};

kwMenubar.ActiveId = -1;
kwMenubar.Padding = 0;
kwMenubar.SepPadding = 5;

/**
 *	Add a kwMenu.
 *
 *	@param	text	The text to be displayed on the kwMenubar for this kwMenu.
 *	@param	link	[optional] The link used if this kwMenu is only a link.
*/
kwMenubar.prototype.add = function(text, link, image, imageWidth, imageHeight)
{
  var menuObj = new kwMenu(text, link, image, imageWidth, imageHeight);
  if (this.isFirstMenu)
  {
	menuObj.isFirst = true;
	this.isFirstMenu = false;
  }
  this.innerHTML += menuObj.getHTML();
  this.menuList[this.menuList.length] = menuObj;
  menuObj.menuBarObj = this;
  return menuObj;
};

/**
 *	Show the kwMenu bar.
*/
kwMenubar.prototype.show = function(elm)
{
	this.parentelm = elm;
    var html = '<table border="0" cellspacing="0" cellpadding="' + kwMenubar.Padding + '" class="' + this.style + '"><tr>' + this.innerHTML + '</tr></table>';
	document.open();
	document.write(html);
	document.close();
	for (var i = 0; i < this.menuList.length; i++)
	{
      okwMenu = document.getElementById(this.menuList[i].id);
      okwMenu.menuObj = this.menuList[i];
	}
};

/**
 *	Class to display a kwMenu.
 *
 *	@author			Adam Callen
 *	@version		1.0
 *	@copyright	Katana Productions Pty Ltd 2004
 *
 *	@param	text	The text to be displayed on the kwMenubar for this kwMenu.
 *	@param	link	[optional] The link used if this kwMenu is only a link.
*/
function kwMenu(text, link, image, imageWidth, imageHeight)
{
  this.text = text;
  this.link = unescape(link);
  if(image)
  {  this.image = image;
  }
  if(imageWidth)
  {    
    this.imageWidth = imageWidth;
  }
  if(imageHeight)
  {
    this.imageHeight = imageHeight;
  }
  this.x = 0;
  this.y = 0;
  this.id = 'kwMenu' + kwMenu.count++;
  this.style = 'menu';
  this.isFirst = false;
  this.hasMenuItems = false;
  this.menuBarObj = null;
  this.Menuitems = new Array();
};

kwMenu.count = 0;
kwMenu.timerID = 0;
kwMenu.minWidth = 140;

/**
 *	Add a kwMenu item.
 *
 *	@param	text	The text to be displayed on the kwMenubar for this kwMenu.
 *	@param	link	The link for this kwMenu item.
*/
kwMenu.prototype.add = function(text, link)
{
  this.hasMenuItems = true;
  var menuitemObj = new kwMenuItem(text, link);
  this.Menuitems[this.Menuitems.length] = menuitemObj;
  return menuitemObj;
};

/**
 *	Get the HTML for the kwMenu.
*/
kwMenu.prototype.getHTML = function()
{
	var html = '';
	if (!this.isFirst)
	{
	  if(!this.image)
	  {
		html += '<td style="padding-left:' + kwMenubar.SepPadding + 'px;padding-right:' + kwMenubar.SepPadding + 'px;">|</td>';
	  }
	}
	else
	{
	  if(!this.image)
	  {
		html += '<td style="padding-left:' + kwMenubar.SepPadding + 'px;padding-right:' + kwMenubar.SepPadding + 'px;"> </td>';
	  }
	}
	if(this.image)
	{
      html += '<td width="' + this.imageWidth + '" height="' + this.imageHeight + '">';
	}
	else
	{
      html += '<td>';	  
	}
	var href;
	if (this.link)
	{
		href = this.link;
	}
	else
	{
		href = '#';
	}
	if(this.image)
	{
	  html += '<a id="' + this.id + '" href="' + href + '" onmouseover="kwMenu.onmouseover(event)" onmouseout="kwMenu.setTimer()" class="' + this.style + '"><img src="' + this.image + '" width="' + this.imageWidth + '" height="' + this.imageHeight + '" border="0" class="' + this.style + '"></a></td>';
	}
	else
	{
	  html += '<a id="' + this.id + '" href="' + href + '" onmouseover="kwMenu.onmouseover(event)" onmouseout="kwMenu.setTimer()" class="' + this.style + '">' + unescape(this.text) + '</a></td>';
    }
  return html;
};

/**
 *	Show the kwMenu.
*/
kwMenu.prototype.show = function(x, y, w)
{
    var tableWidth=(w + 2 * kwMenubar.SepPadding + 10);
	kwMenu.hide();

	if (this.hasMenuItems)
	{
		if (this.isFirst)
		{
			x = this.menuBarObj.parentelm.offsetLeft;
		}
		else
		{
			x = x - kwMenubar.SepPadding + 3;
		}
  var theDoc = document;
  var theBody = theDoc.body;

  tbl = theDoc.createElement("TABLE");
  tbl.id = this.id + "_kwMenutable";
  tbl.border = 0;
  tbl.cellpadding = 0;
  tbl.cellspacing = 0;
//  tbl.className = this.style;
  tbl.onmouseover = kwMenu.clearTimer;
  tbl.onmouseout = kwMenu.setTimer;
  tbl.style.position = 'absolute';
  tbl.style.left = x + 'px';
  tbl.style.top = y + 'px';
  tbl.style.zIndex = '3';
  tbl.width = tableWidth+'px';
  tbl.style.backgroundColor = '#A9AB8C';
  tbody = theDoc.createElement("TBODY");
  tbl.insertBefore(tbody, null);
  theBody.insertBefore(tbl, null);
  for (i=0; i<this.Menuitems.length; i++) {
     tbody.insertBefore(this.Menuitems[i].createElements(), null);
  }
  kwMenubar.ActiveId = this.id;
  for (var i = 0; i < this.Menuitems.length; i++)
  {
      okwMenuitem = document.getElementById(this.Menuitems[i].id);
      okwMenuitem.kwMenuitem = this.Menuitems[i];
	}
  }
};

/**
 *	Hide the kwMenu.
*/
kwMenu.hide = function()
{
	if (kwMenubar.ActiveId != -1)
  {
		var oTable = document.getElementById(kwMenubar.ActiveId + '_kwMenutable');
		document.body.removeChild(oTable);
		kwMenubar.ActiveId = -1;
		document.body.onmouseup = null;
	}
};

/**
 *	Event handler for mouse over a kwMenu item.
*/
kwMenu.onmouseover = function(e)
{
	if (!e) var e = window.event;
	var elm = e.srcElement?e.srcElement : e.target; // allows for cross compatibility
	kwMenu.clearTimer();
    // when there is an image we need to reference the outer html
	if(!elm.menuObj)
	{
	  elm = elm.parentNode;
	}
	var top = elm.menuObj.menuBarObj.top;
	if (!top)
	{
		top = elm.menuObj.menuBarObj.parentelm.offsetTop;
	}
    oCurrentNode = elm.parentNode;
    iLeft = 0;
    while(oCurrentNode.tagName!="BODY"){
      iLeft+=oCurrentNode.offsetLeft;
      oCurrentNode=oCurrentNode.offsetParent;
    }
	// make sure there is a minimum width
//	alert("menuWidth="+elm.parentNode.offsetWidth);
	menuWidth = elm.parentNode.offsetWidth;
//	if (menuWidth<kwMenu.minWidth)
//	{
//	  menuWidth=kwMenu.minWidth;
//	}
	elm.menuObj.show(iLeft, top + elm.parentNode.offsetHeight, menuWidth);
};

/**
 *	Sets the kwMenu timer.
*/
kwMenu.setTimer = function()
{
	kwMenu.timerID = window.setTimeout("kwMenu.ontimeout()", 1000);
};

/**
 *	Clears the kwMenu timer.
*/
kwMenu.clearTimer = function()
{
	if (kwMenu.timerID)
	{
		window.clearTimeout(kwMenu.timerID);
		kwMenu.timerID = 0;
	}
};

/**
 *	Event handler for kwMenu time out.
*/
kwMenu.ontimeout = function()
{
  kwMenu.hide();
};

/**
 *	Class to encapsulate a kwMenu item.
 *
 *	@author			Adam Callen
 *	@version		1.0
 *	@copyright	Katana Productions Pty Ltd 2004
*/
function kwMenuItem(text, link)
{
  this.text = unescape(text);
  this.link = unescape(link);
  this.id = 'kwMenuitem' + kwMenuItem.count++;
  this.style = 'menuItem';
};
kwMenuItem.count = 0;

/**
 *	Get the HTML for the kwMenu item.
*/
kwMenuItem.prototype.getHTML = function()
{
  var html = '<tr><td width="100%" noWrap onmouseover="kwMenuItem.onmouseover()" onmouseout="kwMenuItem.onmouseout()" onclick="kwMenuItem.onclick(\'' + this.link + '\')" style="cursor:hand;padding:3px;padding-left:10px;padding-right:10px;" class="' + this.style + '">' + this.text + '</td></tr>';
  return html;
};

/**
 *	Get the HTML for the kwMenu item.
*/
kwMenuItem.prototype.createElements = function()
{
  var theDoc = document;
  var theBody = theDoc.body;
  tr = theDoc.createElement("TR");
  td = theDoc.createElement("TD");
  a = theDoc.createElement("a");
  a.href = unescape(this.link);
  a.id = this.id;
  a.onmouseover = kwMenuItem.onmouseover;
  a.onmouseout = kwMenuItem.onmouseout;
  a.className = this.style;
  text = theDoc.createTextNode(unescape(this.text));
  a.border="0";
  a.style.cursor="hand";
  a.style.width="100%";
  a.style.padding="1px";
  a.style.paddingLeft="10px";
  a.style.paddingRight="10px";
  a.style.zIndex = '3';
  tr.insertBefore(td, null);
  a.insertBefore(text, null);
  td.insertBefore(a, null);
  return tr;
};

/**
 *	Event handler for mouse over a kwMenu item.
*/
kwMenuItem.onmouseover = function()
{
	kwMenu.clearTimer();
	var elm = window.event.srcElement;
	elm.parentNode.parentNode.style.backgroundColor = '#666930';
};

/**
 *	Event handler for mouse out of a kwMenu item.
*/
kwMenuItem.onmouseout = function()
{
	var elm = window.event.srcElement;
	elm.parentNode.parentNode.style.backgroundColor = '';
};

/**
 *	Event handler for kwMenu item click.
*/
kwMenuItem.onclick = function()
{
	var elm = window.event.srcElement;
	window.location = elm.kwMenuitem.link;
};
