function Topmenu() {
	this.items = new Array();
}

Topmenu.prototype.hideTimout = 60 * 1000; // 60 seconds = 1 minute

Topmenu.prototype.add = function(item, menu) {
	var item = document.getElementById(item);
	var menu = document.getElementById(menu);
	if (!item || !menu) return;
	item._topmenu = {item:item,menu:menu};
	this.items.push(item._topmenu);

	item._offsetX = function() {
		var o = this, x = 0;
		if (o.offsetParent) {
			while (o.offsetParent) {
				x += o.offsetLeft;
				o = o.offsetParent;
			}
		} else if (o.x) {
			x = o.x;
		}
		return x;
	}

	item._offsetY = function() {
		var o = this, y = 0;
		if (o.offsetParent) {
			while (o.offsetParent) {
				y += o.offsetTop;
				o = o.offsetParent;
			}
		} else if (o.y) {
			y = o.y;
		}
		return y;
	}

	var self = this;
	item.onclick = function() {
		if (!item._show) self.show(item.id);
	}
	item.onfocus = function() {
		if (!item._show) self.show(item.id);
	}
}

Topmenu.prototype.show = function(item) {
	if (this._hideTimeout) clearTimeout(this._hideTimeout);

	var item = document.getElementById(item);
	if (!item) return;
	if (!item._topmenu) return;
	if (!item._topmenu.menu) return;

	this.hide();

	item._show = true;
	item.className = 'active';
	item._topmenu.menu.style.visibility = 'hidden';
	item._topmenu.menu.style.display = 'block';
	item._topmenu.menu.style.left = item._offsetX() + 'px';
	item._topmenu.menu.style.top = (item._offsetY() + item.offsetHeight) + 'px';
	setTimeout(function() {
		item._topmenu.menu.style.visibility = 'visible';
	}, 100);

	var self = this;
	this._hideTimout = setTimeout(function() {
		self.hide();
	}, this.hideTimout);
	var body = document.getElementsByTagName('body');
	if (body.length > 0) body[0].onmouseup = function() {
		self.hide();
		return true;
	}
}

Topmenu.prototype.hide = function() {
	if (this._hideTimeout) clearTimeout(this._hideTimeout);

	var body = document.getElementsByTagName('body');
	if (body.length > 0) body[0].onmousedown = null;

	for (var i = 0; i < this.items.length; i++) {
		if (this.items[i].item && this.items[i].menu) {
			this.items[i].item._show = false;
			this.items[i].item.className = 'inactive';
			this.items[i].menu.style.display = 'none';
		}
	}
}