
menuopen=-1;
menustarttop=85;
menuvspacing=28;

var ismsie = document.all ? true:false;
var ismac=(navigator.appVersion.indexOf("Mac")!=-1);
var isopera=(navigator.userAgent.indexOf("Opera")!=-1);

if (ismsie&&!isopera) {
	menuheadwidth=120;
	menuoffset=-1;
	menuwidadd=0;
} else {
	menuheadwidth=115;
	menuoffset=6;
	menuwidadd=-10;
}


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 is the pop-out menu
	// menuheadobj is the head object to the left
	//
	menuobj=findObj("menu"+menuid);
	menuheadobj=findObj("menuhead"+menuid)

	if (menuobj) {
		dh=document.body.clientHeight;
		dscroll=document.body.scrollTop;
		
		if(ismsie)
			menupos=menuheadobj.parentElement.offsetTop-dscroll;
		else
			menupos=menuheadobj.parentNode.offsetTop-dscroll;
			
		menuheight=menuobj.clientHeight;

		// If the pop-out menu would go off the bottom of the browser
		// then shift the top up so that it doesnt
		//
		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;
		}

		if(!ismsie) {
			menuobj.style.zIndex=1;
			menuheadobj.width=120;
		}
		
  		menuobj.style.visibility='visible';

		menuheadobj.className='menuhead_opensub';
	} else {
		menuheadobj.className='menuhead_open';
	}

	menuopen=menuid;
}

function findsrc(e) {
  if(ismsie)
    return window.event.srcElement;
  else  {
    if (!e.target.className) return e.target.parentNode;
    return e.target;	
  }
}

function menuhead_rollon(e) {
	o=findsrc(e);
	
	openmenuid=o.id.substr(8);

	if(menuopen>=0) hidemenu(menuopen);

	showmenu(openmenuid);
}

function menu_rollon(e) {
	o=findsrc(e);
	if (o.className == "menuopt") o.className = "menuopt_over";
}

function menu_rolloff(e) {
	o=findsrc(e);
  	if (o.className.substring(0,7) == "menuopt") o.className = "menuopt";
}

function menu_mousedown(e) {
	o=findsrc(e);
	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(e) {
	o=findsrc(e);
 	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(e) {
	o=findsrc(e);
	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(e) {
	if(menuopen>=0) {
		o=findsrc(e);
		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=menustarttop+(x*menuvspacing);
		curwidth=0;

		for (y in menuoptions[x]) {
			t=menuoptions[x][y].split('|');
			if(y==0) {
				// A container for the menu, doesn't need an id as it's just for positioning
				document.write ('<div class="menuspan" style="top: ' + vpos + 'px;">');

				// The menu header
				document.write ('<div class="menuhead" id="menuhead'+x+'" unselectable="on" onselectstart="return false" style="width: '+ menuheadwidth +'px">'+t[0]+'</div>');
							
				// The menu sub-items, if there are any
				if(subopts>1) {
					wid=(parseInt(t[2])+menuwidadd);
					document.write ('<div class="menu" id="menu'+x+'" style="left: '+ (menuheadwidth+menuoffset) +'px; width: ' + wid +'px;">');
					curwidth=t[2];
				}
			} else {		
				// Sub-menu items
				if(t[0]!='-') {
					document.write ('<div class="menuopt" id="'+x+'.'+y+'" unselectable="on" nowrap>'+t[0]+'</div>');
				} else {
					document.write ('<div class="menuopn" id="'+x+'.'+y+'" unselectable="on">&nbsp;</div>');
				}
			}
		}
		if(subopts>1) {
			//document.write('</table>');
			document.write('</div>');
		}
		document.write('</div>');
	}
}

function menu_addevents() {
	for (x in menuoptions) {
		o=document.getElementById('menuhead'+x);

		o.onmouseover = menuhead_rollon;
		o.onmousedown = menu_mousedown;
		o.onmouseup = menu_mouseup;
		o.onclick = menu_mouseclick;
		
		if (menuoptions[x].length>1) {
			o=document.getElementById('menu'+x)
			
			o.onmouseout = menu_rolloff;
			o.onmouseover = menu_rollon;
			o.onmousedown = menu_mousedown;
			o.onmouseup = menu_mouseup;
			o.onclick = menu_mouseclick;
		}
	}
	document.onmouseover=document_mouseover;
}

// Script (c)2001-2007 Digital Photography Review, http://www.dpreview.com/