//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("printsid", "Prints&nbsp;&nbsp;", null, null, null);
	menu.addItem("booksid", "Books&nbsp;&nbsp;", "", null, null);
	menu.addItem("profileid", "Profile&nbsp;&nbsp;", "", null, null);
	menu.addItem("linksid", "Links", "", null, null);

// addSubItem(idParent, text, hint, location);	
	menu.addSubItem("printsid", "Autumn Colour", "", "http://www.geoffrossphotography.com/category.php?category=autumn");
	menu.addSubItem("printsid", "Canyons, Gorges & Formations", "", "http://www.geoffrossphotography.com/category.php?category=cgf");
	menu.addSubItem("printsid", "Coastlines", "", "http://www.geoffrossphotography.com/category.php?category=coast");  
	menu.addSubItem("printsid", "Deserts", "", "http://www.geoffrossphotography.com/category.php?category=desert");  
	menu.addSubItem("printsid", "Farmlands", "", "http://www.geoffrossphotography.com/category.php?category=farm");
	menu.addSubItem("printsid", "Forests & Trees", "", "http://www.geoffrossphotography.com/category.php?category=forest");
	menu.addSubItem("printsid", "Mountains", "", "http://www.geoffrossphotography.com/category.php?category=mount");
	menu.addSubItem("printsid", "Rivers, Lakes & Waterfalls", "", "http://www.geoffrossphotography.com/category.php?category=rlw");
	menu.addSubItem("printsid", "Urban & Architectural", "", "http://www.geoffrossphotography.com/category.php?category=urban");
	menu.addSubItem("printsid", "Winter & Antarctica", "", "http://www.geoffrossphotography.com/category.php?category=winter");
	
	menu.addSubItem("booksid", "New Zealand Magic", "",  "http://www.geoffrossphotography.com/newzealandmagic.php");
	menu.addSubItem("booksid", "California The Golden State", "",  "http://www.geoffrossphotography.com/californiathegoldenstate.php");

	menu.addSubItem("profileid", "Geoff Ross", "",  "http://www.geoffrossphotography.com/profile.php");
	menu.addSubItem("profileid", "Contact Details", "",  "http://www.geoffrossphotography.com/contact.php");
	
	menu.addSubItem("linksid", "Links", "",  "http://www.geoffrossphotography.com/links.php");
	
	menu.showMenu();
}

//create the menu
function Menu(){
	this.addItem    = addItem;
	this.addSubItem = addSubItem;
	this.showMenu   = showMenu;
	this.bgColor     = "070707";	
	this.mainPaneBorder = 0;
	this.subMenuPaneBorder = 0;
	this.subMenuPaneWidth = 140;
	lastMenu = null;
	
	htmlstr = "";
	htmlstr += "<!-- MENU PANE DECLARATION BEGINS -->\n";
	htmlstr += "\n";
	htmlstr += "<div id='MainTable' style='position:relative'>\n";
	htmlstr += "<table width='100%' height='21' cellpadding='0' cellspacing='0' bgcolor='"+this.bgColor+"' border='"+this.mainPaneBorder+"'>\n";
	htmlstr += "<tr align='right'>";
	htmlstr += "<td><img width='40' height='21' src='http://www.geoffrossphotography.com/graphics/t.gif' alt=''>";
	htmlstr += "</td>";
	htmlstr += "<td>";
	
		htmlstr += "<table width='320' height='21' cellpadding='0' cellspacing='0' bgcolor='"+this.bgColor+"' border='"+this.mainPaneBorder+"'>\n";
		htmlstr += "<tr align='left'>";
		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>";
		htmlstr += "</table>";
	
	htmlstr += "</td>";
	
	htmlstr += "<td><img width='45' height='21' src='http://www.geoffrossphotography.com/graphics/t.gif' alt=''>";
	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 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 + 6;
		submenu.style.visibility = fShow;
		leftX  = calculateSumOffset(menu, 'offsetLeft');
		rightX =leftX + document.getElementById(idMainMenu+"submenu").offsetWidth;
		leftY  = document.getElementById(idMainMenu+"submenu").offsetHeight + window.pageYOffset + 118;
	
	} 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 + 7;
		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 + 96;
	}
	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 container
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;
}


//reloads the page if window is resized
var origWidth, origHeight;

if (document.layers) {
  origWidth = window.innerWidth;
  origHeight = window.innerHeight;
  onresize = function() { if(innerWidth != origWidth || innerHeight != origHeight) location.reload(); }
}

else if (document.all) onresize = function() { window.location.reload(); }	   





