//browser detection

if (document.all) {n=0;ie=1;ns6=0;fShow="visible";fHide="hidden";}//ie
else if (document.getElementById){n=0;ie=0;ns6=1;fShow="";fHide="hidden";}//ns6
else if (document.layers) {n=1;ie=0;ns6=0;fShow="show";fHide="hide";}//ns4

//Initialize variables used in displaySubMenu function
rightX =0;
leftY = 0;
leftX = 0;

//menu contents
function showIntro(){

//addItem(id, text, hint, location, alternatviecLocation);
menu=new Menu();
menu.addItem("Menuid", "CIRCA Course Menu", null, null, null);

//addSubItem(idParent, text, hint, location);
menu.addSubItem("Menuid", "Course Introduction",                "CIRCA/WebPages/index.htm",                            "../WebPages/index.htm"); 
menu.addSubItem("Menuid", "Carbon Rich Products",               "CIRCA/WebPages/Circa_2_Carbon_Rich_Products.htm",     "../WebPages/Circa_2_Carbon_Rich_Products.htm");
menu.addSubItem("Menuid", "Pulvierized Coal",                   "CIRCA/WebPages/Circa_3_Puverized_Coal.htm",           "../WebPages/Circa_3_Puverized_Coal.htm");
menu.addSubItem("Menuid", "Sustainable Development",            "CIRCA/WebPages/Circa_5_Sustainable_Development.htm",  "../WebPages/Circa_5_Sustainable.htm");
menu.addSubItem("Menuid", "Fly Ash in Portland Cement Concrete",    "CIRCA/WebPages/Circa_6_Use_in.htm",               "../WebPages/Circa_6_Use_in.htm");
menu.addSubItem("Menuid", "Agricultural and Other Uses",        "CIRCA/WebPages/Circa_8_Agricuatural_a.htm",           "../WebPages/Circa_8_Agricuatural_a.htm");
menu.addSubItem("Menuid", "Highway Use",                        "CIRCA/WebPages/Circa_9_Highway_Use.htm",              "../WebPages/Circa_9_Highway_Use.htm");
menu.addSubItem("Menuid", "Aggregates from Coal Combustion",    "CIRCA/WebPages/Circa_10_Aggregates_a.htm",            "../WebPages/Circa_10_Aggregates_a.htm");
menu.addSubItem("Menuid", "Other Uses",                         "CIRCA/WebPages/Circa_11_Other_Use_a.htm",             "../WebPages/Circa_11_Other_Uses_a.htm");
menu.addSubItem("Menuid", "Beneficiation",                      "CIRCA/WebPages/Circa_13_Beneficiation.htm",           "../WebPages/Circa_13_Beneficiation.htm");
menu.addSubItem("Menuid", "Current Research Needs",             "CIRCA/WebPages/Circa_15_Current.htm",                 "../WebPages/Circa_15_Current.htm");
menu.addSubItem("Menuid", "References",                         "CIRCA/WebPages/Circa_41_References.htm",              "../WebPages/Circa_41_References.htm");

menu.showMenu();
}

//create the menu
function Menu(){
this.addItem = addItem;
this.addSubItem=addSubItem;
this.showMenu=showMenu;
this.bgColor="white";
this.mainPaneBorder=0;
this.subMenuPaneBorder=0;
this.subMenuPaneWidth=180;
lastMenu=null;

HTMLstr = "";
	HTMLstr += "<!-- MENU PANE DECLARATION BEGINS -->\n";
	HTMLstr += "\n";
	HTMLstr += "<div id='MainTable' style='position:absolute; left:30; top:185'>\n";
	HTMLstr += "<table width='150' cellpadding='0' cellspacing='0' bgcolor='"+this.bgColor+"' border='"+this.mainPaneBorder+"'>\n";
	HTMLstr += "<tr>";
	if (n) HTMLstr += "<td>&nbsp;";
	HTMLstr += "<!-- MAIN MENU STARTS -->\n";
	HTMLstr += "<!-- MAIN_MENU -->\n";
	HTMLstr += "<!-- MAIN MENU ENDS -->\n";
	if (n) HTMLstr += "</td>";
	HTMLstr += "</tr>\n";
	HTMLstr += "</table>\n";
	HTMLstr += "\n";
	HTMLstr += "<!-- SUB MENU STARTS -->\n";
	HTMLstr += "<!-- SUB_MENU -->\n";
	HTMLstr += "<!-- SUB MENU ENDS -->\n";
	HTMLstr += "\n";
 	HTMLstr+= "</div>\n";
	HTMLstr += "<!-- MENU PANE DECALARATION ENDS -->\n";
}

//add drop-down container
function addItem(idItem, text,hint,location,altLocation){
var Lookup = "<!-- ITEM "+idItem+" -->";
	if (HTMLstr.indexOf(Lookup) != -1){
		alert(idParent + " already exist");
		return;
	}

var MENUitem = "";
	MENUitem += "\n<!-- ITEM "+idItem+" -->\n";
	if (n){
		MENUitem += "<ilayer name="+idItem+">";
		MENUitem += "<a href='.' class=clsMenuItemNS onmouseover=\"displaySubMenu('"+idItem+"')\" onclick=\"return false;\">";
		MENUitem += "|&nbsp;";
		MENUitem += text;
		MENUitem += "</a>";
		MENUitem += "</ilayer>";
	}
	else{//IE or NS6
		MENUitem += "<td>\n";
		MENUitem += "<div id='"+idItem+"' style='position:relative; left:50%; font: "+this.menuFont+";'>\n";
		MENUitem += "<a ";
		
		if(ie)
			MENUitem += "class=clsMenuItemIE ";
		if(ns6)
			MENUitem += "class=clsMenuItemNS6 ";
			
		if (hint != null)
			MENUitem += "title='"+hint+"' ";
		if (location != null){
			MENUitem += "href='"+location+"' ";
			MENUitem += "onmouseover=\"hideAll()\" ";
		}
		else{
			if (altLocation != null)
				MENUitem += "href='"+altLocation+"' ";
			else
				MENUitem += "href='.' ";
			MENUitem += "onmouseover=\"displaySubMenu('"+idItem+"')\" ";
			MENUitem += "onclick=\"return false;\" "
		}
		MENUitem += ">";
		MENUitem += text;
		MENUitem += "</a>\n";
		MENUitem += "</div>\n";
		MENUitem += "</td>\n";
	}
	MENUitem += "<!-- END OF ITEM "+idItem+" -->\n\n";
	MENUitem += "<!-- MAIN_MENU -->\n";

	HTMLstr = HTMLstr.replace("<!-- MAIN_MENU -->\n", MENUitem);
}

//add sub-menu items to applicable drop-down container
function addSubItem(idParent, text, hint, location){
var MENUitem = "";
	Lookup = "<!-- ITEM "+idParent+" -->";
	if (HTMLstr.indexOf(Lookup) == -1){
		alert(idParent + " not found");
		return;
	}
	Lookup = "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->";
	if (HTMLstr.indexOf(Lookup) == -1){
		if (n){
			MENUitem += "\n";
			MENUitem += "<layer id='"+idParent+"submenu' visibility=hide bgcolor='"+this.bgColor+"'>\n";
			MENUitem += "<table border='"+this.subMenuPaneBorder+"' bgcolor='"+this.bgColor+"' width="+this.subMenuPaneWidth+">\n";
			MENUitem += "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
			MENUitem += "</table>\n";
			MENUitem += "</layer>\n";
			MENUitem += "\n";
		}
		else{//IE or NS6
			MENUitem += "\n";
			MENUitem += "<div id='"+idParent+"submenu' style='position:absolute; background-color: "+this.bgColor+"; visibility: hidden; width: "+this.subMenuPaneWidth+"; top: -300;'>\n";
			MENUitem += "<table border='"+this.subMenuPaneBorder+"' bgcolor='"+this.bgColor+"' width="+this.subMenuPaneWidth+">\n";
			MENUitem += "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
			MENUitem += "</table>\n";
			MENUitem += "</div>\n";
			MENUitem += "\n";
		}
		MENUitem += "<!-- SUB_MENU -->\n";
		HTMLstr = HTMLstr.replace("<!-- SUB_MENU -->\n", MENUitem);
	}

	Lookup = "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
	if (n)  MENUitem = "<tr><td><a class=clsMenuItemNS title='"+hint+"' href='"+location+"'>"+text+"</a><br></td></tr>\n";
	else //IE or NS6
		if(ie)
			MENUitem = "<tr><td><a class=clsMenuItemIE title='"+hint+"' href='"+location+"'>"+text+"</a><br></td></tr>\n";
		if(ns6)
			MENUitem = "<tr><td><a class=clsMenuItemNS6 title='"+hint+"' href='"+location+"'>"+text+"</a><br></td></tr>\n";
	MENUitem += Lookup;
	HTMLstr = HTMLstr.replace(Lookup, MENUitem);

}

//write out main menu bar
function showMenu(){
document.writeln(HTMLstr);
}

//show submenu, and then calculate its x and y coordinates
function displaySubMenu(idMainMenu){
var menu;
	var submenu;
	if (n){
		submenu = document.layers[idMainMenu+"submenu"];
		if (lastMenu != null && lastMenu != submenu) hideAll();
		submenu.left = document.layers[idMainMenu].pageX;
		submenu.top  = document.layers[idMainMenu].pageY + document.layers[idMainMenu].clip.height;
		submenu.visibility = fShow;
		submenu.zIndex=10
		leftX  = document.layers[idMainMenu+"submenu"].left;
		rightX = leftX + document.layers[idMainMenu+"submenu"].clip.width;
		leftY  = document.layers[idMainMenu+"submenu"].top+document.layers[idMainMenu+"submenu"].clip.height;
	} else if (ns6) {
	
	menu = document.getElementById(idMainMenu);		
		submenu = document.getElementById(idMainMenu+"submenu");
		if (lastMenu != null && lastMenu != submenu) hideAll();
		submenu.style.left = calculateSumOffset(menu, 'offsetLeft');
		submenu.style.top  = document.getElementById(idMainMenu).offsetHeight;
		submenu.style.visibility = fShow;
		leftX  = calculateSumOffset(menu, 'offsetLeft')+35;
		rightX =leftX + document.getElementById(idMainMenu+"submenu").offsetWidth;
		leftY  = document.getElementById(idMainMenu+"submenu").offsetHeight + window.pageYOffset + 200;
	
	} else if (ie) { 
		menu = eval(idMainMenu);
		submenu = eval(idMainMenu+"submenu.style");
		if (lastMenu != null && lastMenu != submenu) hideAll();			
		submenu.left = calculateSumOffset(menu, 'offsetLeft');
		submenu.top  = document.all[idMainMenu].offsetHeight;
		submenu.visibility = fShow;
		leftX  = document.all[idMainMenu+"submenu"].style.posLeft+35;
		rightX = leftX + document.all[idMainMenu+"submenu"].offsetWidth;
		leftY  = document.all[idMainMenu+"submenu"].style.posTop+document.all[idMainMenu+"submenu"].offsetHeight +200;
	}
	lastMenu = submenu;
}

//hide the submenu
function hideAll(){
	if (ns6){
		if (lastMenu != null) 
			{lastMenu.style.visibility = fHide;}
	}else{
		if (lastMenu != null) 
			{lastMenu.visibility = fHide;}
	}		
}

//used to calculate position of a submenu
function calculateSumOffset(idItem, offsetName){
	var totalOffset = -35;
	var item = eval('idItem');
	do{
		totalOffset += eval('item.'+offsetName);
		item = eval('item.offsetParent');
	} while (item != null);
	return totalOffset;
}

//close menu on mouse out of menu containor
function updateIt(e){
	if (ns6){
		var x = e.pageX;
		var y = e.pageY;
		if (x > rightX || x < leftX) hideAll();
		else if (y > leftY) hideAll();
	}
	 else if (ie){
		var x = window.event.clientX;
		var y = window.event.clientY;
		if (x > rightX || x < leftX) hideAll();
		else if (y > leftY) hideAll();
	}
	else if (n){
		var x = e.pageX;
		var y = e.pageY;
		if (x > rightX || x < leftX) hideAll();
		else if (y > leftY) hideAll();
	}
}

//set page to hide menus on a mouse click or on mouseout of menu container
if (ns6)
{
	document.body.onclick=hideAll;
	document.body.onmousemove=updateIt;
}
else if (ie)
{
	document.body.onclick=hideAll;
	document.body.onscroll=hideAll;
	document.body.onmousemove=updateIt;
}
else if (n)
{
	document.onmousedown=hideAll;
	window.captureEvents(Event.MOUSEMOVE);
	window.onmousemove=updateIt;
}

