//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;

//Initialise variable used to store distance of menu from top of screen
menuTop=0;

//menu contents
function showToolbar(thisMenuTop, currentPage, currentSubSent, currentItem){
	
	menuTop=thisMenuTop;

	topNav1img = "topNav1"			
	topNav2img = "topNav2"			
	topNav3img = "topNav3"			
	topNav4img = "topNav4"			
	topNav5img = "topNav5"			
	topNav6img = "topNav6"			
	topNav7img = "topNav7"			
	topNav8img = "topNav8"			
	
	var currentSub = currentPage + "_" + currentSubSent
	switch(currentPage)
	{
		case "index":
				topNav1img = "topNav1_active"			
			break;
		case "profile":
				topNav2img = "topNav2_active"			
			break;
		case "news":
				topNav3img = "topNav3_active"			
			break;
		case "researchReports":
				topNav4img = "topNav4_active"			
			break;
		case "contactUs":
				topNav5img = "topNav5_active"			
			break;
		case "links":
				topNav6img = "topNav6_active"			
			break;
		case "employment":
				topNav7img = "topNav7_active"			
			break;
		case "signup":
				topNav8img = "topNav8_active"			
			break;
	
	}
//addItem(id, text, hint, location, alternativeLocation);
	menu = new Menu();
	menu.addItem("topNav1_id", topNav1img, "0",  "index.cfm", null, 54, 24);
	menu.addItem("topNav2_id", topNav2img, "1",  "profile_introduction.cfm", null, 72, 24);
	menu.addItem("topNav3_id", topNav3img, "1",  "news.cfm", null, 75, 24);	
	menu.addItem("topNav4_id", topNav4img, "1",  "researchReports.cfm", null, 137, 24);
	menu.addItem("topNav5_id", topNav5img, "1",  "contactUs_contactDetails.cfm", null, 100, 24);
	menu.addItem("topNav6_id", topNav6img, "1",  "links_introduction.cfm", null, 64, 24);
	menu.addItem("topNav7_id", topNav7img, "0",  "employment.cfm", null, 95, 24);	
	menu.addItem("topNav8_id", topNav8img, "0",  "signup.cfm", null, 66, 24);

// addSubItem(idParent, text, hint, location);	
	// setup home_id
	// no menu items
	menu.addSubItem("topNav1_id", "", "",  "0", currentSub);

	// setup profile_id

	menu.addSubItem("topNav2_id", "Introduction", "Introduction",  "profile_introduction.cfm", currentSub);
	menu.addSubItem("topNav2_id", "Our Services", "Our Services",  "profile_ourServices.cfm", currentSub);
	menu.addSubItem("topNav2_id", "Reasons for our Services", "Reasons for our Services",  "profile_reasonsForOurServices.cfm", currentSub);
	menu.addSubItem("topNav2_id", "Client Profiles", "Client Profiles",  "profile_clientProfiles.cfm", currentSub);
	menu.addSubItem("topNav2_id", "Types of Real Property", "Types of Real Property",  "profile_typesOfRealProperty.cfm", currentSub);
	menu.addSubItem("topNav2_id", "Plant & Machinery", "Plant & Machinery",  "profile_plantAndMachinery.cfm", currentSub);
	menu.addSubItem("topNav2_id", "Our Directors", "Our Directors",  "profile_ourDirectors.cfm", currentSub);

	// setup news_id
	menu.addSubItem("topNav3_id", "General News", "General News",  "news.cfm?type=3", currentSub);
	menu.addSubItem("topNav3_id", "Listed Property Trust & Syndicates News", "Land Property Trust & Syndicates News",  "news.cfm?type=2", currentSub);
	menu.addSubItem("topNav3_id", "Property News", "Property News",  "news.cfm?type=1", currentSub);



	// setup researchreports_id
	// no menu items
	menu.addSubItem("topNav4_id", "Economic", "Economic Reports",  "researchReports.cfm?type=4", currentSub);
	menu.addSubItem("topNav4_id", "Investment Property Yield Reports", "Investment Property Yield",  "researchReports.cfm?type=5", currentSub);
	menu.addSubItem("topNav4_id", "State-based", "State-based Reports",  "researchReports.cfm?type=6", currentSub);
	menu.addSubItem("topNav4_id", "Property Information", "Property Information Reports",  "researchReports.cfm?type=7", currentSub);
	menu.addSubItem("topNav4_id", "National", "National Reports",  "researchReports.cfm?type=8", currentSub);	

	// setup contactus_id
	menu.addSubItem("topNav5_id", "Contact Details", "Contact Details",  "contactUs_contactDetails.cfm", currentSub);
	menu.addSubItem("topNav5_id", "Quotation / Valuation Order", "Quotation / Valuation Order",  "contactUs_valuationOrderForm.cfm", currentSub);
	menu.addSubItem("topNav5_id", "Research Report Order", "Research Report Order",  "contactUs_researchRequest.cfm", currentSub);

	// setup links_id
	menu.addSubItem("topNav6_id", "Overview", "Overview",  "links.cfm", currentSub);
	menu.addSubItem("topNav6_id", "General Property", "General Property",  "links.cfm?type=1", currentSub);
	menu.addSubItem("topNav6_id", "Banks & Finance", "Banks & Finance",  "links.cfm?type=2", currentSub);
	menu.addSubItem("topNav6_id", "Government", "Government",  "links.cfm?type=3", currentSub);
	menu.addSubItem("topNav6_id", "Legal & Accounting", "Legal & Accounting",  "links.cfm?type=4", currentSub);
	menu.addSubItem("topNav6_id", "Listed Property Trusts & Syndicates", "Listed Property Trusts & Syndicates",  "links.cfm?type=5", currentSub);
	menu.addSubItem("topNav6_id", "News & Information", "News & Information",  "links.cfm?type=6", currentSub);
	menu.addSubItem("topNav6_id", "Builders & Developers", "Builders & Developers",  "links.cfm?type=7", currentSub);

	// setup employment_id
	// no menu items
	menu.addSubItem("topNav7_id", "", "",  "0", currentSub);

	// setup signup_id
	// no menu items
	menu.addSubItem("topNav8_id", "", "",  "0", currentSub);

	menu.showMenu();
}

//create the menu
function Menu(){
	this.addItem    = addItem;
	this.addSubItem = addSubItem;
	this.showMenu   = showMenu;
	this.bgColor     = "#E7E7E7";	
	this.mainPaneBorder = 0;
	this.subMenuPaneBorder = 0;
	this.subMenuPaneWidth = 200;
	this.menuFont = "Arial";
	
	lastMenu = null;
	
	HTMLstr = "";
	HTMLstr += "<!-- MENU PANE DECLARATION BEGINS -->\n";
	HTMLstr += "\n";
	HTMLstr += "<div id='MainTable' >\n";
	HTMLstr += "<table cellpadding='0' cellspacing='0' bgcolor='"+this.bgColor+"' border='"+this.mainPaneBorder+"' >\n";
	HTMLstr += "<tr><td><img src=\"images/topNavGradient.gif\" width=\"10\" height=\"24\" border=\"0\"></td>";
	if (n) HTMLstr += "<td>&nbsp;";
	HTMLstr += "<!-- MAIN MENU STARTS -->\n";
	HTMLstr += "<!-- MAIN_MENU -->\n";
	HTMLstr += "<!-- MAIN MENU ENDS -->\n";
	if (n) HTMLstr += "</td>";
	HTMLstr += "<td><img src=\"images/topNavGradient.gif\" width=\"33\" height=\"24\" border=\"0\"></td></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, itemWidth, itemHeight){
	var Lookup = "<!-- ITEM "+idItem+" -->";
	if (HTMLstr.indexOf(Lookup) != -1){
		alert(idParent + " already exist");
		return;
	}
	var MENUitem = "";
	MENUitem += "\n<!-- ITEM "+idItem+" -->\n";
	
	imageID = idItem.split('_')[0]
	var itemContent = '<img src="images/' + text + '.gif" alt="" name="' + imageID + '"  border="0" width="' + itemWidth + '" height="' + itemHeight + '">';
	
		
	if (n){
		MENUitem += "<ilayer name="+idItem+">";
		MENUitem += "<a href='.' class=clsMenuItemNS";
		//if (hint != "0")
		//{
			MENUitem += " onmouseover=\"displaySubMenu('"+idItem+"')\""; 
		//}
		MENUitem += " onclick=\"return false;\">";
		MENUitem += "|&nbsp;";
		MENUitem += itemContent;
		MENUitem += "</a>";
		MENUitem += "</ilayer>";
	}
	else{//IE or NS6
		MENUitem += "<td>";
		MENUitem += "<div id='"+idItem+"' style='position:relative; font: "+this.menuFont+";'>";
		MENUitem += "<a ";
		
		if(ie)
			MENUitem += "class=clsMenuItemIE ";
		if(ns6)
			MENUitem += "class=clsMenuItemNS6 ";
			
		if (hint != null)
			MENUitem += "title='' ";
		if (location != null){
			MENUitem += "href='"+location+"' ";
			//MENUitem += "onmouseover=\"hideAll("+idItem+")\" ";
			MENUitem += "onmouseover=\"displaySubMenu('"+idItem+"')\" ";
		}
		else{
			if (altLocation != null)
				MENUitem += "href='"+altLocation+"' ";
			else
				MENUitem += "href='.' ";
			//if (hint != "0")
			//{
				MENUitem += "onmouseover=\"displaySubMenu('"+idItem+"')\" ";
			//}
			MENUitem += "onclick=\"return false;\" "
		}
		MENUitem += ">";
		MENUitem += itemContent;
		MENUitem += "</a>";
		MENUitem += "</div>";
		MENUitem += "</td>";
	}
	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, currentSubItem){
	var MENUitem = "";
	Lookup = "<!-- ITEM "+idParent+" -->";
	if (HTMLstr.indexOf(Lookup) == -1){
		alert(idParent + " not found");
		return;
	}
	currentURL=new String(document.location);
	lastSlash=currentURL.lastIndexOf("/");
	scriptName=currentURL.substring(lastSlash+1, currentURL.length);
	//alert(scriptName);
	if 	( 
				( (location.indexOf(currentSubItem) != -1) 	&& (currentSubItem.length) )
				||	
				(location == scriptName)	
			)
	{
		hiliteStyleSuffix = "HILITE"
	}
	else
	{
		hiliteStyleSuffix = ""
	}
	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 cellpadding=\"3\" cellspacing=\"0\" border='"+this.subMenuPaneBorder+"' bgcolor='"+this.bgColor+"' width="+this.subMenuPaneWidth+" style=\"border-top: 1px solid #000;border-left: 1px solid #000;border-right: 1px solid #000;border-bottom: 1px solid #000;\">\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" + hiliteStyleSuffix + " title='"+hint+"' href='"+location+"'>"+text+"</a><br></td></tr>\n";
	else //IE or NS6
		if(ie)
		{
			MENUitem = "<tr><td class='normal" + hiliteStyleSuffix + "'"
			if (hiliteStyleSuffix.length == 0)
			{
			MENUitem += "onmouseover=\"this.className='normalon" + hiliteStyleSuffix + "'\" onmouseout=\"this.className='normal" + hiliteStyleSuffix + "'\" onClick=\"document.location='" + location + "';\"";
			}
			MENUitem += "><a " + hiliteStyleSuffix + " title='' href='"+location+"'>"+text+"</a><br></td></tr>\n";
		}
		if(ns6)
		{
			MENUitem = "<tr><td ";
			if (hiliteStyleSuffix.length == 0)
			{
			MENUitem += "onmouseover=\"this.className='normalon'\" onmouseout=\"this.className='normal'\" onClick=\"document.location='" + location + "';\"";
			}
			MENUitem += "><a class=clsMenuItemNS6" + hiliteStyleSuffix + " title='' href='"+location+"'>"+text+"</a><br></td></tr>\n";
		}

	if (text.length == 0)
	{
			MENUitem = ""
	}
	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;
	
	
	imgName = idMainMenu.split("_")[0];
	if(document.images[imgName].src.indexOf("active") == -1)
		document.images[imgName].src = 'images/' + imgName + '_.gif';
	//alert(document[imgName].width);
	//MM_swapImage(imgName,'','images/' + imgName + '_.gif',1);
	
	//	 onMouseOut="MM_swapImgRestore();"
	switch (imgName)
	{
		case "topNav2":
		case "topNav3":
		case "topNav4":
		case "topNav5":
		case "topNav6":
		default:
			if (n){
				submenu = document.layers[idMainMenu+"submenu"];
				if (lastMenu != null && lastMenu != submenu) hideAll(idMainMenu);
				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");
				//alert("send " + idMainMenu);
				if (lastMenu != null && lastMenu != submenu) hideAll(idMainMenu);
				submenu.style.left = calculateSumOffset(menu, 'offsetLeft');
				submenu.style.top  = document.getElementById(idMainMenu).offsetHeight + menuTop;
				submenu.style.visibility = fShow;
				leftX  = calculateSumOffset(menu, 'offsetLeft');
				rightX =leftX + document.getElementById(idMainMenu+"submenu").offsetWidth;

				rightX = leftX + this.subMenuPaneWidth;
				
				leftY  = document.getElementById(idMainMenu+"submenu").offsetHeight + menuTop + 110;
				
			
			} else if (ie) { 
				menu = eval(idMainMenu);
				submenu = eval(idMainMenu+"submenu.style");
				
				if (lastMenu != null && lastMenu != submenu) hideAll(idMainMenu);			
				submenu.left = calculateSumOffset(menu, 'offsetLeft');
				submenu.top  = document.all[idMainMenu].offsetHeight + menuTop;
				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;
				
			}
			break;
	}
	switch (imgName)
	{
		case "topNav2":
		case "topNav3":
		case "topNav4":
		case "topNav5":
		case "topNav6":
			// leftY = 0;
			break;
	}
	lastMenu = submenu;
	
}

//hide the submenu
function hideAll(theMenuid){

	var setPics = false;
	if (typeof(lastMenu) != "undefined")
	{
		if (ns6){
			if (lastMenu != null) 
				{
					lastMenu.style.visibility = fHide;
					setPics = true;
					lastMenu = null;
				}
		}else{
			if (lastMenu != null) 
				{
					lastMenu.visibility = fHide;
					setPics = true;
					lastMenu = null;
				}
		}
	}
	if (setPics)
	{
		for(i=1;i<=8;i++)
		{
			imgName = "topNav" + i;
			imgNameCompare = imgName + "_id";
			//alert(imgName);
			//alert(imgNameCompare);
			//alert(imgName)
			//alert(theMenuid)
			if (imgNameCompare.indexOf(theMenuid) == -1)
			{
				if(document.images[imgName].src.indexOf("active") == -1)
				{
					//alert("hide " + theMenuid)
					document.images[imgName].src = 'images/' + imgName + '.gif';
				}
			}
		}
	}
}

//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){
	
	if (ns6){
		var x = e.pageX;
		var y = e.pageY;
		if (x > rightX || x < leftX) 
			{
			//alert("send 2");
				//alert(leftX + " " + rightX);
				hideAll();
			}
		else if (y > leftY) {
			//alert("send ");
			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 containor
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;
}	

