// ####################################################################
// Popup Menu Library v0.2
// IE 5+, NN 6
// ####################################################################

// ####################################################################
// Style Names for use with menu Items
// ####################################################################
var menuItemStyleName = 'menuItem';
var menuItemStyleNameOver = 'menuItemOver';

// ####################################################################
// Menu Class
// ####################################################################
function MenuCreate()
{
	document.write('<DIV ID="' + this.Name + '" onMouseOver="this.style.visibility=\'visible\'" onMouseOut="this.style.visibility=\'hidden\'" style="position: absolute; visibility: hidden; top: 0px; left: 0px;">');
	
	for (var i=0; i< this.Items.length; i++)
		this.Items[i].Create();
		
	document.write('</DIV>');
}

function MenuAddItem(name, text, url, hasSubMenu, subMenu)
{
		this.Items[this.Items.length] = new MenuItem(name, text, url, this, hasSubMenu, subMenu, menuItemStyleName, menuItemStyleNameOver);
	
}

function MenuShow()
{
	var myMenu = document.getElementById(this.Name);
	var coorParent = getElementPosition(this.Owner);
	myMenu.style.top = coorParent.top;
	myMenu.style.left = coorParent.left + coorParent.width;
	myMenu.style.visibility = 'visible';
	if (this.isNestedMenu == true)
		this.ParentMenu.Show();
	
}

function MenuHide()
{
	var myMenu = document.getElementById(this.Name);
	myMenu.style.visibility = 'hidden';
	if (this.isNestedMenu == true)
		this.ParentMenu.Hide();
}

function Menu(menuName, owner, isNestedMenu, parentMenu)
{
	this.Name = menuName;
	this.Owner = owner;
	this.Items = new Array();
	this.isNestedMenu = isNestedMenu || false;
	this.ParentMenu = parentMenu || false;
	this.Create = MenuCreate;
	this.AddItem = MenuAddItem;
	this.Show = MenuShow;
	this.Hide = MenuHide;
}

// ##########################################################################
// Class Menu Item
// ##########################################################################
function MenuItemCreate()
{
	if (this.HasSubMenu == true)
	{
		var strMenuItem = '<DIV ID="' + this.Name + '" onMouseOver="this.className=\'' + this.StyleOver +'\';' + this.SubMenu.Name + '.Show();' +  this.Owner.Name + '.Show();" onMouseOut="' + this.SubMenu.Name + '.Hide();this.className=\'' + this.Style + '\'" CLASS="' + this.Style + '"><A HREF="#" onClick="return false;">' + this.Text + '</A></DIV>';
		
		document.write(strMenuItem);
		
		//this.SubMenu.Create();		
	}
	else
	{
		document.write('<DIV ID="' + this.Name + '" CLASS="' + this.Style + '" onMouseOver="this.className=\'' + this.StyleOver + '\';' + this.Owner.Name + '.Show()"    onMouseOut="this.className=\'' + this.Style + '\';' + this.Owner.Name + '.Hide();"><A HREF="' + this.Url + '">' + this.Text + '</a></DIV>');
	}
}

function MenuItemShow()
{
	this.Owner.Show();
}

function MenuItem(name, text, url, owner, hasSubMenu, subMenu, style, styleOver)
{
	this.Name = name;
	this.Text = text;
	this.Url = url;
	this.Owner = owner;
	this.HasSubMenu = hasSubMenu;
	this.SubMenu = subMenu;
	this.Style = style;
	this.StyleOver = styleOver;
	this.Create = MenuItemCreate;
	this.Show = MenuItemShow;
}

// ############################################################################
// get an HTML element top, left, width, height
// ############################################################################
function getElementPosition(elemID) {
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
	var offsetWidth = offsetTrail.offsetWidth;
	var offsetHeight = offsetTrail.offsetHeight;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop, width:offsetWidth, height:offsetHeight};
}