// define the default values
manMenuDefaultWidth			= 130;
manMenuDefaultBorderWidth		= 2;
manMenuDefaultPaddingWidth	= 2;
manMenuDefaultBorderTop		= 1;
manMenuDefaultPaddingTop		= 1;

manMenuItemDefaultHeight		= 20;
manMenuItemDefaultText		= "Untitled";
manMenuItemDefaultHref		= "javascript:void(0)";

manMenuSeparatorDefaultHeight	= 6;

manMenuDefaultImagePath		= "";
manMenuDefaultEmptyText		= "Empty";

// check browsers
var op = /opera 5|opera\/5/i.test(navigator.userAgent);
var ie = !op && /msie/i.test(navigator.userAgent);	// preventing opera to be identified as ie
var mz = !op && /mozilla\/5/i.test(navigator.userAgent);	// preventing opera to be identified as mz
var ieBox = ie && /win/.test(navigator.platform) && (document.compatMode == null || document.compatMode != "BackCompat");	// IE proprietary box model

if (ie && document.getElementById == null) {	// ie4
	document.getElementById = function(sId) {
		return document.all[sId];
	};
}

var ManMenuHandler = {
	idCounter		:	0,
	idPrefix		:	"man-menu-object-",
	getId			:	function () { return this.idPrefix + this.idCounter++; },
	overMenuItem	:	function (oItem) {
		var jsItem = this.all[oItem.id];
		if (jsItem.subMenu) {
			jsItem.parentMenu.hideAllSubs();
			jsItem.subMenu.show();
		}
		else
			jsItem.parentMenu.hideAllSubs();
	},
	blurMenu		:	function (oMenuItem) {
		window.setTimeout("ManMenuHandler.all[\"" + oMenuItem.id + "\"].subMenu.hide();", 200);
	},
	all				:	{}
};

function MANMenu() {
	this._menuItems	= [];
	this._subMenus	= [];
	this.id			= ManMenuHandler.getId();
	this.top		= 0;
	this.left		= 0;
	this.shown		= false;
	ManMenuHandler.all[this.id] = this;
}
MANMenu.prototype.width			= manMenuDefaultWidth;
MANMenu.prototype.borderWidth		= manMenuDefaultBorderWidth;
MANMenu.prototype.paddingWidth	= manMenuDefaultPaddingWidth;
MANMenu.prototype.borderTop		= manMenuDefaultBorderTop;
MANMenu.prototype.paddingTop		= manMenuDefaultPaddingTop;
MANMenu.prototype.emptyText		= manMenuDefaultEmptyText;

MANMenu.prototype.add = function (menuItem) {
	this._menuItems[this._menuItems.length] = menuItem;
	if (menuItem.subMenu)
		this._subMenus[this._subMenus.length] = menuItem.subMenu;
	
	menuItem.parentMenu = this;
};
MANMenu.prototype.show = function () {
	var divElement = document.getElementById(this.id);
	divElement.style.left = op ? this.left : this.left + "px";
	divElement.style.top = op ? this.top : this.top + "px";
	divElement.style.visibility = "visible";
	this.shown = true;
	if (this.parentMenu)
		this.parentMenu.show();
};
MANMenu.prototype.hide = function () {
	this.hideAllSubs();
	var divElement = document.getElementById(this.id);
	divElement.style.visibility = "hidden";
	this.shown = false;
};
MANMenu.prototype.hideAllSubs = function () {
	for (var i = 0; i < this._subMenus.length; i++) {
		if (this._subMenus[i].shown)
			this._subMenus[i].hide();
	}
};
MANMenu.prototype.toString = function () {
	var top = this.top + this.borderTop + this.paddingTop;
	var str = "<div id='" + this.id + "' class='man-menu' style='" + 
	"width:" + (!ieBox  ?  this.width - this.borderWidth - this.paddingWidth  :  this.width) + "px;" +
	"left:" + this.left + "3px;" +
	"top:" + this.top + "3px;" +
	"'>";

	
	if (this._menuItems.length == 0) {
		str += "<span class='man-menu-empty'>" + this.emptyText + "</span>";
	}
	else {	
		// loop through all menuItems
		for (var i = 0; i < this._menuItems.length; i++) {
			var mi = this._menuItems[i]
			str += mi;
			if (mi.subMenu)
				mi.subMenu.top = top - mi.subMenu.borderTop - mi.subMenu.paddingTop;
			top += mi.height;
		}

	}
	
	str += "</div>";

	for (var i = 0; i < this._subMenus.length; i++) {
		this._subMenus[i].left = this.left + this.width - this._subMenus[i].borderWidth/2;
		str += this._subMenus[i];
	}
	
	return str;
};
function MANMenuItem(sText, sHref, sToolTip, oSubMenu) {
	
	this.text = sText || manMenuItemDefaultText;
	this.href = (sHref == null || sHref == "") ? manMenuItemDefaultHref : sHref;
	this.subMenu = oSubMenu;
	if (oSubMenu)
		oSubMenu.parentMenuItem = this;
	this.toolTip = sToolTip;
	this.id = ManMenuHandler.getId();
	ManMenuHandler.all[this.id] = this;
};
MANMenuItem.prototype.height = manMenuItemDefaultHeight;
MANMenuItem.prototype.toString = function () {
	return	"<a" +
			" id='" + this.id + "'" +
			" href='" + this.href + "'" +
			(this.toolTip ? " title='" + this.toolTip + "'" : "") +
			" onmouseover='ManMenuHandler.overMenuItem(this)'" +
			" >" +
			(this.subMenu ? "<img class='arrow' src='" + manMenuDefaultImagePath + "image/arrow.right.gif'>" : "") +
			this.text + 
			"</a>";
};


function MANMenuSeparator() {
	this.id = ManMenuHandler.getId();
	ManMenuHandler.all[this.id] = this;
};
MANMenuSeparator.prototype.height = manMenuSeparatorDefaultHeight;
MANMenuSeparator.prototype.toString = function () {
	return "<div></div>"
};

function MANMenuBar() {
	this._parentConstructor = MANMenu;
	this._parentConstructor();
}
MANMenuBar.prototype = new MANMenu;
MANMenuBar.prototype.toString = function () {
	var str = "<div id='" + this.id + "' class='man-menu-bar'>";
	
	// loop through all menuButtons
	for (var i = 0; i < this._menuItems.length; i++)
		str += this._menuItems[i];
	
	str += "</div>";

	for (var i = 0; i < this._subMenus.length; i++)
		str += this._subMenus[i];
	
	return str;
};

function MANMenuButton(sText, sHref, sToolTip, oSubMenu) {
	this._parentConstructor = MANMenuItem;
	this._parentConstructor(sText, sHref, sToolTip, oSubMenu);
}
MANMenuButton.prototype = new MANMenuItem;
MANMenuButton.prototype.toString = function () {
	return	"<a" +
			" id='" + this.id + "'" +
			" href='" + this.href + "'" +
			(this.toolTip ? " title='" + this.toolTip + "'" : "") +
			(op ? (
				" onoperafocus='ManMenuHandler.overMenuItem(this)'" +
				(this.subMenu ? " onoperablur='ManMenuHandler.blurMenu(this)'" : "")
			) : (
				" onfocus='ManMenuHandler.overMenuItem(this)'" +
				(this.subMenu ? " onblur='ManMenuHandler.blurMenu(this)'" : "")
			)) +
			">" +
			this.text + 
			(this.subMenu ? " <img class='arrow' src='" + manMenuDefaultImagePath + "image/arrow.down.gif' align='absmiddle'>" : "") +				
			"</a>";
};

/* add onfocus/blur for anchors in opera
 * Opera 5.10 seems to support focus and blur but in reality it is even worse
 */
if (op) {
	document.onmousedown = function(e) {
		var a = e.target;
		while (a != null && a.tagName != "A") a = a.parentNode;

		if (document._oldFocus && document._oldFocus != a) {
			if (typeof document._oldFocus.onoperablur == "string") {
				var f = new Function ("event", document._oldFocus.onoperablur);
				document._oldFocus.onFakeBlur = f;
			}
			if (typeof document._oldFocus.onFakeBlur == "function") 
				document._oldFocus.onFakeBlur(e);
		}

		if (a && a != document._oldFocus) {
			document._oldFocus = a;
			if (typeof a.onoperafocus == "string") {
				var f = new Function ("event", a.onoperafocus);
				a.onFakeFocus = f;
			}
			if (typeof a.onFakeFocus == "function") 
				a.onFakeFocus(e);
		}
		else
			document._oldFocus = null;
	};
}
