//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 showToolbar(){
//addItem(id, text, hint, location, alternativeLocation);
	menu = new Menu();
	menu.addItem("Home1ID", "Home", "Main LPC Holdings page","index.html", null);
	menu.addItem("ElementsID", "Heating Elements", "Industrial heating elements",  null, null);
	menu.addItem("WireWoundID", "Wire wound Resistors", "'Bremco' wire wound resistors",  null, null);
	menu.addItem("PowerID", "Power Resistors", "High power wire wound resistors",  null, null);	
	menu.addItem("TorostatsID", "Torostats", "Power rheostats",  null, null);
	menu.addItem("Home2ID", "Home", "Main LPC Holdings page","index.html", null);

// addSubItem(idParent, text, hint, location);	
	menu.addSubItem("ElementsID", "About LPC Elements", "Overview",  "basehe.html");
	menu.addSubItem("ElementsID", "Mikram", "Mikram&reg; nozzle heaters",  "mikram.html");
	menu.addSubItem("ElementsID", "Mica", "Mica range",  "mica.html");
	menu.addSubItem("ElementsID", "Miflex", "Flexible ceramic knuckle",  "miflex.html");
	menu.addSubItem("ElementsID", "Megnam", "High efficiency ceramic",  "megnam.html");
	menu.addSubItem("ElementsID", "Lo-Cost", "Inexpensive nozzle heaters",  "lo-cost.html");
	menu.addSubItem("ElementsID", "Cartridge", "High watt density cartridge heaters",  "cartridge.html");
	menu.addSubItem("ElementsID", "Cast", "Cast range",  "cast.html");
	menu.addSubItem("ElementsID", "Tubular Swaged", "Tubular elements",  "tubular.html");
	menu.addSubItem("ElementsID", "Air Heating", "Air & space heaters",  "airheating.html");
	menu.addSubItem("ElementsID", "Immersion", "Immersible heaters",  "immersion.html");
	menu.addSubItem("ElementsID", "Circulation/Outflow", "Circulation & outflow heaters",  "circoutflow.html");
	menu.addSubItem("ElementsID", "Infrared", "Infrared range",  "infrared.html");
	menu.addSubItem("ElementsID", "Process Ovens", "Industrial process ovens",  "process.html");
	menu.addSubItem("ElementsID", "Drum Units", "Drum mounted heaters",  "drumunits.html");
	menu.addSubItem("ElementsID", "Ancillaries", "Extras & ancillaries",  "ancillaries.html");
	menu.addSubItem("ElementsID", "Cape Insulation", "Insulation products",  "capeinsulation.html");

	menu.addSubItem("WireWoundID", "About Bremco resistors", "Wire woound resistors overview",  "baseww.html");
	menu.addSubItem("WireWoundID", "Miniature Range", "SM-Axial & SSM-Single-ended",  "miniature.html");
	menu.addSubItem("WireWoundID", "NS & NB Ranges", "Silicon coated and open wound resistors",  "nsandnb.html");
	menu.addSubItem("WireWoundID", "Mica based M Range", "Mica based open wound resistors",  "wwmica.html");
	menu.addSubItem("WireWoundID", "HR Range", "Ceramic/brass resistors",  "wwhr.html");
	menu.addSubItem("WireWoundID", "HB Range", "Porcelain resistors",  "wwhb.html");

	menu.addSubItem("PowerID", "Key & Ordering", "Ordering informationi & Key to abbreviations",  "prkeyord.html");
	menu.addSubItem("PowerID", "Round Section", "Round section vitreous enamel & silicon coated",  "prvitround.html");
	menu.addSubItem("PowerID", "Low Wattage", "Low-Wattage vitreous enamel & silicon coated",  "prlowwatt.html");
	menu.addSubItem("PowerID", "Oval Section", "Oval section vitreous enamel & silicon coated",  "prvitoval.html");
	menu.addSubItem("PowerID", "Values & Ratings", "Resistance values and duty ratings",  "prvalues.html");
	menu.addSubItem("PowerID", "Mounting Styles", "Resistor mounting information",  "prmount.html");
	menu.addSubItem("PowerID", "Anti-Condensation Heaters", "Anti-condensation heating units",  "pranticon.html");
	
	menu.addSubItem("TorostatsID", "Introduction", "Overview of Torostat range",  "tsintro.html");
	menu.addSubItem("TorostatsID", "Ratings", "Ratings & Ohmic Values",  "tsratings.html");
	menu.addSubItem("TorostatsID", "Temperature Curves", "Tempearture Ratings & Curves",  "tscurves.html");
	menu.addSubItem("TorostatsID", "Open Single", "Open Single Torostats - Dimensions",  "tssingle.html");
	menu.addSubItem("TorostatsID", "Open Ganged", "Open Double & Triple Ganged Torostats - Dimensions",  "tsgang.html");
	menu.addSubItem("TorostatsID", "Enclosed", "Enclosed Front-of-board & Back-of-board Torostats - Dimensions",  "tsenc.html");
	menu.addSubItem("TorostatsID", "Special Features", "Specialised Torostats",  "tsspecial.html");
	menu.addSubItem("TorostatsID", "Knobs and Dials", "Knobs & Dials for use with Torostats",  "tsknob.html");

	menu.showMenu();
}

//create the menu
function Menu(){
	this.addItem    = addItem;
	this.addSubItem = addSubItem;
	this.showMenu   = showMenu;
	this.bgColor     = "green";	
	this.mainPaneBorder = 1;
	this.subMenuPaneBorder = 1;
	this.subMenuPaneWidth = 150;
	lastMenu = null;
	
	HTMLstr = "";
	HTMLstr += "<!-- MENU PANE DECLARATION BEGINS -->\n";
	HTMLstr += "\n";
	HTMLstr += "<div id='MainTable' style='position:relative'>\n";
	HTMLstr += "<table width='100%' 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; 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 containor
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 + 4;
		submenu.style.visibility = fShow;
		leftX  = calculateSumOffset(menu, 'offsetLeft');
		rightX =leftX + document.getElementById(idMainMenu+"submenu").offsetWidth;
		leftY  = document.getElementById(idMainMenu+"submenu").offsetHeight + window.pageYOffset + 4;
	
	} 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 + 4;
		submenu.visibility = fShow;
		leftX  = document.all[idMainMenu+"submenu"].style.posLeft;
		rightX = leftX + document.all[idMainMenu+"submenu"].offsetWidth;
		leftY  = document.all[idMainMenu+"submenu"].style.posTop+document.all[idMainMenu+"submenu"].offsetHeight + 4;
	}
	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 = 0;
	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){
	var x;
	var y;
	if (ns6){
		x = e.pageX;
		y = e.pageY;
		if (x > rightX || x < leftX) hideAll();
		else if (y > leftY) hideAll();
	}
	 else if (ie){
		x = window.event.clientX;
		y = window.event.clientY;
		if (x > rightX || x < leftX) hideAll();
		else if (y > leftY) hideAll();
	}
	else if (n){
		x = e.pageX;
		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 containor
if (ns6)
{
//	document.body.onclick=hideAll;
//	document.body.onmousemove=updateIt;
}
else if (ie)
{
	document.onclick=hideAll;
//	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;
}

