/*
NAVCODE.JS ------------ 

3/22/01 DJM
	Modified:
		showMenu(...)		Modified to allow for the showing of arrows in w3c and IE, (arrow currently not working in Netscape)
		clearMenus(...)		Modified to allow for the hiding of arrows in w3c and IE, (arrow currently not working in Netscape)
		startTracking(...)	Disabled call to nsTrack() due to errors that wouldn't allow lower level nav levels to open.
3/30/01 DJM
	Modified:
		clearMenus(...)		Modified to walk the menu array from max to min in order to ensure array is cleared for all 
							items above the current opened level.


*/
// array of level info where for each level, index 0 is the open layer name or 0 and index
// 1 is the number of the label (link) that is currently selected.
levels = new Array();
levels[0] = new Array("m0",0);
levels[1] = new Array(0,0);
levels[2] = new Array(0,0);
levels[3] = new Array(0,0);
levels[4] = new Array(0,0);
var timeout;
var arrowsrc = "/images/arrow.gif";
var blanksrc = "/images/blank.gif";

linkcounters = new Array(0,0,0,0,0); // keep track of which layers are being produced next for given level. for links.
layercounters = new Array(0,0,0,0,0); // keep track of which layer is being produced now.
var mwidth = 140;
var content = "";
var explorer;
var w3c;
var iemac;
var NN4;
var curLayer; // for Netscape startTracking()
var agt=navigator.userAgent.toLowerCase(); //for client sniffing

// SNIFF Browser
if (document.all) {
	if ((agt.indexOf("mac")!=-1) &&
		((agt.indexOf("ppc")!=-1) || 
		(agt.indexOf("powerpc")!=-1)) &&
		(document.getElementById)) {
		explorer = 1;
		iemac = 1;
	}
	else {
		explorer = 1;
	}
}
else if (document.getElementById) {
	w3c = 1;
}
NN4 = (document.layers) ? true : false; //test for Netscape 4

// clear timeout, clear menus on levels above and on level for menu to show, show menu layer and arrow image for it's parent link.
// In Netscape link is the click event, in IE and w3c link is the object that was clicked (<a>)
function showMenu(name, level, labelindex, event, link) {

	if(explorer) {clearTimeout(timeout);}  //IE calls onMouseOut for layer when mouseOver link in that layer.

	if(levels[level][0] != 0 && levels[level][0] != name) {
		clearMenus(level);
	}		
//alert(name+","+level+","+labelindex+","+event+","+link);
	if(name) {

		if(explorer) {
			document.images["" + (levels[level-1][0]) + "i" + labelindex].src = arrowsrc;

			if(
			//!(iemac) || 
			document.getElementById(name).offsetTop < 100) { //alert(link.offsetTop);
				document.getElementById(name).style.top = getTopValue(link);
			}

			document.getElementById(name).style.visibility = "visible";
		}
		else if(w3c) {
			document.images["" + (levels[level-1][0]) + "i" + labelindex].src = arrowsrc;
			document.getElementById(name).style.top = event.clientY;
			document.getElementById(name).style.visibility = "visible";
		}
		else {
			if(level == 1) {document.images["m0i" + labelindex].src = arrowsrc;}
			else {
				//--- at this point, Netscape will not handle arrow images below those already painted
				//	... if solution exist, it belongs here
				//		-DJM 3/22/01
			}
			document.layers[name].pageY = event.pageY;  
			document.layers[name].visibility = "show";
			if (NN4){
				//startTracking(name);
			}
		}
	}
	levels[level][0] = name;
	levels[level-1][1] = labelindex;
	if(window.event) {window.event.returnValue = false;}

}

// hide all menus above and including startlevel removing arrow gifs where used.
function clearMenus(startlevel) {

	for(var i=levels.length-1; i>=startlevel; i--) {
		if (levels[i][0]) { //alert(levels[i][0]);
			if(explorer) {
				// hide menues ...
				document.all[levels[i][0]].style.visibility = "hidden";
				// hide menu arrows (including parent) so subsequent openings don't contain arrow gif when opened
				document.images[(levels[i-1][0]) + "i" + (levels[i-1][1])].src = blanksrc;
			}
			else if(w3c) {
				// hide menues ...
				document.getElementById(levels[i][0]).style.visibility = "hidden";
				// hide menu arrows (including parent) so subsequent openings don't contain arrow gif when opened
				document.images[(levels[i-1][0]) + "i" + (levels[i-1][1])].src = blanksrc;
			}
			else {
				// as of 3/22/01, only the menus working, images above level 0, so only need to hide menus -DJM
				document.layers[levels[i][0]].visibility = "hide";
				// --- if a solution is found for Netscape arrows, the hide for levels above 0 goes here -DJM
				//document.images[(levels[i-1][0]) + "i" + (levels[i-1][1])].src = blanksrc;
			}
			levels[i][0] = 0;
			levels[i][1] = 0;
			
		}
	}
	if(startlevel == 1) {
		// all menus will be closed, so clear arrow from level 0.
		document.images[(levels[0][0]) + "i" + (levels[0][1])].src = blanksrc;
		if(explorer) { // NOP
		}
		else if(w3c) { // NOP
			}else { //Netscape...
			//stopTracking();
		}
	}
}


// For Netscape only - start tracking mouse to see if need to scroll to show a menu that 
// doesn't fit in visible window.
function startTracking(name) {
	window.captureEvents(Event.MOUSEMOVE);
	curLayer = document.layers[name];
	//window.onmousemove=nsTrack(curLayer);  // nsTrack not functioning as of 3/22/01 -DJM
}

// Track all mouse movement in Netscape and watch for moving on layer outside of window area.
function nsTrack(e) {
	setTimeout("",500);
	if((e.pageY >= window.pageYOffset + window.innerHeight - 10) && (window.pageYOffset + window.innerHeight < curLayer.pageY + curLayer.clip.bottom + 10)) {
		window.scrollBy(0,5);
	}
	else if((e.pageY <= window.pageYOffset + 10) && (window.pageYOffset > curLayer.pageY - 10)) {
		window.scrollBy(0,-5);
	}
	return false;
}

// Track all mouse movement in IE and watch for moving on layer outside of window area.
function ieTrack(el) {
	setTimeout("",500);
	if(event.clientY >= document.body.clientHeight - 10) {
		window.scrollBy(0,5);
	}
	else if(event.clientY <= 10) {
		window.scrollBy(0,-5);
	}
	return false;
}

// For Netscape only - stop tracking the mouse movements.
function stopTracking() {
	window.releaseEvents(Event.MOUSEMOVE); 
	window.onmousemove=null;
}

// Add up the offsetTop values of all elements above the given one until the body element to get the 
// y coordinate of this element on the page.  (style.top)
function getTopValue(el) {
	var top = 0;
	var tell = "";

	if(iemac) {   top=11;
		// levels 1+
		if(el.parentNode.nodeName == "DIV") {
			while(el.nodeName != "BODY") {
				if(el.nodeName == "DIV"){top += el.offsetTop;}		
				//tell += el.nodeName + "=" + el.offsetTop + "\n";
				el = el.parentNode;
				
			}//alert(tell);
		}
		// level 0
		else {
			while(el.nodeName != "BODY") {
				if(el.nodeName != "TD" && el.nodeName != "TR" && el.nodeName != "P"){top += el.offsetTop;}		
				//tell += el.nodeName + "=" + el.offsetTop + "\n";
				el = el.parentNode;
			}
			//window.alert(top);
		}//window.alert(top);
	} else {
		// levels 1+
		if(el.parentNode.nodeName == "DIV") {
			while(el.nodeName != "BODY") {
				if(el.nodeName == "DIV"){top += el.offsetTop;}	
				//tell += el.nodeName + "=" + el.offsetTop + "\n";
				el = el.parentNode;
				
			}//alert(tell);
		}
		// level 0
		else {
			while(el.nodeName != "BODY") {
				if(el.nodeName != "TD"){top += el.offsetTop;}		
				el = el.parentNode;
			}
		}//window.alert(top);
	}

	return top;
}

function onMenu(name, level) {
	//alert("onMenu "+name);
	clearTimeout(timeout);
}

function startClear(level) {
	timeout = setTimeout("clearMenus(1);",500);
}


//****************************************
// Open a new layer. Specify the level that the new layer will be on and the index of the 
// calling link in it's layer (in order to hide or show that link's arrow image).
function openLayer(level, index) {
	if(index) {this.name = 'm' + level + "_" + index;}
	else {this.name = nextLayer(level);}
	//this.left = level * (mwidth - 8);
	this.left = level * (mwidth - 8);
	this.level = level;
	this.linkcount = 0;

	this.al = al;
	this.cl = cl;

	if(explorer || w3c) {
		print('<div id="' + this.name + '" onmousemove="ieTrack(this)" class="cmenu" style="position: absolute; z-index: 10; top: 1; left: ' + this.left + '; width: ' + mwidth + '; visibility: hidden; background-color: #FFFFCC"');
	}
	else {
		print('<layer name="' + this.name + '" class="cmenu" left="' + this.left + '" width="' + mwidth + '" visibility="hide" z-index="10" bgcolor="#FFFFCC"');
	}
	print(' onMouseOver="onMenu(\'' + this.name + '\',' + this.level + '); return false;" onMouseOut="startClear(); return false;">');
	print('<table class="bordertableSub" width="' + mwidth + '" border="0" cellspacing="0" cellpadding="1">');
	print('<tr><td><table class="bgtable" width="100%" border="0" cellspacing="0" cellpadding="2"><tr><td>');
}

// Add a new link to the layer calling this method.  Give it the specified label and url and specify
// whether it has a menu or not.
function al(label, url, hasMenu) {

	if(hasMenu) {
		print('<div class="csublink">');
		print('<a href="' + url + '" class="csublink" onMouseOver="showMenu(\'' + nextSub(this.level) + '\',' + eval(this.level+1) + ',' + this.linkcount + ',event,this); return false;"');
	}
	else {
		print('<div class="csublink">');
		print('<a href="' + url + '" class="csublink" onMouseOver="showMenu(0,' + eval(this.level+1) + ',0); return false;"');
	}
	print('>' + label + '</a><img name="' + this.name + 'i' + this.linkcount + '" src="' + blanksrc + '" width="7" height="9" alt=""></div>');
	print('<div><img src="/images/trans1x1.gif" width="15" height="5" border="0" alt=""></div>');
	this.linkcount++;
}

// close the layer calling this method
function cl() {
	print('</td></tr></table></td></tr></table>');
	if(explorer || w3c) {print('</div>');}
	else {
		print('</layer>');
		}
}

function nextSub(level) {
	subname = 'm' + eval(level+1) + '_' + linkcounters[level+1];
	linkcounters[level+1]++;
	return subname;
}

function nextLayer(level) {
	layername = 'm' + level + "_" + layercounters[level];
	layercounters[level]++;
	return layername;
}

// append the data to the content string.  Mostly for dev purposes so could output all data at once
// to see final html source when browser only showed the js methods in the page source.
function print(stuff) {
	content += (stuff + '\n');
}

function getContent() {
	return content;
}



