/*
 * Description	: Popup menu for Mozilla 1.0	
 */

//what we use when we call a menu: "this" or "event"
webfxMenuCallAsEvent		         = false;

// parent's parametres
webfxMenuDefaultWidth				= 180;                   // menu width
webfxMenuDefaultBorder				= 1;                     // menu border's width
webfxMenuDefaultBorderColor		= "#999999";             // border's color
webfxMenuExecJScriptBeforeShow	= "showSelects(false);"; // script to run before we show menu
webfxMenuExecJScriptAfterHide		= "showSelects(1);";     // script to run after we hide menu
webfxMenuMainTopPositionDelta		= 0;                     // delta for parent from top
webfxMenuMainLeftPositionDelta	= 1;                     // delta for parent from left

// child's parametres
webfxMenuChildTopPositionDelta 	= 8;   
webfxMenuChildLeftPositionDelta	= -13;   
webfxMenuDefaultPaddingRight		= 0;                   // delta for child from left
webfxMenuDefaultPaddingTop			= 0;                    // delta for child from top
webfxMenuDefaultPaddingLeft		= 0;
webfxMenuDefaultPaddingBottom		= 0; 

// item's parametres
webfxMenuItemDefaultHeight			= 0;                    // item's height
webfxMenuItemDefaultText			= "Untitled";
webfxMenuItemDefaultHref			= "javascript:void(0);";
webfxMenuItemPaddingHorizontal	= 5;
webfxMenuItemPaddingVertical		= 3;
webfxMenuItemImageMarginTop		= 5;
webfxMenuItemFontSize				= 9;
webfxMenuItemFontFamily				= "Arial,sans-serif";
webfxMenuItemFontColor				= "#000000";
webfxMenuItemOnMouseOverColor		= "#333333";
webfxMenuItemOnMouseOutColor		= "#000000";
webfxMenuItemOnMouseOutBackground= "#F2F2F2";
webfxMenuItemOnMouseOverBackground= "#BFEBA3";
webfxMenuImageSub					   = "/unet3/img/images1/HM_More_black_right.gif";

// other
webfxMenuSeparatorDefaultHeight		= 0;
webfxMenuDefaultEmptyText			   = "Empty";
webfxMenuHideTime		   			   = 500;
webfxMenuShowTime			   	      = 0;


var webFXMenuHandler = {
	prev_menu		:	"",
	cur_menu		   :	"",
	idCounter		:	0,
	idPrefix		   :	"wfx-mn-obj-",
	all				:	{},
	getId			   :	function () { return this.idPrefix + this.idCounter++; },
	rollMenuItem   :function (el,act){
			if (act=="over"){
					el.style.background=webfxMenuItemOnMouseOverBackground;
					// in case of Mozilla we need to set color for element "a"
					ael = document.getElementById("ah_" + el.id);
					ael.style.color=webfxMenuItemOnMouseOverColor;
                              ael.style.font="bold " + webfxMenuItemFontSize + "pt, " + webfxMenuItemFontFamily;
			}else{
					el.style.background=webfxMenuItemOnMouseOutBackground;			
					// in case of Mozilla we need to set color for element "a"
					ael = document.getElementById("ah_" + el.id);
					ael.style.color=webfxMenuItemOnMouseOutColor;
                              ael.style.font="normal " + webfxMenuItemFontSize + "pt, " + webfxMenuItemFontFamily;
			}
	},
	overMenuItem	:	function (oItem){
		this.rollMenuItem(oItem,"over");
		if (this.showTimeout != null) window.clearTimeout(this.showTimeout);
		if (this.hideTimeout != null) window.clearTimeout(this.hideTimeout);
			
		var jsItem = this.all[oItem.id];
		if (webfxMenuShowTime <= 0)
			this._over(jsItem);
		else{
		   this.showTimeout = window.setTimeout("webFXMenuHandler._over(webFXMenuHandler.all['" + jsItem.id + "'])", webfxMenuShowTime);}
	},
	outMenuItem		:	function (oItem) {
		this.rollMenuItem(oItem,"out");
		if (this.showTimeout != null) window.clearTimeout(this.showTimeout);
		if (this.hideTimeout != null) window.clearTimeout(this.hideTimeout);

		var jsItem = this.all[oItem.id];
		if (webfxMenuHideTime <= 0)
			this._out(jsItem);
		else	
			this.hideTimeout = window.setTimeout("webFXMenuHandler._out(webFXMenuHandler.all['" + jsItem.id + "'])", webfxMenuHideTime);},
    blurMenu		:	function (menu) {
			window.setTimeout("webFXMenuHandler.all[\"" + menu.id + "\"].hide();", webfxMenuHideTime);
	},
	_over			:	function (jsItem) {
		if (jsItem.subMenu) {
			jsItem.parentMenu.hideAllSubs();
			jsItem.subMenu.show();
		}
		else
			jsItem.parentMenu.hideAllSubs();
	},
	_out			:	function (jsItem){
		if(webfxMenuExecJScriptAfterHide!="")eval(webfxMenuExecJScriptAfterHide);
		var m = jsItem.parentMenu; 
		while (m.parentMenu != null){m = m.parentMenu;}
		if (m != null)	
			m.hide();	
	},
	hideMenu	:	function (menu) {
		this.prev_menu = menu;
		if (this.showTimeout != null) window.clearTimeout(this.showTimeout);
		if (this.hideTimeout != null) window.clearTimeout(this.hideTimeout);
		
		var strToExec = "webFXMenuHandler.all['" + menu.id + "'].hide();" 
		if (webfxMenuExecJScriptAfterHide!="")strToExec += webfxMenuExecJScriptAfterHide;
		this.hideTimeout = window.setTimeout(strToExec, webfxMenuHideTime);
	},
	showMenu	:	function (menu, src, dir) {
       this.cur_menu = menu;
	   if (webfxMenuExecJScriptBeforeShow!="")eval(webfxMenuExecJScriptBeforeShow);

		if (this.showTimeout != null) window.clearTimeout(this.showTimeout);
		if (this.hideTimeout != null) window.clearTimeout(this.hideTimeout);

		if (arguments.length < 3) dir = "vertical";

		if ((this.prev_menu != "") && (this.prev_menu != this.cur_menu)){
				this.prev_menu.hide();
				this.cur_menu.show(src, dir);
		}else{
			menu.show(src, dir);
		}
	}
};

function WebFXMenu() {

	this._menuItems   = [];
	this._subMenus	   = [];
	this.id		      = webFXMenuHandler.getId();
	this.top	         = 0;
	this.left	   	= 0;
	this.shown	   	= false;
	this.parentMenu	= null;
	webFXMenuHandler.all[this.id] = this;
}

WebFXMenu.prototype.width			   = webfxMenuDefaultWidth;
WebFXMenu.prototype.emptyText		   = webfxMenuDefaultEmptyText;
WebFXMenu.prototype.paddingLeft		= webfxMenuDefaultPaddingLeft;
WebFXMenu.prototype.paddingRight	   = webfxMenuDefaultPaddingRight;
WebFXMenu.prototype.paddingTop		= webfxMenuDefaultPaddingTop;
WebFXMenu.prototype.paddingBottom	= webfxMenuDefaultPaddingBottom;
WebFXMenu.prototype.border		      = webfxMenuDefaultBorder;
WebFXMenu.prototype.borderLeft		= webfxMenuDefaultBorder;
WebFXMenu.prototype.borderRight		= webfxMenuDefaultBorder;
WebFXMenu.prototype.borderTop		   = webfxMenuDefaultBorder;
WebFXMenu.prototype.borderBottom	   = webfxMenuDefaultBorder;
WebFXMenu.prototype.borderColor		= webfxMenuDefaultBorderColor;

WebFXMenu.prototype.add = function (menuItem) {
	this._menuItems[this._menuItems.length] = menuItem;
	if (menuItem.subMenu) {
		this._subMenus[this._subMenus.length] = menuItem.subMenu;
		menuItem.subMenu.parentMenu = this;	
	}
	menuItem.parentMenu = this;
};

WebFXMenu.prototype.show = function (relObj, sDir) {
	this.position(relObj, sDir);

	var divElement = document.getElementById(this.id);
	divElement.style.left = this.left + "px";
	divElement.style.top =  this.top + "px";
	divElement.style.visibility = "visible";
	if (this.parentMenu){
		this.parentMenu.show();
	}
	this.shown = true;
};

WebFXMenu.prototype.hide = function () {
	this.hideAllSubs();
	var divElement = document.getElementById(this.id);
	divElement.style.visibility = "hidden";
	this.shown = false;
};

WebFXMenu.prototype.hideAllSubs = function () {
	for (var i = 0; i < this._subMenus.length; i++) {
		if (this._subMenus[i].shown)
			this._subMenus[i].hide();
	}
};
// design of main menu window
WebFXMenu.prototype.toString = function () {
	var top = this.top + this.borderTop + this.paddingTop;
   var str = "<div id='" + this.id + "' style='cursor:pointer;position:absolute;z-index:100;visibility:hidden;background:white;padding:0;border:"+ this.border +"px solid "+ this.borderColor +";" + "width:" + this.width + "px;" + "left:" + this.left  + "px;" + "top:" + this.top + "px;" + "'>";
	
	// loop through all menuItems
	for (var i = 0; i < this._menuItems.length; i++) {
		var mi = this._menuItems[i];
		str += mi;
   }

	str += "</div>";
	for (var i = 0; i < this._subMenus.length; i++) {
		this._subMenus[i].left = this.left + this.width - this._subMenus[i].borderLeft;
		str += this._subMenus[i];
	}
	return str;
};

function WebFXMenuItem(sText, sHref, oSubMenu) {
	this.text = sText || webfxMenuItemDefaultText;
	this.href = (sHref == null || sHref == "") ? webfxMenuItemDefaultHref : sHref;
	this.subMenu = oSubMenu;
	if (oSubMenu)
		oSubMenu.parentMenuItem = this;
	this.id = webFXMenuHandler.getId();
	webFXMenuHandler.all[this.id] = this;
};

// design of item
WebFXMenuItem.prototype.toString = function (){
	return "<div onmouseover ='webFXMenuHandler.overMenuItem(this)' onmouseout='webFXMenuHandler.outMenuItem(this)' id='"+ this.id +"'  style='padding-left:"+ webfxMenuItemPaddingHorizontal +"px;padding-right:"+ webfxMenuItemPaddingHorizontal +"px;padding-top:"+ webfxMenuItemPaddingVertical +"px;padding-bottom:"+ webfxMenuItemPaddingVertical +"px;'><a style='color:"+ webfxMenuItemFontColor+";font-weight:normal;cursor:pointer;border:none;font-size:"+ webfxMenuItemFontSize +"pt;font-family:"+  webfxMenuItemFontFamily+";text-decoration:none;vertical-align:center;text-align:left;border:none;'" + " id='ah_" + this.id + "'" +	" href=\"" + this.href + "\"" + (this.subMenu ? " unselectable='on' tabindex='-1'" : "") + ">" + (this.subMenu ? "<img style='float:right;border:0;margin-top:"+ webfxMenuItemImageMarginTop +"px;' src='" + webfxMenuImageSub +"'>" : "") + this.text + "</a></div>";
};

function WebFXMenuSeparator() {
	this.id = webFXMenuHandler.getId();
	webFXMenuHandler.all[this.id] = this;
};

WebFXMenuSeparator.prototype.height = webfxMenuSeparatorDefaultHeight;
WebFXMenuSeparator.prototype.toString = function () {
	return	"<div" + " id='" + this.id + "'" + " onmouseover='webFXMenuHandler.overMenuItem(this)'" + " onmouseout='webFXMenuHandler.outMenuItem(this)'" + "></div>";
};


/* Position functions */

function getInnerLeft(el) {
	
	if (el == null) return 0;
	if (el == document.documentElement) return 0;
	return getLeft(el) + getBorderLeft(el);
}

function getLeft(el) {
	if (el == null) return 0;
	
	var xParent = getInnerLeft(el.offsetParent);
	var xCurrent = el.offsetLeft;
	if (xParent + "" == "NaN") xParent =  0;
	if (xCurrent + "" == "NaN") xCurrent = 0;

	return xParent + xCurrent;
}

function getInnerTop(el) {
	if (el == null) return 0;
	if (el == document.documentElement) return 0;
	return getTop(el) + getBorderTop(el);
}

function getTop(el) {
	if (el == null) return 0;
   return el.offsetTop + getInnerTop(el.offsetParent);
}

function getBorderLeft(el) {
	if(document.defaultView)
	    return 0;
	else    
		return parseInt(document.defaultView.getComputedStyle(el, null).getPropertyValue("border-left-width"));
}

function getBorderTop(el) {
	if(document.defaultView)
	    return 0;
	else    
		return parseInt(document.defaultView.getComputedStyle(el, null).getPropertyValue("border-top-width"));
}

function getOuterRect(el) {
	return {
		left:   getLeft(el),
		top:    getTop(el),
		width:  el.offsetWidth,
		height: el.offsetHeight
	};
}

// mozilla bug! scrollbars not included in innerWidth/height
function getDocumentRect(el) {
	return {
		left:	0,
		top:	0,
		width:	window.innerWidth - 20,
		height:	window.innerHeight -20
	};
}

function getScrollPos(el) {
	return {
		left: window.pageXOffset,
		top:  window.pageYOffset
	};
}
/* end position functions */

WebFXMenu.prototype.position = function (relEl, sDir) {
   var dir = sDir;
	var piRect;

	// if child element
	if (!relEl) {
		var pi = this.parentMenuItem;
		if (!this.parentMenuItem)return;
		relEl = document.getElementById(pi.id);
		if (dir == null) dir = "horizontal";
		piRect = getOuterRect(relEl);
		}else if (relEl.left != null && relEl.top != null && relEl.width != null && relEl.height != null) {	

		 piRect = relEl;
	// if parent
	}else{
      if (webfxMenuCallAsEvent) piRect = getOuterRect_Event(relEl);
      else piRect = getOuterRect(relEl);
	}

	var menuEl = document.getElementById(this.id);
	var menuRect = getOuterRect(menuEl);
	var docRect = getDocumentRect();
	var scrollPos = getScrollPos();
	var pMenu = this.parentMenu;

	if (dir == "vertical") {
			
         // parent
         if (piRect.left + menuRect.width - scrollPos.left <= docRect.width)
				this.left = piRect.left + webfxMenuMainLeftPositionDelta;
			else if (docRect.width >= menuRect.width) 
				this.left = docRect.width + scrollPos.left - menuRect.width;
			else 
				this.left = scrollPos.left;

			if (piRect.top + piRect.height + menuRect.height <= docRect.height + scrollPos.top)
				this.top = piRect.top + piRect.height + webfxMenuMainTopPositionDelta;
			else if (piRect.top - menuRect.height >= scrollPos.top)
				this.top = piRect.top - menuRect.height;
			else if (docRect.height >= menuRect.height)
				this.top = docRect.height + scrollPos.top - menuRect.height;
			else
				this.top = scrollPos.top;
	}else{
		
			// child
			if (piRect.top + menuRect.height <= docRect.height + scrollPos.top){
				this.top = piRect.top + webfxMenuChildTopPositionDelta;
			}else if (piRect.top + piRect.height - menuRect.height >= 0){
				this.top = piRect.top + piRect.height - menuRect.height;
			}else if (docRect.height >= menuRect.height){
				this.top = docRect.height + scrollPos.top - menuRect.height;
			}else{
				this.top = scrollPos.top;}

			if (piRect.left + piRect.width + menuRect.width <= docRect.width + scrollPos.left){
				this.left = piRect.left + menuRect.width + webfxMenuChildLeftPositionDelta;
			}else if (piRect.left - menuRect.width >= 0){
				this.left = piRect.left - menuRect.width;
			}else if (docRect.width >= menuRect.width){
				this.left = docRect.width  + scrollPos.left - menuRect.width;
			}else{
				this.left = scrollPos.left;}
	}
};

