// JavaScript Document

//global menu state
var menuReady = false;

//pre-cache menubar image pairs
if (document.images) {
	var imagesNormal = new Array();
	imagesNormal["about"] = new Image(34, 120);
	imagesNormal["about"].src = "http://www.newrymemoirs.com/resources/about_off.jpg";	
	imagesNormal["stories"] = new Image(34, 120);
	imagesNormal["stories"].src = "http://www.newrymemoirs.com/resources/stories_off.jpg";
	imagesNormal["photos"] = new Image(34, 120);
	imagesNormal["photos"].src = "http://www.newrymemoirs.com/resources/photos_off.jpg";
	imagesNormal["geography"] = new Image(34, 120);
	imagesNormal["geography"].src = "http://www.newrymemoirs.com/resources/geography_off.jpg";
	imagesNormal["book"] = new Image(34, 120);
	imagesNormal["book"].src = "http://www.newrymemoirs.com/resources/book_off.jpg";
	imagesNormal["contact"] = new Image(34, 120);
	imagesNormal["contact"].src = "http://www.newrymemoirs.com/resources/contact_off.jpg";
	
	var imagesHilite = new Array();
	imagesHilite["about"] = new Image(34, 120);
	imagesHilite["about"].src = "http://www.newrymemoirs.com/resources/about_on.jpg";	
	imagesHilite["stories"] = new Image(34, 120);
	imagesHilite["stories"].src = "http://www.newrymemoirs.com/resources/stories_on.jpg";
	imagesHilite["photos"] = new Image(34, 120);
	imagesHilite["photos"].src = "http://www.newrymemoirs.com/resources/photos_on.jpg";
	imagesHilite["geography"] = new Image(34, 120);
	imagesHilite["geography"].src = "http://www.newrymemoirs.com/resources/geography_on.jpg";
	imagesHilite["book"] = new Image(34, 120);
	imagesHilite["book"].src = "http://www.newrymemoirs.com/resources/book_on.jpg";
	imagesHilite["contact"] = new Image(34, 120);
	imagesHilite["contact"].src = "http://www.newrymemoirs.com/resources/contact_on.jpg";
}

function getElementStyle(elem, CSSStyleProp) {
	var styleValue, camel;
	if (elem) {
		if (document.defaultView && document.defaultView.getComputedStyle) {
			// W3C DOM version
			var compStyle = document.defaultView.getComputedStyle(elem, "");
			styleValue = compStyle.getPropertyValue(CSSStyleProp);
		} else if (elem.currentStyle) {
			// make IE style property camelCase name from CSS version
			var IEStyleProp = CSSStyleProp;
			var re = /-\D/;
			while (re.test(IEStyleProp)) {
				camel = IEStyleProp.match(re)[0].charAt(1).toUpperCase();
				IEStyleProp = IEStyleProp.replace(re, camel);
			}
			styleValue = elem.currentStyle[IEStyleProp];
		}
	}
	return (styleValue) ? styleValue : null;
}

// carry over some critical menu style sheet attributes values
var CSSRuleValues = {menuItemHeight:"22px",
					menuItemLineHeight:"1.4px",
					menuWrapperBorderWidth:"2px",
					menuWrapperPadding:"3px",
					defaultBodyFontSize:"12px"
					};

//specifications for menu contents and menubar image associations
var menus = new Array();
menus[0] = {mBarImgId:"menuImg_0",
			mBarImgNormal:imagesNormal["about"],
			mBarImgHilite:imagesHilite["about"],
			menuItems:[],
			elemId:""
			};
			

menus[1] = {mBarImgId:"menuImg_1",
			mBarImgNormal:imagesNormal["stories"],
			mBarImgHilite:imagesHilite["stories"],
			menuItems:[ {text:"Places", href:"http://www.newrymemoirs.com/stories_pages/_stories-places.html"},
						{text:"People", href:"http://www.newrymemoirs.com/stories_pages/_stories-people.html"},
						{text:"Notable Events", href:"http://www.newrymemoirs.com/stories_pages/_stories-events.html"},
						{text:"Industry", href:"http://www.newrymemoirs.com/stories_pages/_stories-industry.html"}],
			elemId:""
			};

menus[2] = {mBarImgId:"menuImg_2",
			mBarImgNormal:imagesNormal["photos"],
			mBarImgHilite:imagesHilite["photos"],
			menuItems:[ {text:"Civil Rights", href:"http://www.newrymemoirs.com/photo_pages/_photos-civilrights.html"},
					   	{text:"The Troubles", href:"http://www.newrymemoirs.com/photo_pages/_photos-troubles.html"},
						{text:"People", href:"http://www.newrymemoirs.com/photo_pages/_photos-people.html"},
						{text:"Sport", href:"http://www.newrymemoirs.com/photo_pages/_photos-sport.html"},
						{text:"Landmarks", href:"http://www.newrymemoirs.com/photo_pages/_photos-landmarks.html"}],
			elemId:""
			};

menus[3] = {mBarImgId:"menuImg_3",
			mBarImgNormal:imagesNormal["geography"],
			mBarImgHilite:imagesHilite["geography"],
			menuItems:[],
			elemId:""
			};

menus[4] = {mBarImgId:"menuImg_4",
			mBarImgNormal:imagesNormal["book"],
			mBarImgHilite:imagesHilite["book"],
			menuItems:[ {text:"Intro", href:"http://www.newrymemoirs.com/book/ddog_intro.html"},
						{text:"Cover Story", href:"http://www.newrymemoirs.com/book/ddog_cover.html"},
						{text:"Chapter 1", href:"http://www.newrymemoirs.com/book/ddog_page03.html"},
						{text:"Chapter 2", href:"http://www.newrymemoirs.com/book/ddog_page09.html"},
						{text:"Chapter 3", href:"http://www.newrymemoirs.com/book/ddog_page14.html"},
						{text:"Chapter 4", href:"http://www.newrymemoirs.com/book/ddog_page18.html"}],
			elemId:""
			};

menus[5] = {mBarImgId:"menuImg_5",
			mBarImgNormal:imagesNormal["contact"],
			mBarImgHilite:imagesHilite["contact"],
			menuItems:[],
			elemId:""
			};

//create hash table-like lookup for menu objects with id string indexes
function makeHashes() {
	for (var i = 0; i < menus.length; i++) {
		menus[menus[i].elemId] = menus[i];
		menus[menus[i].mBarImgId] = menus[i];
	}
}

//assign menu label image event handlers
function assignLabelEvents() {
	var elem;
	for (var i = 0; i < menus.length; i++) {
		elem = document.getElementById(menus[i].mBarImgId);
		elem.onmouseover = swap;
		elem.onmouseover = swap;
	}
}

// invoked from init(), generates the menu div elements and their contents.
// all this action is invisible to user during construction.
function makeMenus() {
	var menuDIv, menuItem, itemLink, mBarImg, textNode, offsetLeft, offsetTop;
	
	//determine key adjustment factors for the total height of the menu divs.
	
	var menuItemH = 0;
	var bodyFontSize = parseInt(getElementStyle(document.body, "font-size"));
	// test to see if browser's font size has been adjusted by the user
	// and that the new size registers as an applied style properly
	
	if (bodyFontSize == parseInt(CSSRuleValues.defaultBodyFontSize)) {
		menuItemH = (parseFloat(CSSRuleValues.menuItemHeight));
	} else {
		//works nicely in Mozilla
		menuItemH = parseInt(parseFloat(CSSRuleValues.menuItemLineHeight) * bodyFontSize);
	}

	var heightAdjust = parseInt(CSSRuleValues.menuWrapperPadding) + parseInt(CSSRuleValues.menuWrapperBorderWidth);
	
	if (navigator.appName == "Microsoft Internet Explorer" && navigator.userAgent.indexOf("Win") != -1 &&
		(typeof document.compatMode == "undefined" || document.compatMode == "BackCompat") ) {
			heightAdjust = -heightAdjust;
			
	}
	
	//use menus array to drive div creation loop
	for (var i = 0; i < menus.length; i++) {
		menuDiv = document.createElement("div");
		menuDiv.id = "popupmenu" + i;
		//preserve menu's ID as property of the menus array item
		menus[i].elemId = "popupmenu" + i;
		menuDiv.className = "menuWrapper";
		if (menus[i].menuItems.length > 0) {
			menuDiv.style.height = (menuItemH * menus[i].menuItems.length) - heightAdjust + "px";
		} else {
			// don't display any menu div lacking menu items
			menuDiv.style.display = "none";
		}
		
		//define event handlers
		menuDiv.onmouseover = keepMenu;
		menuDiv.onmouseout = requestHide;
		
		//set stacking order in case other layers are around the page
		menuDiv.style.zIndex = 1000;
		
		//assemble menu item elements for inside menu div
		for (var j=0; j < menus[i].menuItems.length; j++) {
			menuItem = document.createElement("div");
			menuItem.id = "popupmenuItem_" + i + "_" + j;
			menuItem.className = "menuItem";
			menuItem.onmouseover = toggleHighlight;
			menuItem.onmouseout = toggleHighlight;
			menuItem.onclick = hideMenus;
			menuItem.style.top = menuItemH * j + "px";
			itemLink = document.createElement("a");
			itemLink.href = menus[i].menuItems[j].href;
			itemLink.className = "menuItem";
			itemLink.onmouseover = toggleHighlight;
			itemLink.onmouseout = toggleHighlight;
			textNode = document.createTextNode(menus[i].menuItems[j].text);
			itemLink.appendChild(textNode);
			menuItem.appendChild(itemLink);
			menuDiv.appendChild(menuItem);
		}
		// append each menu div to the body		
		document.body.appendChild(menuDiv);
	}
	makeHashes();
	assignLabelEvents();
	//pre-position menu
	for (i = 0; i < menus.length; i++) {
		positionMenu(menus[i].elemId);
	}
	menuReady = true;
}

//initialize global that helps manage menu hiding
var timer;

// invoked from mouseovers inside menus to cancel hide
// request from mouseout of menu bar image et al.
function keepMenu() {
	clearTimeout(timer);
}

function cancelAll() {
	keepMenu();
	menuReady = false;
}

// invoked from mouseouts to request hiding all menus
// in 1/4 second, unless cancelled
function requestHide() {
	timer = setTimeout("hideMenus()", 250);
}

//brute Force hiding of all menus and restoration
// of normal menu bar images
function hideMenus() {
	for (var i = 0; i < menus.length; i++) {
		document.getElementById(menus[i].mBarImgId).src = menus[i].mBarImgNormal.src;
		var menu = document.getElementById(menus[i].elemId);
		menu.style.visibility = "hidden";
	}
}

//set menu position just before displaying it
function positionMenu(menuId) {
	//use the menu bar image for position reference of related div
	var mBarImg = document.getElementById(menus[menuId].mBarImgId);
	var offsetTrail = mBarImg;
	var offsetLeft = 0;
	var offsetTop = 0;
	while (offsetTrail) {
		offsetLeft += offsetTrail.offsetLeft;
		offsetTop += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
	}
	if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined") {
		offsetLeft += document.body.leftMargin;
		offsetTop += document.body.topMargin;
	}
	var menuDiv = document.getElementById(menuId);
	//sets the position of the pop out location.
	menuDiv.style.left = offsetLeft + mBarImg.width + 1 + "px";
	menuDiv.style.top = offsetTop + "px";
}

//display a particular menu div
function showMenu(menuId) {
	if (menuReady) {
		keepMenu();
		hideMenus();
		positionMenu(menuId);
		var menu = document.getElementById(menuId);
		menu.style.visibility = "visible";
	}
}

// menu bar image swapping, invoked from mouse events in menu bar
// swap style sheets for menu items during rollovers
function toggleHighlight(evt) {
	evt = (evt) ? evt : ((event) ? event : null);
	if (typeof menuReady != "undefined") {
		if (menuReady && evt) {
			var elem = (evt.target) ? evt.target : evt.srcElement;
			if (elem.nodeType == 3) {
				elem = elem.parentNode;
			}
			if (evt.type == "mouseover") {
				keepMenu();
				elem.className = "menuItemOn";
			} else {
				elem.className = "menuItem";
				requestHide();
			}
			evt.cancelBubble = true;
		}
	}
}

function swap(evt) {
	evt = (evt) ? evt : ((event) ? event : null);
	if (typeof menuReady != "undefined") {
		if (evt && (document.getElementById && document.styleSheets) && menuReady) {
			var elem = (evt.target) ? evt.target : evt.srcElement;
			if (elem.className == "menuImg") {
				if (evt.type == "mouseover") {
					showMenu(menus[elem.id].elemId);
					elem.src = menus[elem.id].mBarImgHilite.src;
				} else if (evt.type == "mouseout") {
					requestHide();
				}
				evt.cancelBubble = true;
			}
		}
	}
}

//create menus only if key items are supported
function initMenus() {
	if (document.getElementById && document.styleSheets) {
		setTimeout("makeMenus()", 5);
		window.onunload = cancelAll;
	}
}

function addEvent(elem, evtType, func, capture) {
	capture = (capture) ? capture : false;
	if (elem.addEventListener) {
		elem.addEventListener(evtType, func, capture);
	} else if (elem.attachEvent) {
		elem.attachEvent("on" + evtType, func);
	} else {
		//for IE/Mac, NN4, and older
		elem["on" + evtType] = func;
	}
}

function addOnLoadEvent(func) {
	if (window.addEventListener || window.attachEvent) {
		addEvent(window, "load", func, false);
	} else {
		var oldQueue = (window.onLoad) ? window.onLoad : function() {};
		window.onload = function() {
			oldQueue();
			func();
		}
	}
}

//these four functions are for rollover functionality.
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

addOnLoadEvent(initMenus);
