// JavaScript Document

var ns6Xfix 	= +0;
var ns6Yfix 	= +0;

var ieMouseFiX	= +0;
var ieMouseFiY	= -3;

var bInitialized = false;

function moveTo(obj, x, y)
{
	if (ie4 || ns6) obj = obj.style;
    obj.xpos = x;
    obj.left = obj.xpos;
    obj.ypos = y;
    obj.top = obj.ypos;
}

function show(oLyr)
{
	if (ns4)
		oLyr.visibility="show";
	else
		oLyr.style.visibility="visible";
	return true;
}

function hide(oLyr)
{
	if (ns4)
		oLyr.visibility="hide";
    else
		oLyr.style.visibility="hidden";
	return true;
}

function hideAllMenus()
{
	// Loop through the menus
	for (q=0; q<oMenus.length; q++)
	{
		if (oMenus[q].status == 'visible')
			oMenus[q].hide();
	}
}

// Handles the mouse moving
function mouseMove(e)
{
	if (!bInitialized)
		return true;
	
	mXpos = (ns4 || ns6)?e.pageX:(event.clientX+document.body.scrollLeft);
	mYpos = (ns4 || ns6)?e.pageY:(event.clientY+document.body.scrollTop);
	
	var x = mXpos;
	var y = mYpos;
	
	if (ie4)
	{
		x += ieMouseFiX;
		y += ieMouseFiY;
	}

	var childNumber;
	
	// Loop through the menus
	for (q=0; q<oMenus.length; q++)
	{
		if (oMenus[q].status == 'visible' && !oMenus[q].isInsideBounds(x, y))
		{
			oMenus[q].hide();
			if (!ns4)
				document.body.style.cursor = 'default';
		}
		else if (oMenus[q].status == 'visible')
		{
			childNumber = oMenus[q].isInsideChild(x, y);
			if (childNumber != -1)
			{
				if (!ns4)
					document.body.style.cursor = 'hand';
				if (oMenus[q].childStatus != childNumber)
				{
					if (oMenus[q].childStatus != -1)
						oMenus[q].fillChild(oMenus[q].childStatus, false);
					oMenus[q].fillChild(childNumber, true);
				}
			}
			else if (oMenus[q].childStatus != childNumber)
			{
				oMenus[q].fillChild(oMenus[q].childStatus, false);
			}
		}
	}
    return true;
}

// Handles the mouse moving
function mouseUp(e)
{
	if (!bInitialized)
		return true;

	mXpos = (ns4 || ns6)?e.pageX:(event.clientX+document.body.scrollLeft);
	mYpos = (ns4 || ns6)?e.pageY:(event.clientY+document.body.scrollTop);
	var x = mXpos;
	var y = mYpos;
	
	if (ie4)
	{
		x += ieMouseFiX;
		y += ieMouseFiY;
	}
	
	var childNumber = -1
	for (q=0; q<oMenus.length; q++)
	{
		if (oMenus[q].status == 'visible')
		{
			childNumber = oMenus[q].isInsideChild(x, y);
			if (childNumber != -1)
			{
				document.location.href=oMenus[q].aLnk[childNumber];
				break;
			}
		}
	}
	
	return true;
}


Menu = function(sID)
{
	var Xfix, Yfix;

	if (ns4)
	{
		this.oMain = eval("document.layers['"+sID+"']");
		Xfix = 0;
		Yfix = 0;
	}
	else if (ie4)
	{
		Xfix = ie4Xfix;
		Yfix = ie4Yfix;
		this.oMain = document.all[sID];
	}
	else if (ns6)
	{
		Xfix = ns6Xfix;
		Yfix = ns6Yfix;
		this.oMain = document.getElementById(sID);
	}
	
	this.mainID = sID;
	
	// Read the config
	this.img				= eval("document.images["+sID+"_image_name]");
	
	this.status				= eval(sID+"_default_status");
	
	this.text_size 			= eval(sID+"_text_size");
	this.text_font 			= eval(sID+"_text_font");
	this.text_color			= eval(sID+"_text_color");
	this.text_align			= eval(sID+"_text_align");
	
	this.backgr_color 		= eval(sID+"_backgr_color");
	this.text_over_color 	= eval(sID+"_text_over_color");
	this.backgr_over_color 	= eval(sID+"_backgr_over_color");
	this.border_color 		= eval(sID+"_border_color");
	
	this.bar_border_width 	= eval(sID+"_bar_border_width");
	this.bar_width 			= eval(sID+"_bar_width");
	this.bar_height			= eval(sID+"_bar_height");
	this.start_left			= eval(sID+"_start_left");
	this.start_top 			= eval(sID+"_start_top");
	
	
	this.childStatus		= -1;
	
	// Fixing...
	this.start_left 	+=Xfix;
	this.start_top  	+=Yfix;
	
	moveTo(this.oMain, this.start_left, this.start_top);
	
	this.aTxt = eval(sID+"_text");
	this.aLnk = eval(sID+"_link");
	
	this.layerWrite(this.oMain, this.getMainText());
	this.initChildrenValues();
	this.setMenuBounds();
}

Menu.prototype.isInsideChild = function(x, y)
{
	var len = this.aLnk.length;

	var tLeft, tRight, tTop, tBottom;
	tLeft 		= this.start_left + this.bar_border_width;
	tRight 		= this.start_left + this.bar_width - this.bar_border_width;
	
	if (x < tLeft || x > tRight)
		return -1;

	for (i=0; i < len; i++)
	{
		tTop 	= parseInt(this.start_top + (i * this.bar_height) + this.bar_border_width);
		tBottom	= tTop + this.bar_height - this.bar_border_width;
		if (y >= tTop  &&  y <= tBottom)
			return i;
	}
	
	return -1;
}

Menu.prototype.setMenuBounds = function()
{
	this.bndLeft 		= this.start_left;
	this.bndTop			= this.start_top - this.img.height;
	this.bndBottom		= this.start_top + (this.bar_height * this.aLnk.length);
	this.bndRight		= this.start_left + this.bar_width;
}

Menu.prototype.isInsideBounds = function(x, y)
{
	if (x >= this.bndLeft  &&  x <= this.bndRight  &&  y >= this.bndTop  &&  y <= this.bndBottom)
		return true;
	else
		return false;
}

Menu.prototype.getChild = function(chldid)
{
	var oRes = null;
	if (ns4)
		oRes = eval("document."+this.mainID+".document."+chldid);
	else if (ie4)
		oRes = document.all[chldid];
	else if (ns6)
		oRes = document.getElementById(chldid);

	return oRes;
}

Menu.prototype.show = function()
{
	var len = this.aTxt.length;
	var sName = "";
	var oRef = null;
	this.initChildrenValues();
	this.status = 'visible';
	show(this.oMain);
	for (i=0; i < len; i++)
	{
		sName = '' + this.mainID + i;
		oRef = this.getChild(sName);
		show(oRef);
	}
}

Menu.prototype.hide = function()
{
	var len = this.aTxt.length;
	var sName = "";
	for (i=0; i < len; i++)
	{
		sName = '' + this.mainID + i;
		hide(this.getChild(sName));
	}
	hide(this.oMain);
	this.status = 'hidden';
}

Menu.prototype.layerWrite = function(oLyr, sText)
{
	if (ns4)
	{
		oLyr.document.open();
		oLyr.document.write(sText);
		oLyr.document.close();
	}
	else oLyr.innerHTML = sText;
}

Menu.prototype.getMainText = function()
{
	var sResult = "";
	var len = this.aTxt.length;
	var sName;
	
	for (i=0; i < len; i++)
	{
		sName = '' + this.mainID + i;
		sResult += this.fillMain(sName, ((i * this.bar_height)), i);
	}
	return sResult;
}

Menu.prototype.fillChild = function(id, over)
{
	var sTable = '';
	var oChild = this.getChild(''+this.mainID+id);
	var text_color, back_color;
	if (over)
	{
		text_color = this.text_over_color;
		back_color = this.backgr_over_color;
	}
	else
	{
		text_color = this.text_color;
		back_color = this.backgr_color;
	}
	
	this.setChildBGColor(oChild, back_color);
	
	/*
	var sOver = ((over)?("onMouseOut"):("onMouseOver")) +
	 "=\"javascript:(getMenuByName('"+this.mainID+"')).fillChild("+id+", " +
	 ((over)?("false"):("true"))+");\"";
	*/

	sOver = "";
	
	sTable = '<TABLE WIDTH="'+this.bar_width+'" BORDER="'+this.bar_border_width+'" CELLPADDING="0" CELLSPACING="0" BORDERCOLOR="'+this.border_color+'"><TR><TD ALIGN="'+this.text_align+'" HEIGHT="'+this.bar_height+'"><A HREF="'+this.aLnk[id] +'" '+sOver+'><FONT FACE="'+this.text_font+'" SIZE="'+this.text_size+'" COLOR="'+text_color+'">'+ this.aTxt[id] +'</FONT></A></TD></TR></TABLE>';
	
	this.childStatus = ((over)?(id):(-1));
	this.layerWrite(oChild, sTable);
}

Menu.prototype.setChildBGColor = function(oLyr, col)
{
	if (ns4)
		oLyr.bgColor = col;
	else
		oLyr.style.backgroundColor = col;
}

Menu.prototype.initChildrenValues = function()
{
	for (i=0; i<this.aLnk.length; i++)
	{
		this.fillChild(i, false);
	}
}

Menu.prototype.fillMain = function (divname, topposition, id)
{
	var sResult;

	text_color = this.text_color;
	back_color = this.backgr_color;
	sResult = '<DIV ID="' + divname +'" STYLE="position:absolute; visibility:hidden; left:'+0+'px; top:'+topposition+'px; width:'+this.bar_width+'px; height:'+this.bar_height+'px; z-index:0; border: 0px none '+this.border_color+'; overflow: visible;"><TABLE WIDTH="'+this.bar_width+'"><TR><TD HEIGHT="'+this.bar_height+'"> &nbsp; </TD></TR></TABLE></DIV>';
	return sResult;
}

function getMenuByName(sName)
{
	var oRet = null;
	for (i=0; i<oMenus.length; i++)
	{
		if (oMenus[i].mainID == sName)
		{
			oRet = oMenus[i];
			break;
		}
	}
	
	return oRet;
}

function hideAllMenus()
{
 	for (k=0; k < ListMenus.length; k++)
	{
		hideMenu(ListMenus[k]);
	}
	return true;
}

function showMenu(id)
{
	if (!bInitialized)
		return true;
	hideAllMenus();
	var oMenu = null;
	oMenu = getMenuByName(id);
	oMenu.show();
}

function hideMenu(id)
{
	if (!bInitialized)
		return true;

	var oMenu = null;
	oMenu = getMenuByName(id);
	oMenu.hide();
}

function Init()
{
	ie4 = false;
	ns4 = false;
	ns6 = false;
	if (document.all) ie4 = true;
	else if (document.layers) ns4 = true;
	else if (document.getElementById) ns6 = true;
	
	var oTmp = null;
 	for (k=0; k < ListMenus.length; k++)
	{
		oTmp = new Menu(ListMenus[k]);
		oMenus[k] = oTmp;
	}
	
	document.onmousemove=mouseMove;
	document.onmouseup=mouseUp;
    if (ns4)
	{
		document.captureEvents(Event.MOUSEMOVE);
		document.captureEvents(Event.MOUSEUP);
	}

	bInitialized = true;
	return true;
}
