// NICCON Menu

// Browser
var IE = (navigator.appVersion.indexOf('MSIE 5') + navigator.appVersion.indexOf('MSIE 6')) != -2;
var NS = navigator.appName.indexOf('Netscape') != -1 && parseInt(navigator.appVersion.charAt(0)) >= 5;

// Support
var menuSupported = IE || NS;

// Global Variables
var menus       = new Array();
var currentMenu = null;

// Constants
var menuColor   = '#3366FF';
var activeColor = '#3399FF';
var delayTime   = 600;

// Menu
function Menu()
{
	if (menuSupported) {
		// Attributes
		this.items   = new Array();
		this.opener  = null;
		this.touched = false;
		this.painted = false;

		// Register in global array
		this.id        = menus.length;
		menus[this.id] = this;

		// Add item
		this.add = function(title, hyperRef)
		{
			if (this.painted) {
				return
			}
			this.items[this.items.length] = new Item(title, hyperRef);
		}

		// Paint menu
		this.paint = function()
		{
			if (this.painted) {
				return;
			}
			document.write('<div id="Menu' + this.id + '" style="position:absolute; visibility:hidden;');
			document.write(' background-color:' + menuColor + ';"><table border=0 cellpadding=0 cellspacing=0>');
			for (var i = 0; i < this.items.length; i++) {
				this.items[i].paint();
			}
			document.write('</table></div>');
			this.painted = true;
		}

		// Get HTML-element
		this.getElement = function()
		{
			return (this.painted) ? document.getElementById('Menu' + this.id) : null;
		}

		// Pop up
		this.popUp = function(opener)
		{
			if (!this.painted) {
				return;
			} else if (currentMenu == this) {
				this.touched = true;
				return;
			} else if (currentMenu != null) {
				currentMenu.hide();
			}
			this.touched = true;
			currentMenu  = this;
			var obj = this.opener = opener;
			this.opener.originalColor = this.opener.style.backgroundColor;
			with (this.opener) {
				style.backgroundColor = menuColor;
				style.cursor          = IE ? 'hand' : 'pointer';
				var x = offsetLeft;
				var y = offsetTop;
			}
			while ((obj = obj.offsetParent) != null) {
				x += obj.offsetLeft;
				y += obj.offsetTop;
			}
			y += this.opener.offsetHeight;
			with (this.getElement()) {
				style.left = x;
				style.top  = y;
				style.visibility = 'visible';
			}
		}

		// Pop down
		this.popDown = function(itemElement)
		{
			if (!this.painted) {
				return false;
			}
			this.touched = false;
			if (itemElement != null) {
				itemElement.style.backgroundColor = menuColor;
			}
			setTimeout('if (currentMenu != null) currentMenu.hide()', delayTime);
		}

		// Hide menu
		this.hide = function()
		{
			if (!this.painted || this.touched || currentMenu != this) {
				return;
			}
			currentMenu = null;
			this.opener.style.backgroundColor  = this.opener.originalColor;
			this.getElement().style.visibility = 'hidden';
		}

		// Keep menu open
		this.keepOpen = function(itemElement)
		{
			if (!this.painted) {
				return;
			}
			this.touched = true;
			if (itemElement != null) {
				itemElement.style.backgroundColor = activeColor;
				itemElement.style.cursor          = IE ? 'hand' : 'pointer';
			}
		}
	} else {
		// Dummies for not-supported browsers
		this.add = this.paint = this.popUp = this.popDown = this.keepOpen = new Function();
	}
}

// Item
function Item(title, hyperRef)
{
	// Attributes
	this.title = title;
	this.hyperRef = hyperRef;

	// Paint item
	this.paint = function()
	{
		document.write('<tr><td height=20');
		document.write(' style="border-style:solid; border-width:1px 0 0 0; border-color:#FFFFFF"');
		document.write(' onmouseover="currentMenu.keepOpen(this)" onmouseout="currentMenu.popDown(this)"');
		document.write(' onclick="location.href=\'' + this.hyperRef + '\'">');
		document.write('&nbsp;<a href="' + this.hyperRef + '" class="menuLink">' + this.title + '</a>');
		document.write(' &nbsp; </td></tr>');
	}
}
