// for hrejsi written by Aragorn, based on Script of Phil Askey

menuopen=-1;
menuurlprefix='';
topposition=24;

function findObj(n) {
	var x;
	
	_d=document;
	if(!(x=_d[n])&&_d.all) x=_d.all[n];
	if(!x && _d.getElementById) x=_d.getElementById(n);
	return x;
}

function hidemenu(menuid) {
	menuobj=findObj("menu"+menuid)
	menuheadobj=findObj("menuhead"+menuid);

	if (menuobj) {
		menuobj.style.visibility='hidden';
	};
	menuheadobj.className='menuhead';
	menuopen=-1;
}

function showmenu(menuid) {
	menuobj=findObj("menu"+menuid);
	menuheadobj=findObj("menuhead"+menuid);

	if (menuobj) {
		dh=document.body.clientHeight;
		menupos=menuheadobj.parentElement.offsetTop-document.body.scrollTop;
		menuheight=menuobj.clientHeight;
		if((menupos+menuheight>dh)&&(menupos>0)) {
			menutop=dh-(menuheight+menupos)-2;
			if (menutop<-menupos) menutop=-menupos;
			menuobj.style.top=menutop;
		} else {
			menuobj.style.top=0;
		}
		
		menuobj.style.visibility='visible';
		menuheadobj.className='menuhead_opensub';
	} else {
		menuheadobj.className='menuhead_open';
	};

	menuopen=menuid;
}

function menuhead_rollon() {
	_o=window.event.srcElement;
	openmenuid=_o.id.substr(8);

	if(menuopen>=0) hidemenu(menuopen);

	showmenu(openmenuid);
}

function menu_rollon() {
	_o=window.event.srcElement;
	if (_o.className == "menuopt") _o.className = "menuopt_over";
}

function menu_rolloff() {
	_o=window.event.srcElement;
  	if (_o.className.substring(0,7) == "menuopt") _o.className = "menuopt";
}

function menu_mousedown() {
	_o=window.event.srcElement;
	if (_o.className == "menuopt_over") _o.className = "menuopt_click";
 	if (_o.className == "menuhead_open") _o.className = "menuhead_click";
 	if (_o.className == "menuhead_opensub") _o.className = "menuhead_clicksub";
}

function menu_mouseup() {
	_o=window.event.srcElement;
 	if (_o.className == "menuopt_click") _o.className = "menuopt_over";
 	if (_o.className == "menuhead_click") _o.className = "menuhead_open";
 	if (_o.className == "menuhead_clicksub") _o.className = "menuhead_opensub";
}

function menu_mouseclick() {
	_o=window.event.srcElement;
	if (_o.className == "menuopt_over") {
		menusubid=_o.id.split(".");
		menuid=menusubid[0];
		menuoptid=menusubid[1];
		t=menuoptions[menuid][menuoptid].split('|');
		newurl=t[1];
		if (newurl.substr(0,7)!='http://') newurl=menuurlprefix + newurl;
		window.location=newurl;
	} else if ((_o.className == "menuhead_open")||(_o.className == "menuhead_opensub")) {
		menuid=_o.id.substr(8);
		t=menuoptions[menuid][0].split('|');
		newurl=t[1];
		if (newurl.substr(0,7)!='http://') newurl=menuurlprefix + newurl;
		window.location=newurl;
	}
}

function document_mouseover() {
	if(menuopen>=0) {
		_o=window.event.srcElement;
		menuobj=findObj("menu"+menuopen);
		if (menuobj) {
			if ((menuobj.style.visibility == "visible")&&(_o.id.length==0)) hidemenu(menuopen);
		} else {
			menuobj=findObj("menuhead"+menuopen);
			if ((menuobj.className == "menuhead_open")&&(_o.id.length==0)) menuobj.className="menuhead";
		}
	}
}

function menu_build() {
 for (x in menuoptions) {
  subopts=menuoptions[x].length;
  vpos=topposition+(x*15);
  for (y in menuoptions[x]) {
   t=menuoptions[x][y].split('|');
   if(y==0) {
    document.write ('<span class="menuspan" style="position: absolute; top: ' 
                    + vpos + ';">&nbsp;&nbsp;');
    document.writeln ('<span style="'+ t[3] +'" class="menuhead" id="menuhead'
                       +x+'" unselectable="on">'+ t[0]+'</span>');
    if(subopts>1) 
     document.writeln ('<span id="menu'
                       +x+'" class="menu"><table width="'
                       +t[2]+'" border="0" cellspacing="0" cellpadding="0">');
   } 
   else {
    document.writeln ('<tr><td class="menuopt" style="'+ t[2] + 
                      '" id="'+x+'.'+y+
                      '" unselectable="on">'+t[0]+'</td></tr>');
   }
  }
  if(subopts>1) document.writeln('</table></span>');
  document.write('</span>');
 }
}

function menu_addevents() {
	for (x in menuoptions) {
		eval('menuhead'+x+'.onmouseover = menuhead_rollon');
		eval('menuhead'+x+'.onmousedown = menu_mousedown');
		eval('menuhead'+x+'.onmouseup = menu_mouseup');
		eval('menuhead'+x+'.onclick = menu_mouseclick');
		if (menuoptions[x].length>1) {
			eval('menu'+x+'.onmouseout = menu_rolloff');
			eval('menu'+x+'.onmouseover = menu_rollon');
			eval('menu'+x+'.onmousedown = menu_mousedown');
			eval('menu'+x+'.onmouseup = menu_mouseup');
			eval('menu'+x+'.onclick = menu_mouseclick');
		}
	}
	document.onmouseover=document_mouseover;
}

