// A menu is one list of items (without submenus)
// Each menu has an id (1, 1_1, etc.), an obj with info about the menu
// a dhtml div/layer with the contents, and an array with the items
// Contents of menu obj:
//  n - id
//  data - array with items
//  dataofs - start ofs in array
//  id - id of div
//  div - ref to div
// NS only:
//  ls - ref to layers for each item

var mWin; // frame with menus
var mOMo; // old gif menubar
var mOMoN; // old menubar img name
function Empty(){}
var menus = new Empty(); // map with menu objs
var mTop = new Empty(); // menu obj for top
var mCodeWinID = name + "mCodeWin"; // name prop in menuframe with codeframe
var mTimer; // id hide timer
var mInTop; // 1=last m.o. evt of top (workaround bug ie 4/5.0)

var mIE = document.all?1:0;
var mNS4 = document.layers?1:0;
var mGecko = navigator.userAgent.indexOf("Gecko")!=-1;
var mDOM = document.getElementById;
var mMac = navigator.appVersion.indexOf("Mac")!=-1;

var mID = name;

var mImg, mImg2;
function mLdImg() {
	if(document.images) {
		mImg = new Image(mSubImgW, mSubImgH); mImg.src = mSubImg;
		if (window.mSubImgMO) { mImg2 = new Image(mSubImgW, mSubImgH); mImg2.src = window.mSubImgMO; }
	}
}

// Get div with given id in menu frame
function mElGet(id) {
	var d = mWin.document;
	if (d.getElementById) return d.getElementById(id);
	if (d.all) return d.all[id];
	if (d.layers) return d.layers[id];
}

// Make div d visible if s else hide div
function mElShow(d, s) {
	if (mIE || mDOM) d.style.visibility = s ? "visible" : "hidden";
	if (mNS4) d.visibility = s ? "show" : "hide";
}

// Move div d to x,y
function mElMove(d, x, y) {
	if (mIE) { d.style.pixelLeft = x; d.style.pixelTop = y; }
	else if (mNS4) d.moveTo(x, y);
	else if (mDOM) { d.style.left = x + "px"; d.style.top = y + "px"; }
}

// Get width of div d
function mElWidth(d) {
	if (mIE || mGecko) return d.offsetWidth;
	if (mNS4) return d.clip.width;
}

// Get left of div d
function mElLeft(d) {
	if (mIE || mGecko) return d.offsetLeft;
	if (mNS4) return d.clip.left;
}

// Get top of div d
function mElTop(d) {
	if (mIE || mGecko) return d.offsetTop;
	if (mNS4) return d.clip.top;
}

// Get height of div d
function mElHeight(d) {
	if (mIE || mGecko) return d.offsetHeight;
	if (mNS4) return d.clip.height;
}

// Get menu frame width
function mWinWidth() {
	if (mIE) return mWin.document.body.clientWidth;
	if (mNS4 || mGecko) return mWin.innerWidth;
}

// Get menu frame height
function mWinHeight() {
	if (mIE) return mWin.document.body.clientHeight;
	if (mNS4 || mGecko) return mWin.innerHeight;
}

// Get menu frame top scroll
function mScrollOfs() {
	if (mIE) return mWin.document.body.scrollTop;
	if (mNS4 || mGecko) return mWin.pageYOffset;
}

function mSetWin() {
	if (window.mFWinStr) mWinStr = mFWinStr();
	if (!mWinStr) return 1;
	mWin = eval(mWinStr);
	var rs = mWin.document.readyState;
	if (rs && rs != "complete") return 1;
	return 0;
}

function mNTop(n) { var i = n.indexOf("_"); return i < 0 ? n : n.substring(0, i); }

// Get menu obj for menu id n. Generates the obj/div if necessary.
function mGet(n) {
	mSetWin();
	n = new String(n);
	var nt = mNTop(n)-1;
	var m = menus[n];
	if (m && mElGet(m.id)) return m; // menu obj and div exists?
	menus[n] = m = new Empty();
	m.n = n;
	m.dataofs = n.indexOf("_") == -1 ? 9 : 0;
	var d = m.data = window["arMenu" + n]; // get the array with items
	if (!d) return null;
	var imgr = window.mSubImgR;
	if (!imgr)
		imgr = mSubImgW * 2;
	imgr += mSubImgW;
	var preimgw = mPopWidth - imgr;
	if (mIE || mDOM) {
		mWin[mCodeWinID] = window;
		m.id = mID + 'mpop' + n;
		/*
		var h = '<table  bgcolor="eeff77" class="' + mID + 'mpop" cellpadding=0 cellspacing=0 border=0>';
		for (var i = m.dataofs, j = 0; i < d.length; i += 3, j++) {
			h += '<tr onmouseover="'+mCodeWinID+'.mPopIn(this,\''+n+'\','+j+')" onmouseout="'+mCodeWinID+'.mPopOut(this)"><td><a class="' + mID + 'mpop" href="' + d[i + 1] + '">' + d[i];
			h += '</a></td>'; 
			if (d[i + 2]) h += '<td><img src="' + mSubImg + '" border=0 width="' + mSubImgW + '" height="' + mSubImgH + '">';  else h += '<td>&nbsp;'
			h += '</td></tr>';
		}
		h += '</table>';
		*/
		var e = mElGet(m.id);
		if (!e) {
			var style = window.mStyles;
			style = style && style[nt] ? style[nt] : mID + 'mpop';
			var h = '';
			for (var i = m.dataofs, j = 0; i < d.length; i += 3, j++) {
				h += (window.mV2 ? '<div' + (mIE ? ' style="width:100%"' : '') : '<span') + ' class="' + style + (window.mV2 ? 'i' : '')
				if (d[i + 1]) h += '" onclick="'+mCodeWinID+'.mLink(0,\''+n+'\','+i+')'
				h += '" onmouseover="'+mCodeWinID+'.mPopIn(this,\''+n+'\','+j+')" onmouseout="'+mCodeWinID+'.mPopOut(this)">'
				if (mIE)
					h += '<span style="width:' + preimgw + 'px">';
				if (d[i + 2] && !mIE)
					h += '<img vspace="2" align="right" src="' + mSubImg + '" border=0 width="' + mSubImgW + '" height="' + mSubImgH + '" name="mi' + mID + n + '_' + (j + 1) + '">';
				if (d[i + 1]) {
					h += '<a class="' + style + '" href="' + d[i + 1] + '"'
					if (window.mTarget) h += ' target="' + mTarget + '"';
					h += '>' + d[i] + '</a>';
				} else
					h += d[i];
				if (mIE) {
					h += '</span>';
					if (d[i + 2] && mIE)
						h += '<img src="' + mSubImg + '" border=0 width="' + mSubImgW + '" height="' + mSubImgH + '" name="mi' + mID + n + '_' + (j + 1) + '">';
				}
				h += window.mV2 ? '</div>' : '</span><br>';
			}
			var divstyle = window.mV2 ? style + 'm' : style;
			if (document.getElementById) { // ie 5 mac fix
				e = mWin.document.createElement("DIV");
				e.className = divstyle;
				//e.style.position = "absolute";
				//e.style.backgroundColor = mPopBG;
				e.style.width = mPopWidth + "px";
				//e.style.position = "absolute"; // NS6
				//e.style.visibility = "hidden"; // NS6
				//e.style.zIndex = 999;
				e.id = m.id;
				e.innerHTML = h;
				mWin.document.body.appendChild(e);
				e.style.position = "absolute"; // NS6
				e.style.visibility = "hidden"; // NS6
			} else {
				h = '<div id="' + m.id + '" class="' + divstyle + '">' + h + '</div>'
				mWin.document.body.insertAdjacentHTML("beforeEnd", h);
				e = mWin.document.all[m.id];
				e.style.width = mPopWidth + "px";
			}
		}
		m.div = e;
		//m.div.style.width = m.div.all[0].offsetWidth; //ie 4 extend fix
	} else if (mNS4) {
		var style = window.mStylesNS;
		style = style && style[nt] ? style[nt] : mID + 'mpop' + (window.mV2 ? '' : 'ns');
		var l = m.div = new Layer(mPopWidth, mWin);
		m.id = l.name;
		var bgs = window.mPopBGs;
		l.bgColor = bgs && bgs[nt] ? bgs[nt] : mPopBG;
		l.clip.width = mPopWidth;
		m.ls = new Array((d.length - m.dataofs) / 3);
		if (window.mOverSSNS) {
			var o = m.lmo = new Layer(preimgw, l);
			o.clip.width = preimgw;
			o.zIndex = 1;
			o.captureEvents(Event.MOUSEUP)
			o.onmouseup = mLink;
			o.onmouseover = mPopIn;
			o.onmouseout = mPopOut;
		}
		var j = 0;
		var y = 0;
		var istyle = style + (window.mV2 ? 'ins' : '');
		for (var i = m.dataofs; i < d.length; i += 3, j++) {
			var ll = m.ls[j] = new Layer(mPopWidth, l);
			ll.document.write('<div class="' + istyle +
				'"><a href="' + (d[i + 1] ? d[i + 1] : '#') + '" class="' + style + (window.mV2 ? 'ns' : '') + '">' + d[i] + '</a></div>');
//alert('<div class="' + istyle +
//				'"><a href="' + (d[i + 1] ? d[i + 1] : '#') + '" class="' + style + (window.mV2 ? 'ns' : '') + '">' + d[i] + '</a></div>');
			ll.document.close();
			ll.clip.width = mPopWidth;
			ll.visibility = "inherit";
			ll.moveTo(0, y);
			y += ll.clip.height;
			ll.onmouseover=mPopIn;
			ll.onmouseout=mPopOut;
			ll.m = m;
			ll.i = j;
			ll.captureEvents(Event.MOUSEUP)
			ll.cap = d[i];
			ll.link = d[i + 1];
			ll.onmouseup = mLink;

			if (d[i + 2]) {
				var il = new Layer(imgr, ll); 
				il.document.write('<img src="' + mSubImg + '" border=0 width="' + mSubImgW + '" height="' + mSubImgH + '" name="mi' + mID + n + '_' + (j + 1) + '">');
				il.document.close();
				//il.left = ll.clip.width - il.clip.width;
				il.left = preimgw;
				il.visibility = "inherit";
				ll.il = il;
			}
		}
		l.clip.height = y;
	}
	return m;
}

// Hide menu m
function mHide(m) {
	if (m.open)
		mHide(m.open);
	mHideSel(m);
	mElShow(m.div, 0);
}

// Hide opened submenu of m
function mHideSub(p) {
	if (p.open) {
		mHide(p.open)
		p.open = null;
	}
}

// Set menubar image
function mSetBarImg(url, mon) { 
	var d = document;
	if (d.layers && mImgLyr) d = d.layers[mImgLyr].document;
	if (!mon) mon = "menubar";
	var i = d.images[mon];
	if (i) {
		var o = i.src;
		i.src = url;
		return o;
	}
}

// Hide top menu
function mHideTop() {
	mHideSub(mTop);
	if (mOMo) {
		//setTimeout('mSetBarImg("' + mOMo + '","' + mOMoN + '")', 1);
		mSetBarImg(mOMo, mOMoN);
		mOMo = null;
	}
}

// Show menu with id n, parent p at x,y, if sl sub left
var mSwTm;
function mShow(p,n,x,y,sl) {
	var m = mGet(n);
	mElMove(m.div, x, y);
	mHideSub(p);
	mHideSel(m);
	if(0&&navigator.userAgent.indexOf("Gecko/2001")!=-1){clearTimeout(mSwTm);mSwTm=setTimeout('mElShow(mGet('+n+').div,1)',1)}else mElShow(m.div,1)
	p.open = m;
	m.sl = sl;
}

// Abort hide timer
function mTimerOff() {
	if (mTimer) clearTimeout(mTimer)
}

// Start hide timer
function mTimerOn() {
	if (mTimer) clearTimeout(mTimer);
	mTimer = window.setTimeout("mHideTop()", mHideTime);
}

// Called on mouseover in menubar, for menuid n, area obj o, show bar gif mo, opt mo img name mon
function mTopIn(n,o,mo,mon) {
	if (mTop.open && mTop.open.n != n)
		mHideTop();
	if (mo) {
		if (mOMo) mSetBarImg(mOMo, mOMoN);
		var oo = mSetBarImg(mo, mon);
		mOMoN = mon;
		mOMo = oo;
	}
	if (mSetWin()) return;
	mInTop = 1;
	mTimerOff();
	if (mIE && !mWin.document.body) return;
	var m = mGet(n);
	if (!m) return;
	var x = mGetOfsX(m, o);
	var y = mGetOfsY(m, o);
	var d = m.div;
	if (!d) return;
	if (d.all) d = d.all[0];
	var maxx = mWinWidth() - mPopWidth; //mElWidth(d);
	var maxy = mWinHeight() - mElHeight(m.div);
	if (maxy < 0) maxy = 0;
	// adjust pos to corner
	if (mPos == "l") x = 0;
	else if (mPos == "t") y = 0;
	else if (mPos == "r") x = maxx;
	else if (mPos == "b") y = maxy;
	// keep in window
	if (x > maxx) x = maxx;
	if (y > maxy) y = maxy;
	mShow(mTop,n,x,y + mScrollOfs());
}

// Called on mouseout in menubar
function mTopOut() {
	if (mInTop) mTimerOn(); // ie<5.5 popin,topout bug
}

// Called on mouseover on menuitem i in menu with id n, from item obj o
function mPopIn(o,n,i) {
	mInTop = 0;
	mTimerOff();
	var m = n ? mGet(n) : this.m;
	if (!m) return; // NS4 mo lyr
	var nt = mNTop(m.n)-1;
	var style = window.mStyles;
	style = style && style[nt] ? style[nt] : mID + 'mpop';
	var istyle = style + (window.mV2 ? 'i' : '')
	var imgw;
	// Hide previous selected item, and show new selected item
	if ((mIE || mDOM) && o != m.sel) { 
		mHideSel(m); m.sel = o;
		o.className = istyle + "Over";
		imgw = mWin;
	}
	if (mNS4 && this != m.sel) {
		mHideSel(m); m.sel = this;
		var bgs = window.mOverBGs;
		bgs = bgs && bgs[nt] ? bgs[nt] : mOverBG;
		i = this.i;
		var l = m.lmo; 
		if (l) {
			l.visibility = "hide";
			l.clip.height = 0;
			l.document.open();
			l.document.write('<div class="' + istyle + 'Overns"><a href="' + this.link + '" class="' + style + 'Overns">' + this.cap + '</a></div>');
			l.document.close();
			l.moveTo(this.x, this.y); l.link = this.link;
			//l.bgColor = bgs; 
			l.zIndex=10; l.visibility = "inherit";
		}
		this.bgColor = bgs;
		imgw = this.il;
	}
	if (m.data[m.dataofs + i * 3 + 2]) { // Item has submenu?
		if (imgw && window.mSubImgMO) {
			var imn = "mi" + mID + m.n + "_" + (i*1+1);
			var im = imgw.document.images[imn];
			im.src = window.mSubImgMO;
			m.selimn = imn;
			if (mNS4) m.selim = im;
		}
		// Find pos of item
		var x, y, w;
		if (mIE || mGecko) {
			w = o.offsetWidth;
			x = 0;
			y = 0;
			while (o) { x += o.offsetLeft; y += o.offsetTop; o = o.tagName == "HTML" ? null : o.parentNode; }
		} else if (mNS4) {
			o = m.ls[i];
			w = o.clip.width;
			x = o.pageX;
			y = o.pageY;
		}
		if (mGecko) { x = mElLeft(m.div); }
		// if it fits, show right, else left
		w = mPopWidth;
		var mw = mPopWidth;
		var isl;
		if (!m.sl && x + w + mw <= mWinWidth())
			x += w;
		else {
			x -= mw; isl = 1;
		}
		if (x < 0)
			x = mWinWidth() - mw;
		var nmid = m.n + "_" + (i*1 + 1);
		var nm = mGet(nmid);
		var maxy = mWinHeight() - mElHeight(nm.div) + mScrollOfs();
		if (maxy < 0) maxy = 0;
		if (y > maxy) y = maxy;
		mShow(m, nmid, x, y, isl);
	} else
		mHideSub(m);
}

// Hide selection of item
function mHideSel(m) {
	if (m.sel) {
		var nt = mNTop(m.n)-1;
		var style = window.mStyles;
		style = style && style[nt] ? style[nt] : mID + 'mpop';
		if (mIE || mDOM) m.sel.className = style + (window.mV2 ? 'i' : '');
		if (mNS4){ if(m.mlo)mElShow(m.lmo, 0); m.sel.bgColor = null; }
		if (m.selimn) {
			var im = m.selim;
			if (!im) im = mWin.document.images[m.selimn];
			if (im) im.src = mSubImg;
		}
		m.selimn = null;
		m.selim = null;
		m.sel = null;
	}
}

// Mouseout of item
function mPopOut(o) {
	mTimerOn();
}

// Click on item: open link (NS4)
function mLink(x,n,i) {
	mTimerOff();
	mHideTop();
	var dstwin = mWin;
	if (window.mTarget) dstwin = top[window.mTarget];
	var l = n ? mGet(n).data[i+1] : this.link;
	if (l) dstwin.location = l;
}
