/*

aqtmenus.js

Aquatax Site Dynamic Menu creation script

*/

var sub_menus = [];

var menu_close_timeout = [];			/* array to hold the menu timer objects */
var delay = 100;					/* millisecond pause on the menu hide operation */

/* Some global objects we use repeatedly below */
var dd = null;					/* document.getElementById('dropdowns_ex'); */
var b = null;					/* document.getElementsByTagName('body'); */
var head = null;					/* document.getElementById('header'); */


/*
The following array elements define the top-menu-bar cells; Parameters are:

- a short name for the section: used to attach submenus only
- the text to display in the cell
- the link to the page (absolute format please)
- the amount of padding (pixels) on the top of the cell (max of 11)
*/
menu_bar = [
	['home','Home Page','/index.html',11],	
	['aquatax','AquaTax Consulting<br>Services & Information','/aquatax.html',5],
	['skmacro','SK Macroinvertebrates<br>& Fish Info','/BugGuide.html',5],
	['skaq','SK Aquatic<br>Info & Surveys','/skaquaticinfo.html',5],
	['snowyowls','RCAF 420 Squadron<br>Tribute','/snowyowl.html',5],
	['photos','Misc. Photos','/insectphotos.html',11],
	['about','About The<br> Webpages & Links','/about.html',5]
];

/*
If you want sub-menus on a cell, you define as the following examples show: the array
KEY is the short name for the menu-bar cell under which the submenu is to be displayed;
The parameters are:

- a short name for the sub-menu item (this must go into the onload="menuInit()" function call in <body> tag
- the text to display in the cell
- the absolute link to the page
*/

/* sk aquatic info sub-menu */
sub_menus['skaq'] = [
	['skaq','SK Aquatic Information','/skaquaticinfo.html'],
	['athabasca','Lake Athabasca','/Athabasca.html'],
	['candle','Candle Lake','/CandleL.html'],
	['pipestone','Pipestone Creek','/Pipestone.html'],
	['nature','Nature SK Sanctuaries','/natskweb.html'],
	['property','Our Property<br>Biodiversity Survey','/Property.html']
];

/* sk macro and fish submenu under here */
sub_menus['skmacro'] = [
	['skaquaticinsects','SK Aquatic Insects','/skAquaticInsectInfo.html'],
	['lifecycles','Insect Lifecycles','/insectlifecycle.html'],
	['collecting','Collecting Aquatic "Bugs"','/collect.html'],
	['ids','Identification & Classification','/classify.html'],
	['skdb','SK Macroinvertebrate<br/>Distribution Database','/invertsdb/'],
	['bugguide','SK Bug Guide','/BugGuide.html'],
	['orderkey','SK Insect Order Key','/TaxaKey.html'],
	['mayflies','Mayflies (Ephemeroptera)','/mayfly.html'],
	['stoneflies','Stoneflies (Plecoptera)','/plecoptera.html'],
	['odonata','Dragonflies & Damselflies (Odonata)','/odonata.html'],
	['hemiptera','True Bugs (Hemiptera)','/hemiptera.html'],
	['coleoptera','Beetles (Coleoptera)','/coleoptera.html'],
	['trichoptera','Caddisflies (Trichoptera)','/trichoptera.html'],
	['minor','Aquatic Moths (Lepidoptera)','/minor.html'],
	['minor','Dobsonflies (Megaloptera)','/minor.html'],
	['minor','Spongillaflies (Neuroptera)','/minor.html'],
	['minor','Parasitic Wasps (Hymenoptera)','/minor.html'],
	['miscflies','Two-winged Flies (Diptera)','/miscdip.html'],
	['leeches','Leeches (Hirudinea)','/leech.html'],
	['snails','Snails & Clams (Mollusca)','/mollusca.html'],
	['crustacea','Crayfish and Scuds (Crustacea)','/crayfish.html'],
	['fish','Fish','/fish.htm']
];

/* photos sub-menu */
sub_menus['photos'] = [
	['insects','"Bug" Photos','/insectphotos.html'],
	['wildlife','Wildlife Photos','/wildlifephotos.html'],
	['plants','Plant Photos','/plantphotos.html'],
	['landscapes','Landscape Photos','/landscapephotos.html'],
	['planes','Planes and Jet Photos','/planephotos.html']
];

/* aquatax sub-menu */
sub_menus['aquatax'] = [
	['aquatax','AquaTax Consulting<br>Home Page','/aquatax.html'],
	['aquataxServices','AquaTax Consulting <br>Services','/aquataxServices.html'],
	['aquataxQualifications','AquaTax Consulting <br>Qualifications','/aquataxQualifications.html']
];



/* snowyowl sub-menu */
sub_menus['snowyowls'] = [
	['snowyowl','420 Squadron Tribute<br> Home Page','/snowyowl.html'],
	['hampden','Hampdens<br>Jan to Jul 1942','/Hampden.html'],
	['wellingtons','Wellingtons<br>Aug 1942 to Oct 1943','/Wellingtons.html'],
	['NAfrica','North Africa<br>May to Oct 1943','/NorthAfrica.html'],
	['halifaxes','Halifaxes<br>Oct 1943 to Apr 1945','/Halifaxes.html'],
	['Lancasters','Lancasters<br> May to Jun 1945','/Lancasters.html'],
	['Group Images','420 Group Photos<br>"Who is Who','/420Photos.html'],
	['Memorial','420 Squadron<br> Losses','/Memorial.html']
];
	
	
/*
   the sk database sub-menus will be built dynamically under here: the first
   element is defined here as the 'search' or intro page
*/
sub_menus['skdb'] = [
	['search','Search Database','/invertsdb/']
];

//
// this is the width to set the FIRST menu bar cell: fixed to correspond to the left panel width
//
var left_panel = '138px';

//
// pull in a jscript file and attached it to the head element: use to set up
// an asynchronous event handler (AJAX);
// 
function IncludeJScript(jsURL,id) {
	var head = document.getElementsByTagName("head").item(0);
	if ( !head ) { alert("No head!"); return null; }
	var s = document.createElement("script");
	if ( !s ) { alert("No script!"); return null; }
    	s.setAttribute("type", "text/javascript");
	s.setAttribute("charset", "utf-8");
	s.setAttribute("src", jsURL);
	s.setAttribute("id", id);
	head.appendChild(s);
}

//
// this is the work horse function: call it from within the <body> tags onload event. The parameter
// is a key to a cell that you want displayed as the selected (or active) style: it should always be the
// page that you are actually viewing, and can be numerical to select a top-menu cell, or a short name for
// a sub-menu cell
//
function menuInit(selected) {
	var ul;
	var ul2;

	//
	// get the dropdowns div and the body/head elements
	//
	dd = document.getElementById('dropdowns_ex');
	b = document.getElementsByTagName('body');
	head = document.getElementById('header');

	if ( dd ) {
		ul = document.createElement('ul');
		dd.appendChild(ul);
		var li;
		var sm;
		for ( i = 0; i < menu_bar.length; i++ ) {
			tname = 'tmenu_'+menu_bar[i][0];
			li = document.createElement('li');
			ul.appendChild(li);
			li.style.paddingTop = menu_bar[i][3]+'px';
			li.style.paddingBottom = (11 - menu_bar[i][3])+'px';
			li.setAttribute('id','tmenu_'+menu_bar[i][0]);
			li.innerHTML = menu_bar[i][1];
			if ( i == 0 ) li.style.width = left_panel;

			if ( selected == i || selected == menu_bar[i][0] ) li.className = 'selected';
			else li.onclick = new Function('jump_to(\''+menu_bar[i][2]+'\')');
		}
	}
	//
	// top level menus are in place: build submenus where needed
	//
	buildSubMenus(selected);
}

//
// this can be called directly from the onload function if you are using static html for the
// top menus: just make sure that the top-menu <li> elements all have correct id specified
// (e.g. <li id="tmenu_skdb">)
//
function buildSubMenus(selected) {
	for ( i = 0; i < menu_bar.length; i++ ) {
		buildSubMenu({"TopMenu":menu_bar[i][0],"Selected":selected,"SubMenus":sub_menus[menu_bar[i][0]]});
	}

	//
	// The sk database sub-menus will be built dynamically: this next line pulls the 
	// menu elements file from the JSON server and gets them into the JavaScript name space
	//
	IncludeJScript('/srv/json.php/dhtml_menu/'+selected+'/buildSubMenu/debug','dbmenu');

	//
	// add the window on resize handler
	//
	window.onresize = positionSubMenus;
}

//
// called from the window onresize event to reposition sub-menus
//
function positionSubMenus() {
	for ( i = 0; i < menu_bar.length; i++ ) {
		tname = 'tmenu_'+menu_bar[i][0];
		var tmenu = document.getElementById(tname);
		var ul = document.getElementById('ul_'+menu_bar[i][0]);
		if ( tmenu && ul ) positionSubMenu(tmenu,ul);
	}
}

//
// position a single sub-menu
//
function positionSubMenu(tmenu,ul) {
	var lt = tmenu.offsetLeft - 1;
	var tt = tmenu.offsetTop + head.offsetHeight + dd.offsetHeight - 4;
	var wt = tmenu.offsetWidth;
	ul.style.left = lt+'px';
	ul.style.top = tt+'px';
}

//
// build a submenu using the passed object: may be called via onload or as
// a JSON callback function
//
function buildSubMenu(json) {
	if ( !dd ) dd = document.getElementById('dropdowns_ex');
	if ( !b ) b = document.getElementsByTagName('body');
	if ( !head ) head = document.getElementById('header');

	var tname = 'tmenu_'+json.TopMenu;
	var smenus = json.SubMenus;
	var selected = json.Selected;
	var tmenu = document.getElementById(tname);
	if ( tmenu && smenus ) {
		ul2 = document.getElementById('ul_'+json.TopMenu);
		if ( !ul2 ) {
			var ul2 = document.createElement('ul');
			ul2.setAttribute('id','ul_'+json.TopMenu);
			ul2.className = 'sub_menu';
			ul2.style.width = (tmenu.offsetWidth+1)+'px';
			ul2.style.visibility = 'hidden';
			b[0].appendChild(ul2);
			positionSubMenu(tmenu,ul2);
		}
		for ( j = 0; j < smenus.length; j++ ) {
			li2 = document.createElement('li');
			li2.innerHTML = smenus[j][1];
			li2.style.paddingTop = '1px';
			li2.onmouseover = new Function('set_visible(\'ul_'+json.TopMenu+'\',1)');
			li2.onmouseout = new Function('delayed_hide(\'ul_'+json.TopMenu+'\',0)');
			if ( selected == smenus[j][0] ) li2.className = 'selected';
			else li2.onclick = new Function('jump_to(\''+smenus[j][2]+'\')');
			ul2.appendChild(li2);
		}
		tmenu.onmouseover = new Function('set_visible(\'ul_'+json.TopMenu+'\',1)');
		tmenu.onmouseout = new Function('delayed_hide(\'ul_'+json.TopMenu+'\',0)');
	}
}

//
// the main jump function
//
function jump_to(str) {	document.location.href = str; }

//
// set a sub-menu visible, or hide it
//
function set_visible(ulstr,on) {
	var dd = document.getElementById(ulstr);
	if ( on == 1 ) cancel_close(ulstr);
	dd.style.visibility = (on == 1 ? 'visible' : 'hidden');
}

//
// delayed hide function: create an associative array of timeout objects; set the timer
// and the menu will disappear after the specified delay
//
function delayed_hide(item) {
	menu_close_timeout[item] = setTimeout('set_visible(\''+item+'\',0)',delay);
}

//
// cancel the timer event on a submenu, so it doesn't get hidden after all
//
function cancel_close(item) {
	if ( menu_close_timeout[item] ) clearTimeout(menu_close_timeout[item]);
}


