//==============================================================================
//	Free floating navigation menu module
//
//	Author:	Kevin Johnson
//	Date:	25th April 2005
//
//	(c) Copy right Townfield Enterprises 2005
//
//------------------------------------------------------------------------------
//  Change control
//
//	WARNING jscript date object uses 0-11 for months (Jan - Dec)!!
//
var version = 1.0 ;
var vDate = new Date(2006,7,09) ; // August
//
//	First implementation.
//	Based on WITTON implementation - V1.11 
//
//------------------------------------------------------------------------------
//	Initialise control data
//
var pageTopOffset = 5						// CONTENTS Top margin style in px
var minNavSpacerHeight = 35					// Minimum height of NavSpacer in px
var navPromptStyleName = '.NavBar A SPAN'	// Name of Prompt box style selector
//var navPromptStyleName = '#NavSpacer'		// Name of Prompt box style selector
var navPromptStyleIndx = 0					// Item index in stylesheet for above
var code = ''
//
//	Scan the style sheet rules to find the item who's selector text is the same
//	as the prompt box style selector [navPromptStyleName]
// 
while (document.styleSheets(0).rules.item(navPromptStyleIndx).selectorText != navPromptStyleName){
	navPromptStyleIndx = navPromptStyleIndx + 1
	}

function WriteMenu(Menu) {
//
//	Function WriteMenu()
//		Generates HTML code from the body tag through to the end of the navigation
//		menu DIV.  The menu content is selected via the selector value "Menu".
//
//		Display formatting is entirely controlled by the CSS selectors...
//			.NavBar						Container for navigation menu
//			.NavLab						
//			#NavSpacer
//			.NavBar A SPAN				[SPAN within A within .NavBar]
//			A:hover						[pseduo class on the above anchor (A)]
//
//		Events processed on <BODY> element
//			onload						[pageLoad - 
//			onresize					[navBarSize - 
//			onscroll					[calculate new offset for navbar from the
//											top of the DOCUMENT such that it
//											remains at the top of the SCREEN]
//
  code =
"	<BODY " +
"		onload=\"pageLoad();\" " +
"		onresize=\"navBarSize();\" " +
"   	onscroll=\"navBarScroll();\" " +
"		onDblclick = \"diagnostics();\" " +
"	> " +
"    <div id=\"Diagnostic\">If you can see this please send the details of your web browser (name and version) to the web designer - see link lower left.</div> " +
"    <div CLASS=\"NavBar\" " +
"		id=\"NavBar\" " +
"		>"

  switch(Menu) {
 	case "NavBarMain":
  		addMenuItem('index.htm','Home','Website front page');
  		addMenuItem('food.htm','Food','Regular menus &amp; sepecial events');
  		addMenuItem('drink.htm','Drink','Beers and wines')
  		addMenuItem('events.htm','Events','What\'s on during the week and special occasions')
  		addMenuItem('history.htm','History','The history of this old watering hole...')
//  		addMenuItem('locality.htm','Locality','Things to do around and about Kingsley')
  		addMenuItem('findus.htm','Find us','How to get here')
  		addMenuItem('links.htm','Links','Links to local web sites')
  	break

  }
  code +=
"		<div id=\"NavGap\"></div> " +
"		<div id=\"NavSpacer\"></div> " +
"		<div class=\"NavFoot\"> " +
"			website design by:" +
"			<a href=\"mailto:witton@townfield.me.uk?subject=Witton website\" " +
"				class=\"eLink\" " +
"				title=\"Please report all website problems here.\" " +
"			>townfield enterprises</a>" +
"			<a href=\"http://portal.plus.net/my/mydiscount_info/landing_page.html?WRKUh%2Fz%2FuZ5dXmEjImm%2BIDazsKZ3Uh%2F1g7w4%2BW9hIrU%3D\" " +
"				><img src=\"http://portal.plus.net/images/referrals/adsl-broadband/super-fast/free-activation-1mb.gif\" " +
"				 	title=\"Web site hosting provided by\nPlusNet - an ISP of distinction.\nClick here to join them.\" " +
"				 	alt=\"PlusNet for super fast broadband!\" " +
"				 	border=\"0\"></a>" +
"		</div>" +
"	</div>" +
"	<DIV ID=\"Contents\" " +
"		CLASS=\"content\" " +
"	> "

  return code
}
function addMenuItem(pageURL, linkText, explainText) {
//
//	Function addMenuItem(targetURL, linkText, explainText)
//
//		Appends to the existing contents of the global variable 'code',
//		the HTML to construct an Anchor element, having...
//		- a target URL of 'targetURL'
//		- visible description of 'linkText'
//		- extended descriptive text of 'explaintext'
//
//		If the menu item referes to the current page, then an Anchor element
//		is not generated.  In its place an "active page" indicator is created
//		thus implementing best web page design pratice and providing clear
//		indication to users the location of the current page in the navigation
//		system.
//
//		Active page indicator:	linkText <
//		Style is "NavBarActive"
//
  if(	window.location.toString().indexOf(pageURL)>0 
  		|| (
			window.location.toString().slice(-1) == '/'
			&&
			pageURL == 'index.htm'
			)  
	 )
	{
  	code += '<span CLASS="NavBarActive">' + linkText + ' <</span> '
  }
  else {
	code += "	<A Href=" + pageURL + ">" + linkText + "<span>" + explainText + "</span></A> "
  };
}
function addMenuLabel(labelText) {
  code += "	  <span class=\"NavLab\">" + labelText + "</span>"
}
function addPageRef(pageURL, linkText) {
  code += "	<A Href=#" + pageURL + ">" + linkText + "</A> "
}
//
//------------------------------------------------------------------------------
//	Navigation bar size control functions
//
//		The navigation is contained within a DIV block called NavBar
//		It consists of a number of BLOCK <a> tags followed by
//		a DIV called NavGap.
//		A DIV called NavSpacer is used to...
//			(a) "streach" the size of the NavBar to that of the client height
//			(b) provide a place holder for the display of the navigation prompt
//				held in the <SPAN> content of the navigation <a> tags
// 
function pageLoad() {
//
//	Function pageLoad()
//		Called when web page is loaded into the browser
//		Remembers the initial height of the navigation bar
//		Sets the top of the navPromptStyleName to the top of the NavSpacer
//
	NavBar.originalHeight = document.all.NavBar.clientHeight;
	document.styleSheets(0).rules.item(navPromptStyleIndx).style.top = parseInt(NavSpacer.offsetTop) ;
	navBarSize();
}
function navBarSize() {
//
//	Function navBarSize()
//		Resize the height of the navigation bar when the page is loaded or
//		the window is resized, such that the NavBar <DIV> is the height of
//		the client window less twice the pageTopOffset to provide for equal top
//		and bottom borders.
//
//		Adjusts the height of the NavSpacer to push the web development 
//		credits to the bottom of the navigation bar. 
//
	NavBar.currentHeight = document.all.NavBar.clientHeight
	NavBar.style.height = document.body.clientHeight - 10;
	jheight = parseInt(NavBar.style.height) - NavBar.currentHeight ;
	NavSpacer.style.height = NavSpacer.clientHeight + jheight ;
}
function navBarScroll() {
	if (document.body.scrollHeight - document.body.scrollTop + pageTopOffset
		> document.body.clientHeight) {
			NavBar.style.top=document.body.scrollTop + pageTopOffset
			Diagnostic.style.top = document.body.scrollTop + pageTopOffset + 25
	}
}
function diagnostics() {
	diagnosticHTML  = "<h3>Web page diagnostic report</h3>"
	diagnosticHTML += "<p>Code version: " + version
	diagnosticHTML += "<br>Code date: " + vDate
	diagnosticHTML += "<br>Browser name: " + navigator.appName
	diagnosticHTML += "<br>Browser version: " + navigator.appVersion + "</p>"
	diagnosticHTML += "<hr>"
	diagnosticHTML += "<button class=\"ir\" onclick=\"diagnostics();\">Refresh</button>" 
	diagnosticHTML += "<p>NavBar top: " + document.all.NavBar.offsetTop
	diagnosticHTML += "<br>Navbar original height: " + NavBar.originalHeight
	diagnosticHTML += "<br>NavBar current height: " + document.all.NavBar.clientHeight
	diagnosticHTML += "<br>NavBar height change: " + parseInt(document.all.NavBar.clientHeight - NavBar.currentHeight)
	
	diagnosticHTML += "<br><br>NavBar spacer clientHeight: " + document.all.NavSpacer.clientHeight
	diagnosticHTML += "<br>NavBar spacer offsetHeight: " + document.all.NavSpacer.offsetHeight
	diagnosticHTML += "<br>NavBar spacer posHeight: " + document.all.NavSpacer.posHeight
	diagnosticHTML += "<br>NavBar spacer height adjustment: " + jheight
	diagnosticHTML += "<br>NavBar spacer top: " + document.all.NavSpacer.offsetTop

	diagnosticHTML += "<br><br>NavBar prompt style: " + navPromptStyleName
	diagnosticHTML += "<br>NavBar prompt style Idx: " + navPromptStyleIndx
	diagnosticHTML += "<br>NavBar prompt height: " + document.styleSheets(0).rules.item(navPromptStyleIndx).style.height
	diagnosticHTML += "<br>NavBar prompt top: " + document.styleSheets(0).rules.item(navPromptStyleIndx).style.top + "</p>"
	diagnosticHTML += "<hr>"
	diagnosticHTML += "<button class=\"ir\" onclick=\"Diagnostic.style.visibility='hidden';\">Close</button>" 
	diagnosticHTML += "<p>Current page: " + window.location
	diagnosticHTML += "<br>Refering page: " + document.referrer + "</p>"
	Diagnostic.innerHTML = diagnosticHTML
	Diagnostic.style.visibility = "visible"
}