// -- 
// The cookie code in this file is modified from the example in the 
// Netscape JavaScript 1.3 Guide: 
//   http://devedge-temp.mozilla.org/library/manuals/2000/javascript/1.3/guide/ 
// -- 

// Some variables we need...
var navMenuArray; 
var navOOSArray;
var topMenu = "navMenu";
var now = new Date();
var cookieDie = new Date();
// expiration is +1year in millisecs
cookieDie.setTime(now.getTime() + (365 * 24 * 60 * 60 * 1000));

// Cookie functions
function initCookie(name,value) {
	if (! getCookie(name)) {
		setCookie(name,value,cookieDie);
	}
}

function setCookie(name,value,expire) {
	var cookie = name + "=" + escape(value);
	if (expire!=null) {
		cookie = cookie + ";expires=" + expire.toGMTString();
	}
	if (document.cookie = cookie) {
		return true;
	} else {
		return false;
	}
}

function getCookie(name) {
	var search = name + "=";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = document.cookie.indexOf(";",offset);
			if (end == -1) {
				end = document.cookie.length;
			}
			return unescape(document.cookie.substring(offset,end));
		}
	}
}

// Image swapper
function imageSwap(myImage,newSrc) {
	myImage.src=newSrc;
}

// Functions that alter our page, either changing HTML or CSS
function changeByTag(myTag,myAttrib,myValue) {
	if (document.getElementsByTagName) {
		var tagSet = document.getElementsByTagName(myTag)
		if (tagSet!=null)
		{
			for (i=0; i<tagSet.length; i++) {
				tagSet[i].style[myAttrib]=myValue;
			}
		}
	}
}

function changeById(myId,myAttrib,myValue) {
	if (document.getElementById) {
		document.getElementById(myId).style[myAttrib]=myValue;
	}
}

function showById(myId) {
	changeById(myId,"visibility","visible");
}

function hideById(myId) {
	changeById(myId,"visibility","hidden");
}

function alterInnerText(myId,myNewText) {
	if (document.getElementById) {
		document.getElementById(myId).innerHTML=myNewText;
	}
	
}

// AccPal related stuff
function resetDisplay() {
	setCookie("bgState",1,cookieDie);
	setCookie("textState",0,cookieDie);
	setCookie("csState",0,cookieDie);
	self.location.reload();
}

function palBG() {
	if (getCookie("bgState")==1) {
		setCookie("bgState",0,cookieDie);
	} else {
		setCookie("bgState",1,cookieDie);
	}
	self.location.reload();
}

function doBG() {
	if (getCookie("bgState")==1) {
		alterInnerText("doBG","Hide Background");
		showById("gradient");
	} else {
		alterInnerText("doBG","Show Background");
		hideById("gradient");
	}	
}

function bigText() {
	changeByTag("body","fontSize","15pt");
	changeByTag("h1","fontSize","30pt");
	changeById("mainNav","height","235px");
}

function normalText() {
	changeByTag("body","fontSize","10pt");
	changeByTag("h1","fontSize","20pt");
	changeById("mainNav","height","");
}

function palText() {
	if (getCookie("textState")==1) {
		setCookie("textState",0,cookieDie);
	} else {
		setCookie("textState",1,cookieDie);
	}
	self.location.reload();
}

function doText() {
	if (getCookie("textState")==1) {
		alterInnerText("doText","Normal Text");
		bigText();
	} else {
		alterInnerText("doText","Big Text");
		normalText();
	}
}

function palColourScheme(myMode) {
	setCookie("csState",myMode,cookieDie);
	self.location.reload();
}

function colourScheme() {
	myMode=getCookie("csState");
	var myFore;
	var myPane;
	var myBack;
	var myBorder;
	var myPal;

	switch (myMode) {
	case "1":	
		myFore="black";
		myPane="white";
		myBack="white";
		break
	case "2":
		myFore="white";
		myPane="#004080";
		myBack="#004080";
		break
	case "3":
		myFore="white";
		myPane="black";
		myBack="black";
		break
	case "4":
		myFore="yellow";
		myPane="black";
		myBack="black";
		break
	default:
		myFore="white";
		myPane="#0080ff";
		myBack="#004080";
		break
	}
	myBorder="1pt solid " + myFore;
	changeByTag("body","color",myFore);
	changeByTag("a","color",myFore);
	changeByTag("body","background",myBack);
	changeById("contentPane","background",myPane);
	changeById("contentPane","border",myBorder);
	if (myMode != 1) {
		myPal=myPane;
	} else {
		myPal="black";
	}
	changeById("printIcon","background",myPal);
	changeById("apIcon","background",myPal);
	for (var myMenu in allMenuArray) {
		changeById(allMenuArray[myMenu],"border",myBorder);
		changeById(allMenuArray[myMenu],"background",myPane);
	}
}

// MENUING SYSTEM v2

function closeAllMenus() {
	var working;
	var id;
	var index;
	for (index=0; index < navMenuArray.length; index++) {
		working = navMenuArray[index];
		myId = working.id;
	// Do not close the top level menu
		if (myId != topMenu) {
			hideById(myId);
		}
	}
}

function openMenu(myMenu) {
	if (isMenu(myMenu)) {
		closeExcept(myMenu);
		showById(myMenu);
	}
}

function closeExcept (myText) {
	var index;
	var test = myText.substr(0,(myText.length - 4));
	for (index=0; index < navMenuArray.length; index++) {
		against = navMenuArray[index].id;
		len = against.length - 4;
		if (test.indexOf(against.substr(0,len)) == -1) {
			closeMenu(against);
		}
	}
}

function closeMenu(myMenu) {
	if ((myMenu != topMenu) && (isMenu(myMenu))) {
		hideById(myMenu);
	}
}

function isNav (myText) {
	if (myText.substr(0,3) == "nav") {
		return true;
	} else {
		return false;
	}
}

function menuExists (myText) {
	var i;
	for (i=0; i < navMenuArray.length; i++) {
		if (navMenuArray[i].id == myText) {
			return true;
		}
	}
	return false;
}

function isMenu (myText) {
	if (
		(myText.substr(myText.length - 4) == "Menu") &&
		(menuExists(myText))
	) {
		return true;
	} else {
		return false;
	}
}

function hasMenu (myText) {
	if (
		(isNav(myText)) && 
		(! isMenu(myText)) &&
		(isMenu(myText + "Menu"))
	) {
		return true;
	} else {
		return false;
	}
}

function stripTrailingBulletStuff(myText) {
	var posn=-1;
	if (myText.lastIndexOf("UL") != -1) {
		posn = myText.lastIndexOf("UL");
	} else if (myText.lastIndexOf("LI") != -1) {
		posn = myText.lastIndexOf("LI");
	}

	if (posn != -1) {
		return myText.substr(0,posn);
	} else {
		return myText;
	}
}

function menuContaining (myText) {
	var posn;
	if (isMenu(myText)) {
		myText = "";
	} else {
		myText = stripTrailingBulletStuff(myText);
		myRegex = /[A-Z][^A-Z]+$/;
		posn = myText.search(myRegex);
		if (posn != -1) {
			myText = myText.substr(0,posn);
		}
		myText += "Menu";
	}
	return myText;
}

function parentOf (myText) {
	var posn;
	if (isMenu(myText)) {
		posn = myText.lastIndexOf("Menu");
		myText = myText.substr(0,posn);
	} else {
		myText = stripTrailingBulletStuff(myText);
		myText = menuContaining(myText);
	}
	return myText;
}

function outOfNavScope (myText) {
	for (var i=0; i < navOOSArray.length; i++) {
		if (navOOSArray[i] == myText) {
			return true;
		}
	}
	return false;
}

function mouseOverProcessor (myC,myR) {
	myR.id = stripTrailingBulletStuff(myR.id);
	if (outOfNavScope(myR.id)) {
		if (hasMenu(myC.id)) {
			openMenu(myC.id + "Menu");
		} else {
			closeAllMenus();
		}
	} else if (outOfNavScope(myC.id)) {
		closeAllMenus();
	} else if (! isMenu(myC.id)) {
		if (menuContaining(myC.id) == parentOf(myR.id)) {
			if (isMenu(myR.id)) {
				active = myR.id;
			} else {
				active = myR.id + "Menu";
			}
			closeMenu(active);
		}
		if (hasMenu(myC.id)) {
			openMenu(myC.id + "Menu");
		}
	} else if (
		( ! isMenu(myR.id)) && 
		(isMenu(myC.id)) &&
		(hasMenu(myR.id))
	) {
		if (myC.id != (myR.id + "Menu")) {
			closeMenu(myR.id + "Menu");
		}
	} else {
		if ( 
			(myR.id != myC.id) &&
			(myC.id != menuContaining(myR.id))
		) {
			if (isMenu (myR.id)) {
				closeMenu(myR.id);
			} else {
				closeMenu(menuContaining(myR.id));
			}
		}
	}
}

function menuEvent(anEvent) {
	var myEvent;
	if (anEvent) {
		myEvent = anEvent;
		myCurrent = myEvent.currentTarget;
		myRelated = myEvent.relatedTarget;
		myModel = "W3";
	} else if (window.event) {
		myEvent = window.event;
		myRelated = myEvent.fromElement;
		myCurrent = myEvent.toElement;
		myModel = "IE";
	}

// If we don't know what either of these are, we can't really work out whats
// going on, so we ignore the event
	if (myCurrent && myRelated && myCurrent.id && myRelated.id) {

	// Stop event bubbling as soon as we get here
		if (myModel == "W3" ) {
			myEvent.stopPropagation();	
		} else {
			myEvent.cancelBubble = true;
		}

		switch (myEvent.type) {
		case "mouseover" :
			mouseOverProcessor(myCurrent,myRelated);
			break;
		// case "mouseout" : IF REQD
		} 
	} 
} 


function initPage() {
	if (document.getElementById) {
		var insert=0;
		divArray = document.getElementsByTagName("div");
		navMenuArray = new Array;
		for (var index=0; index < divArray.length; index++) {
			if (divArray[index].id.length > 3 && divArray[index].id.substr(0,3) == "nav") {
				navMenuArray[insert] = divArray[index];
				insert++;
			}
		}
		navOOSArray = new Array ("gradient","contentPane","logo","footer");

		// Initialise our cookies for the accessibility palettes
		initCookie("bgState",1);
		initCookie("textState",0);
		initCookie("csState",0);

// DISABLED WHILE THERE'S NO AccPal
/*
		doBG();
		doText();
		colourScheme(getCookie("csState"));

		showById("palette");
*/
		initNavHandlers();
	} // end of document.getElementById code
}

