(function() {
	var gMenuArray = [], gActiveMenuObj = null, gActiveItemObj = null, gHideFlagOrTimerID = false, gTimerStart = 0;
	function addOffset(coord, obj, prop) {
		while (obj) {
			if (obj[prop]) {
				coord += parseInt(obj[prop], 10);
			}
			obj = obj.offsetParent;
		}
		return coord;
	}
	function setElementPosition(obj, x, y) {
		var style = obj.style;
		style.display = 'block';
		style.position = 'absolute';
		style.left = x + 'px';
		style.top = y + 'px';
		style.zIndex = 1;
	}
	function clearActiveItem() {
		var itemObj = gActiveItemObj;
		if (itemObj) {
			itemObj.style.backgroundColor = itemObj.parentMenu.menuItemBgColor;
			itemObj.style.color = itemObj.parentMenu.color;
			gActiveItemObj = null;
		}
	}
	function hideAllMenus() {
		var i, l, menuObj;
		clearActiveItem();
		for (i = 0, l = gMenuArray.length; i < l; i++) {
			menuObj = document.getElementById(gMenuArray[i].menuId);
			if (menuObj) {
				setElementPosition(menuObj, -1000, -1000);
				menuObj.style.visibility = 'hidden';
			}
		}
		gActiveMenuObj = null;
	}
	function clearHideTimeout() {
		if (gHideFlagOrTimerID !== false) {
			clearTimeout(gHideFlagOrTimerID);
		}
		gHideFlagOrTimerID = false;
	}
	function hideCallback() {
		var menuObj, elapsed, timeout;
		if (gHideFlagOrTimerID !== false) {
			menuObj = gActiveMenuObj;
			if (menuObj) {
				elapsed = new Date().getTime() - gTimerStart;
				timeout = menuObj.parentMenu.hideTimeout;
				if (elapsed < timeout) {
					clearTimeout(gHideFlagOrTimerID);
					gHideFlagOrTimerID = setTimeout(hideCallback, timeout + 100 - elapsed);
					return;
				}
				hideAllMenus();
			}
			clearHideTimeout();
		}
	}
	function startTimeout() {
		var menuObj = gActiveMenuObj;
		if (menuObj) {
			gTimerStart = new Date().getTime();
			gHideFlagOrTimerID = setTimeout(hideCallback, menuObj.parentMenu.hideTimeout);
		}
	}
	function onMenuOut() {
		clearActiveItem();
		startTimeout();
	}
	function onMenuItemOver() {
		clearHideTimeout();
		clearActiveItem();
		if (this.style && this.parentMenu) {
			this.style.backgroundColor = this.parentMenu.menuItemBgColorActive;
			this.style.color = this.parentMenu.colorActive;
			gActiveItemObj = this;
		}
	}
	function setupMenuBehavior(id, menu) {
		var img = document.getElementById(id), imgSrc, overSrc;
		if (img && img.src) {
			imgSrc = img.src;
			overSrc = imgSrc.replace('1.gif', '2.gif');
			(new Image()).src = overSrc;
			img.onmouseout = function() { img.src = imgSrc; startTimeout(); };
			img.onmouseover = function() { img.src = overSrc; menu.showMenu(150, 0, img); };
		}
	}
	function Menu(id, width) {
		this.menuWidth = width;
		this.items = [];
		gMenuArray[gMenuArray.length] = this;
		setupMenuBehavior(id, this);
	}
	function MenuItem(text, location) {
		this.text = text;
		this.location = location;
	}
	Menu.prototype = {
		fontFamily: 'Arial, Helvetica, sans-serif',
		fontSize: 10,
		fontWeight: 'normal',
		color: '#000',
		colorActive: '#FFF',
		menuBorderColor: '#CCC',
		menuBorderColor2: '#FFF',
		menuBgColor: '#555',
		menuItemBgColor: '#FFF',
		menuItemBgColorActive: '#999',
		menuItemHeight: 16,
		menuBorder: 1,
		menuItemBorder: 1,
		menuItemIndent: 4,
		menuItemSpacing: 0,
		hideTimeout: 200
	};
	Menu.prototype.addMenuItem = function(text, location) {
		this.items[this.items.length] = new MenuItem(text, location);
	};
	MenuItem.prototype.buildMarkup = function(itemId) {
		return '<a id="menuItem_' + itemId + '" href="' + this.location + '"><span id="menuItemText_' + itemId + '">' + this.text + '&nbsp</span>\n</a>\n';
	};
	Menu.prototype.buildMarkup = function(iMenu, count) {
		var i, content;
		content = '<div id="menu_' + iMenu + '" style="visibility:hidden;">\n';
		content += '<div id="menuLite_' + iMenu + '">\n';
		content += '<div id="menuFg_' + iMenu + '">\n';
		for (i = 0; i < this.items.length; i++) {
			content += this.items[i].buildMarkup(iMenu + '_' + i);
		}
		content += '</div>\n';
		content += '</div>\n';
		content += '</div>\n';
		return content;
	};
	Menu.prototype.initBgElement = function(obj, bgColor, factor) {
		var style = obj.style;
		style.backgroundColor = bgColor;
		style.height = this.menuHeight + (this.menuBorder * factor) + 'px';
		style.width = this.menuWidth + (this.menuBorder * factor) + 'px';
	};
	Menu.prototype.initProperties = function(iMenu) {
		var i, itemId, obj, style, top = 0, left = 0,
		textTop = (this.menuItemHeight - this.fontSize) / 2 - 1;
		for (i = 0; i < this.items.length; i++) {
			itemId = iMenu + '_' + i;
			obj = document.getElementById('menuItem_' + itemId);
			if (obj) {
				obj.parentMenu = this;
				setElementPosition(obj, left, top);
				obj.onmouseover = onMenuItemOver;
				obj.onclick = hideAllMenus;
				style = obj.style;
				style.width = this.menuWidth + 'px';
				style.height = this.menuItemHeight + 'px';
				style.backgroundColor = this.menuItemBgColor;
				style.color = this.color;
				style.fontFamily = this.fontFamily;
				style.fontSize = this.fontSize + 'px';
				style.fontWeight = this.fontWeight;
				style.textDecoration = 'none';
				top += this.menuItemHeight + this.menuItemBorder + this.menuItemSpacing;
			}
			obj = document.getElementById('menuItemText_' + itemId);
			if (obj) {
				setElementPosition(obj, this.menuItemIndent, textTop);
			}
		}
		this.menuHeight = top - 1 - this.menuItemSpacing;
		this.menuId = 'menu_' + iMenu;
		obj = document.getElementById(this.menuId);
		if (obj) {
			obj.onmouseout = onMenuOut;
			obj.parentMenu = this;
			setElementPosition(obj, -1000, -1000);
			this.initBgElement(obj, this.menuBorderColor, 4);
		}
		obj = document.getElementById('menuLite_' + iMenu);
		if (obj) {
			setElementPosition(obj, this.menuBorder, this.menuBorder);
			this.initBgElement(obj, this.menuBorderColor2, 2);
		}
		obj = document.getElementById('menuFg_' + iMenu);
		if (obj) {
			setElementPosition(obj, this.menuBorder, this.menuBorder);
			this.initBgElement(obj, this.menuBgColor, 1);
		}
	};
	Menu.prototype.showMenu = function(x, y, offsetObj) {
		var menuObj = document.getElementById(this.menuId);
		if (menuObj) {
			clearHideTimeout();
			x = addOffset(x, offsetObj, 'offsetLeft');
			y = addOffset(y, offsetObj, 'offsetTop');
			hideAllMenus();
			gActiveMenuObj = menuObj;
			setElementPosition(menuObj, x, y);
			menuObj.style.visibility = 'visible';
			menuObj.parentMenu.xOffset = document.body.scrollLeft;
			menuObj.parentMenu.yOffset = document.body.scrollTop;
			clearHideTimeout();
		}
	};
	Menu.buildMenus = function() {
		var iMenu = 0, content = '',
		container = document.getElementById('menuContainer');
		if (container) {
			for (iMenu = 0; iMenu < gMenuArray.length; iMenu++) {
				content += gMenuArray[iMenu].buildMarkup(iMenu);
			}
			container.innerHTML = content;
		}
		for (iMenu = 0; iMenu < gMenuArray.length; iMenu++) {
			gMenuArray[iMenu].initProperties(iMenu);
		}
	};
	window.Menu = Menu;
} ());
