// menus in XP style
// uses: objutl.js, lists.js

/* Global vars:
	_mnu_thide		// handle of timeout to hide
	_mnu_tsh		// handle of timeout to show
	_mnu_topVis		// leaf inside visible (to hide on timeout or other-show)
	_mnu_ssh		// submenu to show on timeout
	_mnu_sto
	_mnu_hto		// corresponding timeout constants
*/
var _mnu_thide, _mnu_tsh, _mnu_topVis, _mnu_ssh;
var _mnu_sto = 200, _mnu_hto = 500;

/* Submenu object (MNUPopup) attributes:
	_pitm	// parent item (may be null for stand-alone ctxt menu)
			// topmost div element is at _pbox attr of list
	_vis	// current visibility
	_msin	// if mouse is currently inside popup
	fxx		// do not adjust x-coordinate to fit into screen
	fxy		// do not adjust y-coordinate to fit into screen
	
	_pbox._mnu		// this
*/

/* Menu item (MNUItem) attributes:
	tit		// item text
	iind	// index into _fmt._mIL (default: 0 - no image, shown images start at 1)
	stl		// style name prefix of item box, used with 'hl', 'dn', 'sv' extensions
			// inherits _fmt._stlMI
	sub		// submenu MNUPopup if exists, uses _fmt._msubI as marker and (!) _fmt._msubNI as NO-marker
	
	For vertical or horizontal menu
	Horizontal menu uses _fmt._voffs to offset submenu vertically
*/

// static image separator
DECL_CLASS("MNUSepItem");
function MNUSepItem() { this._baseinit(); }
MNUSepItem.Init = function (url, stl) {
	this._baseinit(); this.stl = stl; this.url=url;
}
D_CONSTRUCTOR(MNUSepItem, MNUSepItem.Init)
MNUSepItem.prototype._Lay = function () {
	var c;
	if (this.url.length>2) {
		c = document.createElement("IMG");
		c.src = this.url;
	}
	else {
		c = document.createElement("SPAN");
		c.appendChild(document.createTextNode(this.url));
	}
	c.className = this.stl?this.stl:"";
	ouf_clear(this._pbox);
	this._pbox.appendChild(c);
}

DECL_CLASS("MNUItem", LSTmtItem);
function MNUItem() { this._baseinit(); }
MNUItem.Init = function (tit, iind, stl, sub) {
	this._baseinit();
	this.tit=tit; this.iind=iind; this.stl=stl; this.sub=sub;
	if (sub) sub._pitm=this;
}
D_CONSTRUCTOR(MNUItem, MNUItem.Init);
MNUItem.prototype._Lay = function () {
	var o=this, s, r, c, p;
	if (o._lst.horl) {
		s = document.createElement("SPAN");
		// s.style.height = "1px";
		if (o.iind) {
			p = document.createElement("IMG");
			p.src = _fmt._mIL[o.iind];
			s.appendChild(p);
		}
		s.appendChild(document.createTextNode(o.tit));
		if (o.sub) o.sub.fxy=1;
	}
	else {
		s = document.createElement("TABLE");
		s.cellSpacing = s.cellPadding = s.border = 0;
		s.width = "100%";
		r = s.insertRow(0);
		if (o.iind) {
			c = r.insertCell(r.cells.length);
			c.vAlign = "top";
			c.width = 1;
			p = document.createElement("IMG");
			p.src = _fmt._mIL[o.iind];
			c.appendChild(p);
		}
		c = r.insertCell(r.cells.length);
		c.appendChild(document.createTextNode(o.tit));
		c = r.insertCell(r.cells.length);
		c.vAlign = "top";
		c.width = 16;
		p = document.createElement("IMG");
		p.src = o.sub?o._fmt._msubI:o._fmt._msubNI;
		c.appendChild(p);
		if (o.sub) o.sub.fxx=1;
	}
	ouf_clear(o._pbox);
	o._pbox.appendChild(s);
	o._SetOvrBox(s);
	o._UpdSt();
}
MNUItem.prototype._UpdSt = function (dn, ovr) {
	var o=this, t = o._pbox.childNodes[0], ns;
	if (o.sub) {
		ns = (o.stl ? o.stl : o._fmt._stlMI) + (ovr?'hl':(o.sub._vis?'sv':''));
		if (ovr && !o._ovr) {
			if (!o.sub._vis) {
				// setting timeout to show
				_mnu_ssh = o.sub;
				_mnu_tsh = setTimeout("_mnu_show()",_mnu_sto);
			}
		}
		else if (o._ovr && !ovr) {
			// resetting timeout to show
			if (o.sub == _mnu_ssh) { clearTimeout(_mnu_tsh); _mnu_ssh=_mnu_tsh=0; }
			// setting timeout to hide child
			if (o.sub == _mnu_topVis && !_mnu_thide) _mnu_thide = setTimeout("_mnu_hide()",_mnu_hto);
		}
	}
	else ns = (o.stl ? o.stl : o._fmt._stlMI) + (dn?'dn':(ovr?'hl':''));
	if (t.className != ns) t.className = ns;
}
MNUItem.prototype._AlignSub = function () {
	var p=ouf_topos(this._pbox);
	if (this._lst.horl) {
		p.x -= 3;
		p.y += (this._fmt._voffs?this._fmt._voffs:0) + this._pbox.offsetHeight;
	}
	else {
		p.x += this._pbox.offsetWidth - 3;
		p.y -= 3;
	}
	return p;
}

DECL_CLASS("MNUPopup", LSTStream);
function MNUPopup() { this._baseinit(); }
MNUPopup.Build = function (stl, w) {
	var o=this, b=document.body; o._baseinit(); o._sls=o; o._pl=1;
	o._pbox = document.createElement("DIV");
	o._pbox.className = stl;
	ouf_setevt(o._pbox, "mouseover", new Function("e", "e.currentTarget._mnu._MsOvr(e)"));
	ouf_setevt(o._pbox, "mouseout", new Function("e", "e.currentTarget._mnu._MsOut(e)"));
	o._pbox._mnu=o;
	if (w) o._pbox.style.width=w+"px";
	b.insertBefore(o._pbox, b.childNodes.length?b.childNodes[0]:null);
	o._Lay();
}
D_CONSTRUCTOR(MNUPopup, MNUPopup.Build);
MNUPopup.prototype._MsOvr = function (e) {
	this._msin=1;
}
MNUPopup.prototype._MsOut = function (e) {
	if (!ouf_contains(this._pbox, e.relatedTarget) && this._pbox != e.relatedTarget) {
		this._msin = 0;
		if (this == _mnu_topVis) {
			// setting timeout to hide
			if (_mnu_thide) clearTimeout(_mnu_thide);
			_mnu_thide = setTimeout("_mnu_hide()",_mnu_hto);
		}
	}
}
MNUPopup.prototype.Show = function (x,y) {
	if (_mnu_tsh) { clearTimeout(_mnu_tsh); _mnu_ssh=_mnu_tsh=0; }
	_mnu_ssh=this; _mnu_show(x, y);
	_mnu_thide = setTimeout("_mnu_hide()",_mnu_hto*3);
}

// global functions
function _mnu_show(x, y) {
	// hiding immediately
	if (_mnu_thide) { clearTimeout(_mnu_thide); _mnu_hide(); }
	// appointed submenu
	var s=_mnu_ssh, i = s._pitm, ss = s._pbox.style, p, b=document.body, oz=ss.zIndex;
	if (i) p=i._AlignSub(), x=p.x, y=p.y;
	// fit to screen
	ss.zIndex=-1;
	ss.display="block";
	if (!s.fxy) {
		if (y+s._pbox.offsetHeight > b.clientHeight+b.scrollTop) y=b.clientHeight+b.scrollTop-s._pbox.offsetHeight;
		if (y < b.scrollTop) y = b.scrollTop;
	}
	if (!s.fxx) {
		if (x+s._pbox.offsetWidth > b.clientWidth+b.scrollLeft)  x=b.clientWidth+b.scrollLeft-s._pbox.offsetWidth;
		if (x < b.scrollLeft) x = b.scrollLeft;
	}
	// showing
	ss.left = x + "px";
	ss.top = y + "px";
	ss.zIndex=oz;
	s._vis=1;
	// just shown popup is actually youngest
	_mnu_topVis = s;
	_mnu_tsh=_mnu_ssh=0;
}
function _mnu_hide() {
	_mnu_thide=0;
	var s = _mnu_topVis;
	while (s) {
		// checking visibility conditions for popup
		if (s._msin || (s._pitm && s._pitm._ovr)) break;
		// hiding
		s._pbox.style.display="none";
		s._vis=0;
		if (s._pitm) s._pitm._UpdSt(s._pitm._dn, s._pitm._ovr);
		// to parent
		if (s._pitm && s._pitm._lst._isOfType("MNUPopup")) s = s._pitm._lst;
		else s=0;
	}
	_mnu_topVis = s;
}
