/*
'+====================================================================+
'| THE SOFTWARE NETWORK                                               |
'+====================================================================+
'| NAME:		jvsSNMPopupMenu.js                                    |
'| TYPE:		CLIENT-SIDE JAVA SCRIPT                               |
'| APPLICATION:	THE SOFTWARE NETWORK ADMIN SITE                       |
'| AUTHOR:		TSN                                                   |
'| CREATE DATE:	2004-06-11                                            |
'+====================================================================+
'| VERSION LEVEL/CHANGE HISTORY                                       |
'+====================================================================+
'| V0.0.1/2004-06-11 - Created                                        |
'| V0.5.0/2004-06-14 - First beta version released                    |
'| V0.9.0/2004-06-14 - RC1 version released                           |
'+====================================================================+
*/

// Constants
var	SNM_PM_STRING		= 0x0001;
var SNM_PM_SEPARATOR	= 0x0002;
var mousex = 0;
var mousey = 0;

//Document mousemove handler
if(isNC4) document.captureEvents(Event.MOUSEMOVE);
if(isMSIE){
	document.onmousemove=function()
	{
		mousex=event.clientX+document.body.scrollLeft;
		mousey=event.clientY+document.body.scrollTop;
		return true;
	}
}else if (isOpera7){
	document.onmousemove=function()
	{
		mousex=event.clientX+document.body.scrollLeft;
		mousey=event.clientY+document.body.scrollTop;
		return true;
	}
	document.oncontextmenu = function()
	{
		event.returnValue = false;
		event.cancelBubble = true;
		return false;
	}
}else if(isOpera){
	document.onmousemove=function()
	{
		mousex=event.clientX;
		mousey=event.clientY;
		return true;
	}
	document.oncontextmenu = function()
	{
		event.returnValue = false;
		event.cancelBubble = true;
		return false;
	}
}else if(isNC){
	document.onmousemove=function(e)
	{
		mousex = e.pageX;
		mousey = e.pageY;
		return true;
	}
	document.oncontextmenu = function(e)
	{
		return false;
	}
}

// Public Methods
function SNMPopupMenu_Append (oItem)
{
	oItem.Parent = this;
	this.Items [this.ItemCount++] = oItem;
}

function SNMPopupMenu_Create ()
{
	document.writeln ('<div id="pm_' + this.Name + '" ' +
		'style="width:'+this.Width+'px;position:absolute;"' +
		'onmouseover = "' + this.Name + '.OnMouseOver();"' +
		'onmouseout	 = "' + this.Name + '.OnMouseOut();">');
	document.writeln ('</div>');
}

function SNMPopupMenu_Update ()
{
	var sContent = "";
	var i;
	sContent += '<table cellpadding="0" cellspacing="0" border="0" class="menuOutsideTable" >';
	sContent += "<tr><td>";
	sContent += '<table cellspacing="0" border="0" cellpadding="0" class="menuInsideTable"> ';

	// Draw items here
	for (i = 0; i < this.ItemCount; i++)
	{
		sContent += this.Items [i].Draw (i);
		this.Height += this.Items [i].Height;
	}
	sContent += "</table></td></tr></table>";
	this.Layer = layer ("pm_" + this.Name);
	document.getElementById ("pm_" + this.Name).innerHTML = sContent;
	this.Hide();
}


function SNMPopupMenu_Show ()
{
	this.Layer.setVisibility(true);
	this.Layer.setDisplay(true);
}

function SNMPopupMenu_Hide ()
{
	var i;
	var flag = 0;
	for (i = 0; i < this.ItemCount; i++)
	{
		if (this.Items [i].ItemCount == 1)
		{
			if(this.Items [i].Items[0].UnderMenu == 1)
			{
				flag = 1;
			}
			else
			{
				var loTD = layer("td_"+this.Name);
				if (loTD.object)
				{
					if (this.Parent.Items[this.Parent.activeTab] == this)
					{
						loTD.object.className = "ActiveTab";
						loTD.object.children[0].className = "ActiveTab";
					}
					else
					{
						loTD.object.className = "inActiveTab";
						loTD.object.children[0].className = "inActiveTab";
					}
				}
				this.Layer.setVisibility(false);
				this.Layer.setDisplay(false);
			}
		}
	}
	if (flag==0)
	{	
		if (this.Layer != null)
		{
			this.Layer.setVisibility(false);
			this.Layer.setDisplay(false);
			var loTD = layer("td_"+this.Name);
			if (loTD.object)
			{
				if (this.Parent.Items[this.Parent.activeTab] == this)
				{
					loTD.object.className = "ActiveTab";
					loTD.object.children[0].className = "ActiveTab";
				}
				else
				{
					loTD.object.className = "inActiveTab";
					loTD.object.children[0].className = "inActiveTab";
				}
			}
		}
	}
}

function SNMPopupMenu_Popup (sParam1, sParam2)
{
	this.Param1 = "";
	this.Param2 = "";
	
	this.Param1 = sParam1;
	this.Param2 = sParam2;
	
	for (var i=0; i<this.Parent.ItemCount;i++)
	{
		if (this != this.Parent.Items[i])
		{
			this.Parent.Items[i].Hide();
		}
	}
	
	if (this.Param2 == "SecondMenu")
	{
		// Calculate menu position
		var x;
		var y;
	
		x = 0;
		y = 0;
	
		var menu_x;
		var menu_y;
		
		menu_x = document.getElementById ("pm_" + this.Parent.Parent.Name).style.left;
		menu_x = menu_x.substring (0, menu_x.length -2);
		var digMenu = new Number (menu_x);
		menu_y = document.getElementById ("pm_" + this.Parent.Parent.Name).style.top;
		menu_y = menu_y.substring (0, menu_y.length -2);
		var digMenu_y = new Number (menu_y);
	
		if ((digMenu + 190 + 190) > getWindowWidth())
		{
			x = digMenu - 190;
		}
		else
		{
			x = 190 + digMenu;
		}
		if ((mousey + this.Parent.Parent.Height + 25) > getWindowHeight())
		{	
			y = digMenu_y - this.Height + 74;
		}
		else
		{
			y = digMenu_y - 2 + 76;
		}	
	}

	else {
		// Calculate menu position
		var x;
		var y;
		var oTD = layer ("td_"+this.Name);
		oTD.object.className = "TabHover";
		oTD.object.children[0].className = "TabHover";
		
		x = oTD.getAbsoluteLeft();
		y = oTD.getAbsoluteTop()+oTD.getHeight();
//alert(x+" "+y+", "+mousex+" "+mousey+", "+getWindowWidth() + " " +getWindowHeight());
		if ((x + this.Width) > getWindowWidth())
		{
			x = x + oTD.getWidth()- this.Width;
		}
		else
		{
			x = x;
		}
		if ((y + this.ItemCount*20) > getWindowHeight())
		{
			y = y - oTD.getHeight() - this.ItemCount*24;
		}
		else
		{
			y = y;
		}
	}
	// Show menu
	document.getElementById ("pm_" + this.Name).style.left = x;
	document.getElementById ("pm_" + this.Name).style.top = y;
	if (this.Parent.Type == 0)
	{
		this.Show ();
	}
	if (window.event)
	{
		window.event.returnValue = false;
	}
}

// Event Handlers
function SNMPopupMenu_OnMouseOver ()
{
	this.UnderMenu = 1;
}

function SNMPopupMenu_OnMouseOut ()
{
	if (this.TimeID == null)
	{
		this.TimeID = window.setInterval (this.Name+".ClearTimeout();",this.Time);
	}
	this.UnderMenu = 0;
}

function SNMPopupMenu_ClearTimeout ()
{
	if (this.UnderMenu == 0)
	{
		if (this.TimeID != null)
		{
			var loTD = layer ("td_"+this.Name);
			if (!(mousex >= loTD.getAbsoluteLeft() && mousex <= loTD.getAbsoluteLeft() + loTD.getWidth() 
				&& mousey >= loTD.getAbsoluteTop() && mousey <= loTD.getAbsoluteTop() + loTD.getHeight()))
			{
				window.clearInterval (this.TimeID);
				this.TimeID = null;
				this.Hide();
				this.UnderMenu = 0;
			}
		}
	}
}	

function SNMPopupMenu_SetTimeout (time)
{
	this.Time = time;
}

// Class Definition
function SNMPopupMenu (sName)
{	
	// Internal data
	this.ItemCount		= 0;
	this.Layer			= null;
	this.Items			= new Array ();
	
	// Public properties
	this.Name			= sName;
	this.Parent			= null;
	this.Height			= 0;
	this.Time			= 10;
	this.TimeID			= null;
	this.UnderMenu		= 0;
	this.Width			= 150;
	
	// Public methods
	this.Append			= SNMPopupMenu_Append;
	this.Create			= SNMPopupMenu_Create;
	this.Update			= SNMPopupMenu_Update;
	this.Show			= SNMPopupMenu_Show;
	this.Popup			= SNMPopupMenu_Popup;
	this.Hide			= SNMPopupMenu_Hide;

	this.SetTimeout		= SNMPopupMenu_SetTimeout;
	this.ClearTimeout	= SNMPopupMenu_ClearTimeout;
	
	// Event handlers
	this.OnMouseOver	= SNMPopupMenu_OnMouseOver;
	this.OnMouseOut		= SNMPopupMenu_OnMouseOut;
}

////////////////////////////////////////////////////////////////
// Public Methods
function SNMPopupMenuItem_Append (oItem)
{
	oItem.Parent = this;
	this.Items [this.ItemCount++] = oItem;
}
// SNMPopupMenuItem class
// Public functions
function SNMPopupMenuItem_Draw (iItemIndex)
{	
	var sContent = "";
	if (this.Type == SNM_PM_STRING)
	{
		sContent += '<tr' +
		' onmouseover = "' + this.Parent.Name + '.Items[0].OnMouseOver(' + "'" + this.Parent.Name + "'" + ',' + iItemIndex + ');" ' +
		' onmouseout  = "' + this.Parent.Name + '.Items[0].OnMouseOut(' + "'" + this.Parent.Name + "'" + ',' + iItemIndex + ');" ' +
		'onclick = "' + this.Parent.Name + '.Items[0].OnClick(' + "'" + this.Parent.Name + "'" + ',' + iItemIndex + ');">' + 
		'<td id="mtd_' + this.Parent.Name + '_' + iItemIndex + '" style="padding-left:2px;padding-right:2px;padding-top:2px;padding-bottom:2px;">';
		sContent += ' <table class="menuNormal" cellspacing="0" cellpadding="0" id="mtab_' + this.Parent.Name + '_' + 
		iItemIndex + '">';
		sContent += "<tr>";
		if (this.IconNormal != "")
		{
			sContent += '<td width="30" style="padding-left:2px;"><img id="mim_' + iItemIndex + '" src="' + this.IconNormal + '" width="16" height="16"></td>';
		}
		else
		{
			sContent += '<td width="30" style="padding-left:2px;cursor:default;">&nbsp;</td>';
		}
		sContent += '<td class="menuText" style="height: 20px; cursor:default;" width="160">' + this.Text + '</td>';
		sContent += "</tr>";
		sContent += "</table>";
		sContent += "</td>";
		sContent += "</tr>";
		this.Height = 20;
	}
	else	//draw line
	{
		sContent += '<tr><td><table cellspacing="0" cellpadding="0"><tr>';
		sContent += '<td width="30"></td>';
		sContent += '<td width="166" bgcolor="blue"></td>';
		sContent += '</tr></table></td></tr>';
		this.Height = 4;
	}
	
	return sContent;
}

// Event Handlers
function SNMPopupMenuItem_OnClick (sName, Selected)
{
	var sCommand = "";
	sCommand = this.Parent.Items[Selected].Handler + "(";
	if (this.Parent.Items[Selected].Params != null)
	{
		for (var i=0;i<this.Parent.Items[Selected].Params.length;i++)
		{
			sCommand += "'" + this.Parent.Items[Selected].Params[i] + "'";
			if (i != this.Parent.Items[Selected].Params.length-1)
			{
				sCommand += ",";
			}
		}
	}
/*	if (this.Parent.Param1 != "")
	{
		// Add first parameter
		sCommand += "'" + this.Parent.Param1 + "'";
	}
	if (this.Parent.Param2 != "")
	{
		// Add second parameter
		sCommand += ", '" + this.Parent.Param2 + "'";
	}
*/	sCommand += ");";
	this.OnMouseOut(sName, Selected);
	this.Parent.Hide();
	eval (sCommand);
}

function SNMPopupMenuItem_OnMouseOver (sName, iRowIndex)
{
	// Highlight row
	document.getElementById ("mtab_" + sName + '_' + iRowIndex).className = "menuHover";
	if (this.Parent.Items[iRowIndex].ItemCount == 1)
	{
		this.Parent.Items[iRowIndex].Items[0].Popup(this.Parent.Param1, "SecondMenu");
	}
	if (this.Parent.Items[iRowIndex].IconNormal != "" && this.Parent.Items[iRowIndex].IconSelected != "")
	{
		document.getElementById ("mim_" + iRowIndex).src = this.Parent.Items[iRowIndex].IconSelected;
	}
	if (this.Parent.TimerID != null)
	{
		window.clearInterval (this.Parent.TimerID);
		this.Parent.TimerID = null;
	}

		var i;
		for (i=0;i<this.Parent.ItemCount;i++)
		{
			if (i != iRowIndex)
			{
				if (this.Parent.Items[i].ItemCount == 1)
				{
					this.Parent.Items[i].Items[0].Hide();
				}
			}
		}
}

function SNMPopupMenuItem_OnMouseOut (sName, iRowIndex)
{	
	document.getElementById ("mtab_" + sName + '_' + iRowIndex).className = "menuNormal";
	document.getElementById ("mtd_" + sName + '_' + iRowIndex).style.padding = "2px 2px";
	if (this.Parent.Items[iRowIndex].IconNormal != "" && this.Parent.Items[iRowIndex].IconSelected != "")
	{
		document.getElementById ("mim_" + iRowIndex).src = this.Parent.Items[iRowIndex].IconNormal;
	}
}

// Class Definition
function SNMPopupMenuItem (iType, sText, sHandler, sIconNormal, sIconSelected, aParams)
{
	// Internal data
	this.ItemCount		= 0;
	this.Items			= new Array ();
	this.Params			= aParams;

	// Class data
	this.Type			= iType;
	this.Text			= sText;
	this.Handler		= sHandler;
	this.IconNormal		= sIconNormal;
	this.IconSelected	= sIconSelected;

	this.Height			= 0;

	// Public functions
	this.Draw			= SNMPopupMenuItem_Draw;

	// Event Handlers
	this.OnClick		= SNMPopupMenuItem_OnClick;
	this.OnMouseOver	= SNMPopupMenuItem_OnMouseOver;
	this.OnMouseOut		= SNMPopupMenuItem_OnMouseOut;
	this.Append			= SNMPopupMenuItem_Append;
}

// Public Methods
function SNMMainPopupMenu_Append (oItem, iWidth, sName, sOnClick, iActiveTab)
{
	if (iActiveTab == 1)
	{
		this.activeTab = this.ItemCount;
	}
	oItem.Parent = this;
	this.Names [this.ItemCount] = sName;
	this.OnClicks [this.ItemCount] = sOnClick;
	this.Widths [this.ItemCount] = iWidth;
	this.Items [this.ItemCount++] = oItem;
	oItem.Create();
}

function SNMMainPopupMenu_Create ()
{
	document.writeln ('<div id="mmenu_' + this.Name + '">');
	document.writeln ('</div>');
}

function SNMMainPopupMenu_Update ()
{
	var sClass = "inActiveTab";
	var i;

	// Draw items here
	for (i = 0; i < this.ItemCount; i++)
	{
		if (this.activeTab == i)
			sClass = "ActiveTab";
		else
			sClass = "inActiveTab";

		this.Items[i].Update();
	}
	this.Layer = layer ("mmenu_" + this.Name);
	return true;
}

// Event Handlers
function SNMMainPopupMenu_OnMouseOver (i)
{
	this.Items[i].Popup();
	this.Items[i].OnMouseOver();
}

function SNMMainPopupMenu_OnMouseOut (i)
{
	this.Items[i].OnMouseOut();
}

function SNMMainPopupMenu_Hide ()
{
	for (var i=0;i<this.ItemCount;i++)
	{
		this.Items[i].Hide();
	}
}
// Class Definition
function SNMMainPopupMenu (sName, iType)
{
	// Internal data
	this.ItemCount		= 0;
	this.Items			= new Array ();
	this.Names			= new Array ();
	this.OnClicks		= new Array ();
	this.Widths			= new Array ();
	
	//Class data
	this.Type			= iType; //Direction of the Main Menu
	this.Name			= sName;
	this.UnderMenu		= 0;
	this.Layer			= null;
	this.activeTab		= -1;
	
	// Public methods
	this.Append			= SNMMainPopupMenu_Append;
	this.Create			= SNMMainPopupMenu_Create;
	this.Update			= SNMMainPopupMenu_Update;
	this.Hide			= SNMMainPopupMenu_Hide;

	// Event handlers
	this.OnMouseOver	= SNMMainPopupMenu_OnMouseOver;
	this.OnMouseOut		= SNMMainPopupMenu_OnMouseOut;
}