//////////////////////////////////////////////////////////////////////
//
// A few styles are expected to be defined in the accompanying
// HTML that includes this JavaScript. They are all of element
// independent class type:
//
// .Menu	  : the menu container. The style for these are always
//		    initially set to include the following, even if not
//		    explicitly in the class
//			position:absolute;
//			visibility:hidden;
//			overflow:hidden;
// .MenuItem	  : the item container
// .MenuText	  : used to render the item text. The style for these
//		    are always set to include the following, even if not
//		    explicitly in the class
//			width:100%;
// .MenuArrow	  : the pullright arrow container
// .MenuPullRight : the pullright arrow itself
//

var RollerMenus       = new Array();
var RollMenu          = new Array();
var RollMenuTop       = -1;
var RollMenuAppearing = null;
var RollTimer         = 0;

function addEvent(obj,ev,fun) {
	if (obj.addEventListener){
		obj.addEventListener(ev,new Function('event', fun),false);
	} else {
		eval('obj.on'+ev+'=new Function("'+fun+'")');
	}
}
function getCoords(el,smnu) {
	smnu.l = 0;
	smnu.t = 0;
	if (isIE || isDOM ) {
		while(el){
			smnu.t += el.offsetTop;
			smnu.l += el.offsetLeft;
			el = el.offsetParent;
		}
	} else {
		smnu.l = el.pageX;
		smnu.t = el.pageY;
	}
}
function applyOnClass(){
	if(this.mstyle.menuHi)applyClass(this.name,this.mstyle.menuHi);
}
function applyOffClass(){
	if(this.mstyle.menuLo)applyClass(this.name,this.mstyle.menuLo);
}
function RollMenuAppear() {
	var smnu = RollMenuAppearing;
	var sty  = smnu.sty;
	sty.visibility='visible';
	if (isIE) {
		sty.posTop  = smnu.t;
		sty.posLeft = smnu.l;
	} else {
		sty.top  = smnu.t;
		sty.left = smnu.l;
	}
	RollMenuAppearing = null;
}
function RollMenuVis(how) {
	if (RollTimer != null) {
		clearTimeout(RollTimer);
		RollTimer = null;
	}
	// Bit of fudge as calling this function
	// from timer seems to screw up the args
	if (how!='visible' && this.name != RollMenu[RollMenuTop].name){
		mnu=RollMenu[RollMenuTop];
	}else{
		mnu=this;
	}
	if (RollMenuTop >= 0 &&
		RollMenu[RollMenuTop].name == mnu.name &&
		how == 'visible') return;
	mnu.displayed = (how == 'visible');
	if (mnu.displayed) {
		var smnu = new Object();
		if ( mnu.img != null ) {
			if (isNS6) {
				smnu.t = mnu.img.offsetTop;
				smnu.l = mnu.img.offsetLeft;
				if ( mnu.where == 'right' )
					smnu.l += mnu.img.offsetWidth;
				else if ( mnu.where == 'below' )
					smnu.t += mnu.img.offsetHeight;
			} else if (isIE || isDOM) {
				smnu.t = mnu.img.offsetParent.offsetTop;
				smnu.l = mnu.img.offsetLeft;
				if ( mnu.where == 'right' )
					smnu.l += mnu.img.offsetWidth;
				else if ( mnu.where == 'below' )
					smnu.t += mnu.img.offsetHeight;
			} else {
				smnu.t = mnu.img.y;
				smnu.l = mnu.img.x;
				if ( mnu.where == 'right' )
					smnu.l += mnu.img.width;
				else if ( mnu.where == 'below' )
					smnu.t += mnu.img.height;
			}
		} else {
			mnu.setHi();

			if (RollMenuTop>=0) {
				pmnu   = RollMenu[RollMenuTop];
				smnu.t = pmnu.dt;
				smnu.l = pmnu.dl;
				if ( mnu.where == 'right' ){
					if(isNS6){
						smnu.l += pmnu.w - 3;
						smnu.t  = mnu.el.offsetTop-3;
					}else if(isIE||isDOM){
						smnu.l += pmnu.w - 3;
						smnu.t += mnu.el.offsetTop-2;
					}else{
						smnu.l += pmnu.w - 3;
						smnu.t  = mnu.el.pageY-2;
					}
				} else if ( mnu.where == 'below' )
					smnu.t += pmnu.h - 1;
			} else {
				getCoords(mnu.el, smnu);
				if ( mnu.where == 'right' )
					if(isIE||isDOM)
						smnu.l += mnu.el.offsetWidth;
					else
						smnu.l += mnu.el.clip.width;
				else if ( mnu.where == 'below' ){
					if(isIE||isDOM)
						smnu.t += mnu.el.offsetHeight;
					else
						smnu.t += mnu.el.clip.height;
				}
			}
		}
		mnu.dt     = smnu.t;
		mnu.dl     = smnu.l;
		mnu.parent = RollMenuTop;
		smnu.w     = mnu.w;
		smnu.h     = mnu.h;
		smnu.sty   = mnu.sty;
		RollMenuAppearing = smnu;
		RollMenu.push( mnu );
		RollMenuTop = RollMenu.length - 1;
		RollMenuAppear();
	} else {
		mnu.setLo();
		RollMenu.pop();
		RollMenuTop = RollMenu.length - 1;
		RollMenuAppearing = null;
		mnu.sty.visibility='hidden';
	}
}
function getEl(el){
	if (isIE||isDOM) return document.getElementById(el);
	if (RollMenuTop<0) return document.layers[el];
	return RollMenu[RollMenuTop].lay.layers[el];
}
function RollMenuPrepare(el) {
	this.el = getEl(el);
	if (this.mlayer.length) {
		if (isDOM) {
			this.lay = document.getElementById(this.mlayer);
		} else {
			this.lay = document.layers[this.mlayer];
		}
		if (isIE||isDOM) {
			this.w   = this.lay.offsetWidth;
			this.h   = this.lay.offsetHeight;
			this.sty = this.lay.style;
		} else {
			this.w   = this.lay.clip.width;
			this.h   = this.lay.clip.height;
			this.sty = this.lay;
		}
	}
}
function RollMenuAdd(name, el) {
	if(!RollerMenus[name]) return;
	mnu = RollerMenus[name];
	mnu.prepare(el);
	mnu.setVis('visible');
}
function isInParent(x,y,mnu) {
	p = new Object();
	if (mnu.img != null){
		if (isNS6) {
			p.t = mnu.img.offsetTop;
			p.l = mnu.img.offsetLeft;
			p.w = mnu.img.offsetWidth;
			p.h = mnu.img.offsetHeight;
		} else if (isIE || isDOM) {
			p.t = mnu.img.offsetParent.offsetTop;
			p.l = mnu.img.offsetLeft;
			p.w = mnu.img.offsetWidth;
			p.h = mnu.img.offsetHeight;
		} else {
			p.t = mnu.img.y;
			p.l = mnu.img.x;
			p.w = mnu.img.width;
			p.h = mnu.img.height;
		}
	}else{
		getCoords(mnu.el, p);
		if(isIE||isDOM){
			p.w = mnu.el.offsetWidth;
			p.h = mnu.el.offsetHeight;
		}else{
			p.w = mnu.el.clip.width;
			p.h = mnu.el.clip.height;
		}
	}
	if (((x <= p.l) || (x >= p.l + p.w)) || ((y <= p.t) || (y >= p.t + p.h))) {
		return false;
	}else{
		return true;
	}
}
function RollMenuClear() {
	if (RollMenu.length == 0) return;
	while ( RollMenuTop>=0 ) RollMenu[RollMenuTop].setVis('hidden');
}
function RollMenuDown(e) {
	if (RollMenu.length == 0) return;
	if (RollTimer != null ) return;
	var x, y, lt, t, l, w, h;
	var mnu;
	while ( RollMenuTop >= 0 ) {
		mnu = RollMenu[RollMenuTop];
		t = mnu.dt;
		l = mnu.dl;
		if (isIE) {
			x = e.clientX+document.body.scrollLeft-document.body.clientLeft;
			y = e.clientY+document.body.scrollTop-document.body.clientTop;
			w = mnu.lay.offsetWidth;
			h = mnu.lay.offsetHeight;
			lt = mnu.lay.offsetTop;
		} else if (isDOM) {
			x = e.pageX;
			y = e.pageY;
			w = mnu.lay.offsetWidth;
			h = mnu.lay.offsetHeight;
			lt = mnu.lay.offsetTop;
		} else {
			x = e.pageX;
			y = e.pageY;
			w = mnu.lay.clip.width;
			h = mnu.lay.clip.height;
			lt = mnu.lay.top;
		}
		if (isInParent(x,y+lt-mnu.dt,mnu)) {
			return;
		}
		if (((x < l) || (x >= l + w)) || ((y < t) || (y >= t + h))) {
			if (RollMenuTop>0){
				RollMenu[RollMenuTop].setVis('hidden');
			}else{
				RollTimer = setTimeout(RollMenu[RollMenuTop].setVis,250,'hidden');
				return;
			}
		} else {
			return;
		}
	}
}

////////////////////////////////////////////////////////////////////////
// A menu item
function MenuItem(mnu){
	itmno       = ++mnu.items;
	this.id     = mnu.name+itmno;
	this.name   = mnu.name+'i'+itmno;
	this.mstyle = mnu.mstyle;
}
function makeIntoTxtItem(mnu,itemtxt,itemhref){
	itemdiv=document.createElement("div");
	itemdiv.className=this.mstyle.menuItem;
	itemdiv.zIndex=mnu.items;
	mnu.div.appendChild(itemdiv);

	adiv=document.createElement("div");
	adiv.id=this.name;
	adiv.setAttribute('name',this.name);
	adiv.className=this.mstyle.menuLo;
	addEvent(adiv,"mouseover","applyClass(this.id,'"+this.mstyle.menuHi+"')");
	addEvent(adiv,"mouseout","applyClass(this.id,'"+this.mstyle.menuLo+"')");
	itemdiv.appendChild(adiv);

	aitem=document.createElement("a");
	aitem.id=this.name;
	aitem.name=this.name;
	aitem.target="_top";
	aitem.className=this.mstyle.menuLo;
	aitem.href=itemhref;
	adiv.appendChild(aitem);

	txtdiv=document.createElement("div");
	txtdiv.className=this.mstyle.menuText;
	txtdiv.style.width='100%';
	txtdiv.innerHTML=itemtxt;
	aitem.appendChild(txtdiv);
}
function makeIntoMenuItem(mnu,itemtxt){
	itemdiv=document.createElement("div");
	itemdiv.className=this.mstyle.menuItem;
	itemdiv.id=this.id;
	itemdiv.zIndex=mnu.items;
	mnu.div.appendChild(itemdiv);

	adiv=document.createElement("div");
	adiv.id=this.name;
	adiv.setAttribute('name',this.name);
	addEvent(adiv,"mouseover","RollMenuAdd('"+this.name+"','"+this.id+"')");
	addEvent(adiv,"mouseout","RollMenuDown(event)");
	itemdiv.appendChild(adiv);

	aitem=document.createElement("a");
	aitem.id=this.name;
	aitem.name=this.name;
	aitem.target="_top";
	aitem.className=this.mstyle.menuLo;
	aitem.href="#";
	adiv.appendChild(aitem);

	txtdiv=document.createElement("div");
	txtdiv.className=this.mstyle.menuText;
	txtdiv.style.width='100%';
	aitem.appendChild(txtdiv);

	imgdiv=document.createElement("div");
	imgdiv.className=this.mstyle.menuArrow;
	txtdiv.appendChild(imgdiv);

	img=document.createElement("img");
	img.className=this.mstyle.menuPullRight;
	img.src=RollImgBase+"arrow.gif";
	imgdiv.appendChild(img);

	txtspan=document.createElement("div");
	txtspan.innerHTML=itemtxt;
	txtdiv.appendChild(txtspan);
}
function makeIntoImgItem(mnu,itmimg,itmhref,itmalt){
	itemdiv=document.createElement("div");
	itemdiv.className=this.mstyle.menuItem;
	itemdiv.id=this.id;
	itemdiv.zIndex=mnu.items;
	mnu.div.appendChild(itemdiv);

	aitem=document.createElement("a");
	aitem.id=this.name;
	aitem.name=this.name;
	aitem.target="_top";
	aitem.href=itmhref;
	addEvent(aitem,"mouseover","return RollOver('"+this.name+"')");
	addEvent(aitem,"mouseout","return RollOff('"+this.name+"')");
	itemdiv.appendChild(aitem);

	img=document.createElement("img");
	img.id=this.name;
	img.setAttribute('name',this.name);
	img.src=itmimg;
	img.alt=itmalt;
	img.border=0;
	aitem.appendChild(img);
}
MenuItem.prototype.asTxtItem=makeIntoTxtItem;
MenuItem.prototype.asMenuItem=makeIntoMenuItem;
MenuItem.prototype.asImgItem=makeIntoImgItem;
MenuItem.prototype.prepare=RollMenuPrepare;
MenuItem.prototype.setVis=RollMenuVis;
MenuItem.prototype.setHi=applyOnClass;
MenuItem.prototype.setLo=applyOffClass;

//////////////////////////////////////////////////////////////////////
// A menu
function RollOverMenu(name,mstyle,mname,where){
	if ( ! RollerMenus[name] ) {
		RollerMenus[name]  = this;
		this.div           = createMenuDiv(mname,mstyle);
		this.items         = 0;
		this.name          = name;
		this.where         = where;
		this.mlayer        = mname;
		this.displayed     = false;
		this.mstyle        = mstyle;
		if ( Rollers[name] )
			this.img = Rollers[name].doc.images[name];
		else
			this.img = null;
	}
}
function createMenuDiv(id,mstyle){
	div=document.createElement("div");
	div.className=mstyle.menuDiv;
	div.id=id;
	div.style.zIndex=0;
	div.style.position='absolute';
	div.style.visibility='hidden';
	div.style.overflow='hidden';
	addEvent(div,"mouseout","RollMenuDown(event)");
	document.body.appendChild(div);
	return div;
}
function NewItem(itmtxt, itmhref) {
	mnuitm = new MenuItem(this);
	mnuitm.asTxtItem(this,itmtxt,itmhref);
}
function NewImgItem(itmimg, itmhref, itmalt) {
	mnuitm = new MenuItem(this);
	mnuitm.asImgItem(this,itmimg,itmhref,itmalt);
	if ( Rollers[mnuitm.name] )
		mnuitm.img = Rollers[mnuitm.name].doc.images[mnuitm.name];
	else
		mnuitm.img = null;
}
function NewMenuItem(itmtxt, mname, where) {
	mnuitm = new MenuItem(this);
	mnuitm.asMenuItem(this,itmtxt);

	mnuitm.where     = where;
	mnuitm.mlayer    = mname;
	mnuitm.displayed = false;
	mnuitm.img       = null;
	RollerMenus[mnuitm.name] = mnuitm;
}
RollOverMenu.prototype.addItem=NewItem;
RollOverMenu.prototype.addMenuItem=NewMenuItem;
RollOverMenu.prototype.addImgItem=NewImgItem;
RollOverMenu.prototype.prepare=RollMenuPrepare;
RollOverMenu.prototype.setVis=RollMenuVis;
RollOverMenu.prototype.setHi=applyOnClass;
RollOverMenu.prototype.setLo=applyOffClass;

//////////////////////////////////////////////////////////////////////
// A set of style class names to be used in a menu
function RollOverStyle(mDiv,mItem,mTxt,mArr,mPullR,mHi,mLo){
	this.menuDiv		= mDiv;
	this.menuItem		= mItem;
	this.menuText		= mTxt;
	this.menuArrow		= mArr;
	this.menuPullRight	= mPullR;
	this.menuHi		= mHi;
	this.menuLo		= mLo;
}
