//Global variable to count number of menus created
var menuNum=0;

//Global array to hold all the names menus created
var menuArray = new Array();


//create a menu
b = new menu("browse", 50, 68);
d = new menu("directories", 182, 68);
r = new menu("resources", 313, 68);

//add an item to the menu
//addItem(link, imageOn, imageOff, additional onFunction, additional offFunction)
b.addItem(""+baseDir+"browse/location/index.html",""+baseDir+"images/drpdwn_country_location_on.gif",""+baseDir+"images/drpdwn_country_location_off.gif","","");
b.addItem("#",""+baseDir+"images/drpdwn_separator.gif",""+baseDir+"images/drpdwn_separator.gif","","");
b.addItem(""+baseDir+"browse/subjects/index.html",""+baseDir+"images/drpdwn_subject_on.gif",""+baseDir+"images/drpdwn_subject_off.gif","","");
b.addItem("#",""+baseDir+"images/drpdwn_separator.gif",""+baseDir+"images/drpdwn_separator.gif","","");
b.addItem(""+baseDir+"browse/types/index.html",""+baseDir+"images/drpdwn_type_on.gif",""+baseDir+"images/drpdwn_type_off.gif","","");
b.addItem("#",""+baseDir+"images/drpdwn_separator.gif",""+baseDir+"images/drpdwn_separator.gif","","");
b.addItem(""+baseDir+"browse/partners/index.html",""+baseDir+"images/drpdwn_partner_on.gif",""+baseDir+"images/drpdwn_partner_off.gif","","");
b.addItem("#",""+baseDir+"images/drpdwn_bottom.gif",""+baseDir+"images/drpdwn_bottom.gif","","");

d.addItem(""+baseDir+"directories/leader/index.html",""+baseDir+"images/drpdwn_project_leader_on.gif",""+baseDir+"images/drpdwn_project_leader_off.gif","","");
d.addItem("#",""+baseDir+"images/drpdwn_separator.gif",""+baseDir+"images/drpdwn_separator.gif","","");
d.addItem(""+baseDir+"directories/funder/index.html",""+baseDir+"images/drpdwn_funding_on.gif",""+baseDir+"images/drpdwn_funding_off.gif","","");
d.addItem("#",""+baseDir+"images/drpdwn_bottom.gif",""+baseDir+"images/drpdwn_bottom.gif","","");

r.addItem(""+baseDir+"resources/submit.html",""+baseDir+"images/drpdwn_submit_on.gif",""+baseDir+"images/drpdwn_submit_off.gif","","");
r.addItem("#",""+baseDir+"images/drpdwn_separator.gif",""+baseDir+"images/drpdwn_separator.gif","","");
r.addItem(""+baseDir+"news.html",""+baseDir+"images/drpdwn_news_on.gif",""+baseDir+"images/drpdwn_news_off.gif","","");
r.addItem("#",""+baseDir+"images/drpdwn_separator.gif",""+baseDir+"images/drpdwn_separator.gif","","");
r.addItem(""+baseDir+"resources/events.html",""+baseDir+"images/drpdwn_events_on.gif",""+baseDir+"images/drpdwn_events_off.gif","","");
r.addItem("#",""+baseDir+"images/drpdwn_separator.gif",""+baseDir+"images/drpdwn_separator.gif","","");
r.addItem(""+baseDir+"resources/other_resources.html",""+baseDir+"images/drpdwn_resources_on.gif",""+baseDir+"images/drpdwn_resources_off.gif","","");
r.addItem("#",""+baseDir+"images/drpdwn_bottom.gif",""+baseDir+"images/drpdwn_bottom.gif","","");
//main function that writes the menus
function main(){
	b.writeMenu();
	d.writeMenu();
	r.writeMenu();
}











//Menu Constructor	
function menu(name,x,y){
	//clears item string
	this.itemStr = "";
	//clears html string
	this.topStr = "";
	//initializes itemNumber to 0
	this.itemNumber = 0;
	this.name = name;
	this.x = x;
	this.y = y;
	//increments the number of menus
	menuNum++
	this.topStr = "<DIV id="+this.name+">";
	this.topStr += "<TABLE border=\"0\" cellPadding=\"0\" cellSpacing=\"0\">";
	
	this.addItem = addItem;
	this.writeMenu = writeMenu;
	
	menuArray[menuNum] = this.name

	this.styleStr = "#" +this.name+ " {position: absolute; left:"+this.x+"px; top:"+this.y+"px; visibility: hidden;  z-index:100;}"

	document.write('<style type="text/css">')
	document.write(this.styleStr)
	document.write('</style>')
}

//function to add item
function addItem(linkLocation,imgOnLocation,imgOffLocation,addOnFunc,addOffFunc){
	//increments number of items
	this.itemNumber++;	
	this.itemStr += "<TR><TD><A href=\""+linkLocation+"\" onmouseout=\"offMenuItem(\'"+imgOffLocation+"\',\'"+this.name+this.itemNumber+"\',\'"+this.name+"\');"+addOffFunc+"\" onmouseover=\"onMenuItem(\'"+imgOnLocation+"\',\'"+this.name+this.itemNumber+"\',\'"+this.name+"\');"+addOnFunc+"\"><img name="+this.name+this.itemNumber+" src="+imgOffLocation+" alt=\"\" border=\"0\"></A></TD></TR>";
}

var bottom = "</TABLE></DIV>";
//function to show menu
function writeMenu(){
	document.write(this.topStr);
	document.write(this.itemStr);
	document.write(bottom);
}






/*
	DHTML menu system using images.
*/

//Detect if NS4 or IE
var ns = (document.layers) ? true : false;
var ie = (document.all) ? true : false;

//Detect if Mac
var isMac = (navigator.platform.indexOf('Mac') != -1)  ? true : false;

//Detect if NS6
var ns6 = (navigator.userAgent.indexOf('Mozilla/5.0') != -1) ? true : false;


//Variables for IE DOM
var coll;
var styleObj;

//Create objects for NS DOM & IE DOM
if(document.layers){     
  	 coll = "";
	 styleObj = "";
}else{
     coll = "all.";
     styleObj = ".style";
}

//Timer to show & hide in seconds
var timer = .5;

//Timer ID to clear timed hide of main menus
var timerID = null;

/*
Function for link onmouseover
Onmouseover link - hide all menus, Clear timer for hiding main menus, Clear timer for showing content, if applicable - show menu(from parameter) & hide content else - show content.
*/
function onLink(menu){
			hideAll();
			if(timerID){
				clearTimeout(timerID);
			}
			if(menu){ 
				show(menu);
			}
}

/*
Function for link onmouseout
Onmouseout - set timer to show content,  set timer to hide all menus.
*/
function offLink(){
		timedHide();
}

/*
Function for menu item onmouseover
Onmouseover a menu item of main menu - clear timer for showing content, clear timer for hiding main menus, clear timer for hiding secondary menus, hide all secondary menus, is applicable - show secondary menu(from parameter)
*/
function onMenuItem(iLocation,iName,mName){
	rollMenuOn(iLocation,iName,mName);
	clearTimeout(timerID);
}

/*
Function for menu item onmouseout
Onmouseout a menu item set timer to hide all secondary menus, set timer to hide all main menus, set timer to show content.
*/
function offMenuItem(iLocation,iName,mName){
	rollMenuOff(iLocation,iName,mName);
	timerID = setTimeout("hideAll()",timer*1000);
}


//Function that hides all main & secondary menus
function hideAll(){
	var mainObj;
		for(var i = 1; i<=menuNum; i++){
			mainObj = menuArray[i];
			hide(mainObj);
		}
}

//Function timed hide for all menus
function timedHide(){
		timerID = setTimeout("hideAll()",timer*1000);
}


//Function that makes an object visible
function show(obj){
	   if(ns6){
	   		document.getElementById(obj).style.visibility = "visible";
			document.getElementById(obj).style.display = "";
	   }
	   else{
	   		var theObj = getObject(obj);
	   		theObj.visibility = "visible";
	   }

}

//Function that hides an object
function hide(obj){
		if(ns6){
	   		document.getElementById(obj).style.display = "none";
			document.getElementById(obj).style.visibility = "hidden";
	   	}
		else{
	   		var theObj = getObject(obj);
	   		theObj.visibility = "hidden";
	  	}
}

//Function that takes an object name as an argument and creates the appropriate DOM reference.
function getObject(obj){
     var theObj;
     if(typeof obj == "string"){
          theObj = eval("document." + coll + obj + styleObj);
     }
     else{
          theObj = obj;
     }
     return theObj;
}  


function rollMenuOn(imgOnLoc,imgName,menuName){
	if(ns){
		eval("document."+menuName+".document.images[imgName].src = imgOnLoc");
	}
	else{
		document.images[imgName].src = imgOnLoc;
	}
}

function rollMenuOff(imgOffLoc,imgName,menuName){
	if(ns){
		eval("document."+menuName+".document.images[imgName].src = imgOffLoc");

	}
	else{
		document.images[imgName].src = imgOffLoc;
	}
}
