//
// to customize:
//
// set the homepage name, the root dir and image dir
// set the onLoadBody() for more than 2 sets of menus
//


// get the root directory...

var gHome = "index.htm"
var gRootName = ""
var gMatchPath = "(.*[\\/\\\\]"+gRootName+"[\\/\\\\])"

// --------------
// -- VERSIONS -- 
// --------------
// 
var V3 = 0;
if (parseInt(navigator.appVersion)<=3) V3 = 1;
var NN4off = 0; // flag
if (V3) NN4off = 1;
var IE4 = (document.all) ? 1 : 0;
var NN6DOM = 0;
	if (parseInt(navigator.appVersion) >= 5 && navigator.appVersion.indexOf("MSIE 5") == -1) NN6DOM = 1;
if (NN6DOM) IE4 = 1;
var NN4 = (document.layers) ? 1 : 0;
var VER4 = (IE4 || NN4) ? 1 : 0;

var browser = "IE4"
if (NN4) browser = "NN4"
if (NN6DOM) browser = "NN6"
//for popup window
//var version4 = (!parseInt(navigator.appVersion) <=3) ? event : null;
//alert(version4);

// -----------
// -- PATHS --
// -----------
//
// find the pathnames to the images since this .js will be used in
// many different levels of the site potentially
//
//alert('location='+document.location);
var thisurl = ""+document.location
var path_main = "/error_in_js/"

if (!V3) {
	var thestring = gMatchPath 
	var re1 = new RegExp(thestring,"i")
	var ma1 = re1.exec(thisurl)

//	alert('Found' + ma1[0]);
	if (ma1) { 
		path_main = ma1[1]
	}
}

var path_en = path_main

//  
// all non 4.0 browsers back to home! (except the very first page after splash...)
//
if (V3 && (thisurl.indexOf(gHome)<0))
	window.location.href=path_main+gHome

var path_mainimages = path_main + "images/" // usually
//alert(path_mainimages);
var path_enimages = path_mainimages

// ------------------
// -- more globals --
// ------------------

var preselectleft = new Array();

var gMenus,lMenus;

var gMouse = ""
var CRLF = "\n"
var gDoc = ""
var gDoc2 = ""
var gX = ""
var gY = ""
var gcount = 0
var mapprefix = "map_"
var divprefix = "DIV"
var menuprefix = "menu_"

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

//
// end globals and initialization
//

// ------------------------------------------------------------------- //

// testing
function dump(thing) {
	documentwriteln("["+thing+"]<br>")
}

function preSelect(menu,num,widthopt) {
	if (V3 || !menu) return; // 
	
	menu.preselectnum = num;
	if (widthopt) menu.preselect = widthopt;
		
	mapOver(menu.name,num,true,widthopt,1) // even if it isn't visible right away, we need to get it set right!
	if (!menu.visible || (menu.visible == "menu")) {
		//setVis(divprefix+menu.name+"SEL","hide",1)
	}
}

function documentwrite(thing) {
	gDoc += thing
}

//
// amenu is text!
//
function showhideMenu(amenu,how,mousing) {
	if (!menuprefix) {
		MM_reloadPage(true);
		return
	}
	var jsmenu = eval(menuprefix+amenu)
	var divname = divprefix+amenu
	
	setVis(divname+"VIS",how,1)
 
	if (how == "hide") {
		//if (!mousing) setVis(divname+"SEL",how,1)
		setVis(divname+"ROLL",how,1)
	} else {
		// only re-show if a single item has been preselected!
		if (jsmenu.preselect) {
			setVis(divname+"SEL","show",1)
		}
	}
	
}		

// ------------ //
// -- EVENTS -- //
// ------------ //

var gTID = 0;
var gMenu = "";
var gWhich = -1;

function divTimeout(amenu) {
	var thetimeout = "showhideMenuBoth('"+amenu+"','hide',1)"
	//gMenu = amenu;
	gTID = setTimeout(thetimeout,500)
	return gTID
}

function subsTimeout(amenu,which) {
	var thetimeout = "hideSubs('"+amenu+"',0,1)"
	gMenu = amenu;
	gTID = setTimeout(thetimeout,500)
	gWhich = which
	return gTID
}

//
// we want a sub menu to trickle down and hide it's parent
//
function showhideMenuBoth(amenu,how,timeout) {
	if (timeout) gTID = 0;
	if (timeout && (gMenu == amenu)) return;

	// but for now just do the first one...
	showhideMenu(amenu,how,1)
	var menuobj = eval(menuprefix+amenu) 
	
	var parent = menuobj.parent;
	if (parent) showhideMenu(parent.name,"hide",1)
	
	if (how == "hide") hideSubs(menuobj)

}

function hideSubs(menuobj,except,timeout) {
	var subs = menuobj.submenus
	if (subs) {
		for (var i=0;i<subs.length;i++) {
			if (subs[i].name != except) 
				showhideMenu(subs[i].name,"hide",1)
		}
	}
	if (timeout) gTID = 0
}

function removeTimeout(gTID) {
	clearTimeout(gTID)
	gTID = 0;
	gMenu = "";
}

// ---------------- //
// -- END EVENTS -- //
// ---------------- //
	
//
// dumps the imagemap
//
function showMenuMap() {
	if (!this.links.length) {
		//alert(this.name+" has no links")
		return;
	}
	
	var x = ""

	x+= "\n<MAP NAME=\""+mapprefix+this.name+"\">"
	for (var i=0; i<this.links.length;i++) {
		var ml = this.links[i];
		var divname = this.name;
		var href = ml.url
		if (ml.url == "#")
			href = "javascript:void(0)" //"javascript:alert('Please select a menu item.')"
		var y ="\n<AREA SHAPE=RECT COORDS=\""+ml.coords+"\" alt=\""+ml.alt+"\" HREF=\""+href+"\" "
		var ws = ""
		//if (ml.url == "#") ws = "window.status='';"
		//if (ws) alert(ws)
		y+=" onMouseOver=\""+ws+"mapOver('"+divname+"',"+ml.linknum+",true);"+ws+"\" onMouseOut=\"mapOver('"+divname+"',"+ml.linknum+",false)\">"
		x+=y
	}
	x+="\n</MAP>"

	gY = x
	//docwrite(x,0)
}

//
// shows the image
//
function showMenuImage() {
	if (V3) return;
	var w = this.width;
	var h = this.height;
	var top = this.top;
	var left = this.left;

	var name = this.name;
	var style = "width:"+w+"px;height:"+h+"px;"
	style += "position:absolute;overflow:hidden;" //top:0px;left:0px;"
	var NN4where = ""
	if (!NN4) { 
		var divtype = "DIV"
		style += "top:"+top+"px;left:"+left+"px;"
	} else {
		divtype = "LAYER"
		NN4where = " top="+top+" left="+left+" visibility="
	}
	// works for all 3...
	blocknone = "block"
	if (!this.visible || (this.visible == "menu")) blocknone="none";
	
	var divname = divprefix+name
	
	vis = "visible"
	//if (NN6DOM) vis = "hidden";
	
var xy = "";

	// debugging NN6
	//var mo = "" // " onMouseOver=\" gMouse+=this.id+' '; \" "
	var mo = ""
	
	//
	// 1 - default images
	//
	var vis2 = "hidden"
	
	if (NN6DOM) vis2 = "hidden;display:block" // a difference in DHTML in NN6
	
	var vis1 = "visibility:" // the word
	
	var vis3 = vis4 = vis5 = "" // nn4
	
	if (NN4) {
		vis2 = "visible" // a bug in NN4
		
		// NN4 layers!
		vis3 = "hide" 
		vis4 = "hide" // "show"
		var flagged = 0;
		// menubar and the left menus are visible...

		if (this.name.indexOf("drop")==-1) {
			vis4 = "show z-index=100"
			flagged = 1;
		}

		vis2 = ""
		vis1 = ""	
		vis5 = vis3+" z-index=100 "	
	}
	
	xy+= "\n\n<"+divtype+NN4where+vis4+" "+mo+" ID=\""+divname+"VIS\" style=\""+vis1+vis2+";" +style+ "\">\n"
	xy+= "<img src=\""+this.image1+"\" WIDTH="+w+" HEIGHT="+h+" ISMAP USEMAP=\"#"+mapprefix+name+"\" BORDER=0>\n"
	xy+= "</"+divtype+">"

	//
	// 2 - The Rollovers! (it doesn't seem to matter which of these two comes last)
	//
	
	vis2 = "hidden" // NN4 and IE45
	if (NN6DOM) vis2 = "visible;display:none;"
	
	if (vis3) vis2 = "hidden"
	
	xy+="\n<"+divtype+NN4where+vis3+" "+mo+" ID=\""+divname+"ROLL\" style=\""+vis1+vis2+";" +style+ "\">"
	xy+="\n<img src=\""+this.image2+"\" WIDTH="+w+" HEIGHT="+h+" ISMAP USEMAP=\"#"+mapprefix+name+"\" BORDER=0>"
	xy+="\n</"+divtype+">"

	//
	// 3 - PRESELECTED ITEM.  this layer is special.  it doesn't have a link!
	//
	xy+="\n<"+divtype+NN4where+vis5+" "+mo+" above=\""+divname+"ROLL\" ID=\""+divname+"SEL\" style=\""+vis1+vis2+";" + style+ "\">"
	xy+="\n<img src=\""+this.image2+"\" WIDTH="+w+" HEIGHT="+h+" BORDER=0 alt=\"You are here\" ISMAP USEMAP=\"#"+mapprefix+name+"\" BORDER=0 >"
	xy+="\n</"+divtype+">"	

	gX=xy // global
}

//
// not used
// 0 normal, 1 both, 2 debug only
//
function docwrite(thing,debughtml) {
	if (!debughtml) debughtml = 0;
	if (debughtml <= 1)
		documentwrite(thing)
	if (debughtml >= 1) {
		var re1 = new RegExp("<","g")
		var re2 = new RegExp(">","g")
		thing = thing.replace(re1,"&lt;")
		thing = thing.replace(re2,"&gt;<br>")
		documentwrite(thing)
	}
}


// ------------- //
// -- OBJECTS -- //
// ------------- //

//
// Menu 
//
function Menu(name,width,height,left,top,visible,imagepathopt,imagenameopt) {
	//
	// can convert coords (left,top / right,bottom) into top,left,height,width
	// we also get the imagenames from the name (name_off.gif,name_on.gif)
	//
	
	// properties
	this.name = name
	
	// can be set for hiding parent from submenu!
	this.parent = 0;
	
	// ex: "/images/"
	if (!imagepathopt) imagepathopt = ""
	
	// for rollovers
	var iname = name
	if (imagenameopt) iname = menuprefix+imagenameopt // reuse the same image for the countries
	
	this.image1 = imagepathopt + iname + "Off.jpg"
	this.image2 = imagepathopt + iname + "On.jpg"
	
	// if (imagenameopt) document.write("<img src="+this.image1+">")
	
	this.left = left
	this.top = top
	
	// 0 = no, 1 = yes, "menu" means no, but shows/hides accordingly.
	this.visible = visible
	
	this.height = height
	this.width = width
	this.links = new Array() // empty at first
	
	// methods
	//this.dump = dumpMenu
	this.showMap = showMenuMap
	this.showImage = showMenuImage
	
	this.preselect = 0; // by default, no preselection
	this.preselectnum = 0;
}

//
// MenuLink 
//
function MenuLink(menu,linknum,url,alt,coords,selected,submenuopt) {
	//
	// even if there is a submenu, I think we usually want to allow a click on the item.
	//
	this.linknum = linknum
	this.url = url
	this.menu = menu
	
	if (!selected) selected = 0;
	this.selected = selected
	if (selected) {
		menu.preselectnum = linknum
		menu.preselect = selected; // "0" (not 0) or -1 or whatever...
	}
	
	if (submenuopt) {
		// for example: "one"
		this.submenuname = submenuopt
	}
	
	if (!alt) alt = ""
	this.alt = alt

		this.coords = -1
		this.top = -1
		this.left = -1
		this.right = -1
		this.bottom = -1
		
	if (coords) {
		var ca = coords.split(",")
		if ((ca[0] == "h") || (ca[0] == "v")) {
			ml = menu.links;
			
			ca[1] = parseInt(ca[1]) + 1 // no overlaps
			
			if (ca[0] == "v") {
				var vadd = ca[1];
				var hadd = 0;
			} else {
				vadd = 0;
				hadd = ca[1];
			}
			
			//
			// left top, right bottom
			//
			var j = ml.length-1
			
			if (!ml[j]) {
				// alert(j+" "+menu+","+linknum+","+url+","+alt+","+coords+","+selected+","+submenuopt)
			}
			
			ca[0] = ml[j].left+hadd
			ca[1] = ml[j].top+vadd
			ca[2] = ml[j].right+hadd
			ca[3] = ml[j].bottom+vadd
			
		}
		// if (menu.name == "zero") alert(ca[0]+","+ca[2])
		
		this.left = parseInt(ca[0])
		this.top = parseInt(ca[1])
		this.right = parseInt(ca[2])
		this.bottom = parseInt(ca[3])
		
		// for NN6 flakiness in IMAGEMAP and CLIPPING
		ca[2]-=3 // ca[2]=Math.max(ca[2]-3,1)
		ca[3]-=3 // ca[3]=Math.max(ca[3]-3,1)
		
		// convert into a comma-separated string!
		this.coords = ""+ca 
	}
	
	if (!submenuopt) submenuopt = ""
	this.submenuopt = submenuopt
	
	if (menu)
		menu.links[menu.links.length] = this
}

// ----------------- //
// -- end OBJECTS -- //
// ----------------- //

function doDefaultVisibility(Menus) {
	if (V3) return;
	for (var i=0;i<Menus.length;i++) {
		var amenu = Menus[i]
		if (amenu.visible == 1) 
			how = "show";
		else 
			how = "hide";
		showhideMenu(amenu.name,how)
	}
}

function doWriteDivs(Menus) {
	var len = Menus.length
	//len = 1
	for (var i=0;i<len;i++) {
		var m = Menus[i]
		
		//1
		m.showImage()
		documentwrite(gX)
		gX=""
	}
}

function doWriteMaps(Menus) {
	var len = Menus.length
	//len = 1
	for (var i=0;i<len;i++) {
		var m = Menus[i]
		
		//2
		m.showMap()
		gDoc2 += gY
		gY=""
	}
}

// ------------------------------------------------------------------------- //

// ------------ //
// -- PUBLIC -- //
// ------------ //

// These 4 functions are called directly from the HTML
//
// - mapOver()
// - onBodyLoad()
// - createDHTMLMenus()
// - writeRandomImage()

//
// (onMouseOver and onMouseOut handler)
//
function mapOver(name,which,on,presel,pre) {
	if (V3) return;
	var divname = divprefix + name; // the one with image2!
	
	//
	// it's a flag and also the value.
	// so... if zero, pass "0", not 0
	//
	if (presel) {
		divname += "SEL"
		var preselected = Number(presel)
	} else
		divname += "ROLL"

	var menuobj = eval(menuprefix+name) // this is a JS-defined object!  Not in the DOM.
	var linkobj = menuobj.links[which-1]

	if (menuobj.visible == "menu") {
		if (!on) divTimeout(name)
		if (on) removeTimeout(gTID)
	}
	
	var subs = menuobj.submenus;

	//if (on && !pre && subs && linkobj && (linkobj.url == "#")) window.status = ""
	
	//if (window.status.indexOf("#")>=0) window.status = ""

	if (on && subs) {
		var except = menuobj.links[which-1]; // numbering starts at zero
		if (except)
			hideSubs(menuobj,except.submenuname) // should be timed...
	}
	
	if (!on && !gTID && linkobj && linkobj.submenuopt) {
		//
		// only kill the submenu if there is no pending timeouts
		//
		var thelink = linkobj.submenuopt
		divTimeout(thelink)
	}
	
	if (on) gMenu = ""
		
	//	if (on && gMenu) {
	//		showhideMenuBoth(gMenu,"hide")
	//		gMenu = ""
	//	}

	var submenu = linkobj.submenuname
	if (!pre && submenu && on) {
	
		// show the associated SubMenu	
		showhideMenu(submenu,"show")
		// and set the timeout!
		gMenu = submenu
		//divTimeout(submenu)
	}
	if (!on) gMenu = ""
	
	if (presel) menuobj.preselect = 1; // set the flag!
	
	if (!VER4) return; // this should never happen
	
	var which1 = MM_findObj(divname)

	if (!on) { 
		//if (divname.indexOf("SEL")>0) alert(divname)
		setVis(which1,"hide");	// whichEl.visibility = "hidden"; 
		return
	}

	clLeft = linkobj.left // arPopups[divname][which][0];
	clRight = linkobj.right  // arPopups[divname][which][1];
	clTop = linkobj.top
	clBot = linkobj.bottom
	
	if (preselected > -1) clLeft = preselected;

	//if (!which1) alert(divname)

	if (IE4) 
		whichEl = which1.style; 	// whichEl = document.all.elMenuOver.style;
	else 
		whichEl = which1;			// document.elMenu.document.elMenuOver

	//if (!whichEl) alert(divname)
	
	if (NN4) {
			whichEl.clip.left = clLeft;
			whichEl.clip.top = clTop;
			whichEl.clip.right = clRight;
			whichEl.clip.bottom = clBot;
			
	} else {
			if (NN6DOM) {
				//
				// clipping works THE OPPOSITE in NN6 (only affects the Right and Bottom)
				// only these need adjusting...
				//
				clRight = menuobj.width - clRight;
				clBot = menuobj.height - clBot; // 0 in the case of horizontal tiled...
			}
			whichEl.clip = "rect(" + clTop + "px, " + clRight + "px, " + clBot + "px, " + clLeft + "px)";
	}
	setVis(which1,"show") //whichEl.visibility = "visible" 
	//if (window.status.indexOf("#")>=0) window.status = ""
}

//
// if there are more than 2 sets of menus involved this will need to
// be modified.
//
function onBodyLoad() {
	if (NN4 && NN4off) return; // debug
	// we wait to do these after body has loaded
	replaceRandomImage()
	if (gMenus) doDefaultVisibility(gMenus)
	if (lMenus)	doDefaultVisibility(lMenus)
	if (gMenus) defaultTopSelection(thisurl)
	if (lMenus) defaultLeftSelection(thisurl)
}

//top - unused
function createDHTMLMenus() {
	// do nothing...
	//document.write("\n\n<div style=\"position:absolute;visibility:visible;top:0px;left:0px;height:1px;width:1px;\"></div>\n\n")
}

//bottom
function doHTMLMenus() {
	if (NN4off) return;  // turn off NN4 menus

	var xx = "" 
	//if (NN4) xx = "\n\n<layer top=0 left=0 style=\"position:absolute;visibility:visible;top:0px;left:0px;height:1px;width:1px;\"></layer>\n\n";
	
	xx = "\n\n<div style=\"position:absolute;visibility:visible;top:0px;left:0px;height:1px;width:1px;\"></div>\n\n" // NN4 bug needs this
	
	var yy = "";
	
	if (lMenus) {
		doWriteDivs(lMenus)
		xx+= gDoc;
		
		doWriteMaps(lMenus)
		yy+= gDoc2
		
		gDoc = ""
		gDoc2 = ""
	}

	if (gMenus) {
		doWriteDivs(gMenus)
		xx+= gDoc
		doWriteMaps(gMenus)
		yy+= gDoc2
		gDoc = ""
		gDoc2 = ""
	}

	//var re1 = />/g
	//xx = xx.replace(re1,">\n")
	//yy = yy.replace(re1,">\n")
	
	document.write(xx)
	document.write(yy);
}


//
// called by onLoad()
//
function replaceRandomImage() {
	var randomimage = document.ri
	if (randomimage) randomimage.src = writeRandomImage(1)
}

//
// random image routine (unrelated to the menus)
//
function writeRandomImage(giveURL) {
	var h = 63
	var w = 108
	
	var path = path_main+"images/randoms/" // path_main global

	var names = "randomImage1.gif,randomImage2.gif,randomImage3.gif,randomImage4.gif,randomImage5.gif"
	
	names = names.split(",")

	// pick one
	var r = Math.round(Math.random()*(names.length-1))
	r = names[r]

	// dump it
	var res = path+r
	var alt = "" // "image "+r
	if (giveURL) return res;
	res = "<img alt=\""+alt+"\" border=0 height="+h+" width="+w+" src="+res+">";
	document.write(res)
	return "";
}

//open window
function openWin(url, name) {
	var winProperties = "resizable,toolbar=0,width=350,height=325"
  popupWin = window.open(url,name,winProperties);
}

function launchSubs(inname) {
	xpos=(screen.width - 615) / 2 ;
	ypos=(screen.height - 550) / 2 ;

	newwin=window.open(inname,"bdec_subs","width=615,height=550,toolbar=no,resizable=0,scrollbars,left="+xpos+",top="+ypos);
	if (newwin !=null) 
		newwin.focus()
}

function launchFreex(inname) {
	xpos=(screen.width - 610) / 2 ;
	ypos=(screen.height - 550) / 2 ;

	newwin=window.open(inname,"rmmx_free","width=750,height=550,toolbar=no,resizable=1,scrollbars,left="+xpos+",top="+ypos);
	if (newwin !=null) 
		newwin.focus()
}

