
window.onresize = resize;
window.onload = loaded;

//Global variables
var timer1 = 35;
var liMenu;
var liPos;
var endPos;
var divMenu;
var navBar;
var secondNav;
var inProgress = 0;
var expand;

/*This function toggles individual sections for the navbar on / off.*/
function navBarToggle(thisClass) {
	navBar = document.getElementById('navbar');
	secondNav = document.getElementById('secondnav');
	divMenu = navBar.getElementsByTagName('div');
	
	if(!inProgress) {
		inProgress = 1; //functions as a lock so that the same (or different) menu cannot be expanded simultaneously
		
		for(var i = 0; i < divMenu.length; i++)
			if(divMenu[i].className == thisClass) {
				liMenu = divMenu[i].getElementsByTagName('li');
				liPos = liMenu.length - 1;
				endPos = liPos; //endPos used for reversing the order of <li> shown, see toggleMe()
				
				if(liMenu[0].style.display == "none") {
					expand = 1;
					divMenu[i].getElementsByTagName('a')[0].style.background = "url(/images/collapse.gif) no-repeat center right";
					divMenu[i].getElementsByTagName('a')[0].style.margin = "0 6px 0 0";
				}
				else {
					expand = 0;
					divMenu[i].getElementsByTagName('a')[0].style.background = "url(/images/expand.gif) no-repeat center right";
					divMenu[i].getElementsByTagName('a')[0].style.margin = "0 5px 0 0";
				}
					
				setTimeout(toggleMe, timer1);
				
				if(liMenu[0].style.display == "none")
					divMenu[i].style.margin = "0 0 8px 0";
				else
					divMenu[i].style.margin = "0";
			}
	}
}

/*See navBarToggle().*/
function toggleMe() {
	if(liPos >= 0) {
		if(expand)
			liMenu[(endPos - liPos)].style.display = "";
		else
			liMenu[liPos].style.display = "none";
		liPos--;
		
		if(secondNav)
			secondNav.style.top = navBar.offsetHeight + 125 + 10 + "px"; //moves second nav bar if present
			
		setTimeout(toggleMe, timer1);
	}
	else {
		inProgress = 0;
		
		if(secondNav)
			adjustInside(); //adjusts the footer if second nav bar present
	}
}

/*This function changes the min-height of the #inside div
	to make it long enough for the secondary nav bar.*/
function adjustInside() {
	var secondNavPos = findPosY(secondNav) + secondNav.offsetHeight;
	var inside = document.getElementById('inside');
	
	//if((secondNavPos - findPosY(inside) - 25) > 440)
		inside.style.minHeight = secondNavPos - findPosY(inside) - 25 + "px";
	//else
		//inside.style.minHeight = 440 + "px";
}

/*This function closes the entire navbar when called.*/
function closeNavBar() {
	navBar = document.getElementById('navbar');
	secondNav = document.getElementById('secondnav');
	divMenu = navBar.getElementsByTagName('div');
	liMenu = document.getElementById('navbar').getElementsByTagName('li');
	
	for(var i = 0; i < liMenu.length; i++)
		liMenu[i].style.display = "none";
		
	for(var i = 0; i < divMenu.length; i++) {
		divMenu[i].style.margin = "0";	
		divMenu[i].getElementsByTagName('a')[0].style.background = "url(/images/expand.gif) no-repeat center right";
		divMenu[i].getElementsByTagName('a')[0].style.margin = "0 5px 0 0";
	}
		
	secondNav.style.top = navBar.offsetHeight + 125 + 10 + "px";	
}

/*This function is for the expand / collapsing functionality of
	find an article, etc.*/
function initCollapse(id, collapse, color) {
	var main = document.getElementById(id);
	var aList = main.getElementsByTagName('a');
	var ulList = main.getElementsByTagName('ul');
	
	if(ulList) {
		for(var i = 0; i < ulList.length; i++)
			if(ulList[i].className == 'left')
				var left = ulList[i].getElementsByTagName('ul');
			else if(ulList[i].className == 'right')
				var right = ulList[i].getElementsByTagName('ul');
				
		if(collapse) {
			for(i = 0; i < left.length; i++)
				left[i].style.display = "none";
			
			for(i = 0; i < right.length; i++)
				right[i].style.display = "none";
				
			for(i = 0; i < aList.length; i++)
				if(aList[i].getAttribute('onclick'))
					if(color == "green")
						aList[i].style.background = "url(/images/arrow_green.png) no-repeat right center";
					else
						aList[i].style.background = "url(/images/arrow_maroon.png) no-repeat right center";
		}
		else {
			for(i = 0; i < left.length; i++)
				left[i].style.display = "";
			
			for(i = 0; i < right.length; i++)
				right[i].style.display = "";
				
			for(i = 0; i < aList.length; i++)
				if(aList[i].getAttribute('onclick'))
					if(color == "green")
						aList[i].style.background = "url(/images/arrow_green_down.png) no-repeat right center";
					else
						aList[i].style.background = "url(/images/arrow_maroon_down.png) no-repeat right center";
		}
	}
}

/*See initCollapse.*/
function toggleDisplay(obj, color) {
	var child;
	
	if(obj.parentNode)
		if(child = obj.parentNode.getElementsByTagName('ul')[0])
			if((child.style.display == "none")) {
				child.style.display = "";
				if(color == "green")
					obj.style.background = "url(/images/arrow_green_down.png) no-repeat right center";
				else
					obj.style.background = "url(/images/arrow_maroon_down.png) no-repeat right center";
			}
			else {
				child.style.display = "none";
				if(color == "green")
					obj.style.background = "url(/images/arrow_green.png) no-repeat right center";
				else
					obj.style.background = "url(/images/arrow_maroon.png) no-repeat right center";
			}
}

/*This function randomizes the welcome screen (index) picture.*/
function welcome() {
	var pictures = new Array();
	var backColor = new Array();
	var links = new Array();
	
	pictures[0] = "url(/about/img/childrens.png) no-repeat right";
	backColor[0] = "#854210";
	links[0] = "/about/childrens.html";
	pictures[1] = "url(/about/img/clarke.png) no-repeat right";
	backColor[1] = "#b4905c";
	links[1] = "/about/clarke.html";
	pictures[2] = "url(/about/img/govdocs.png) no-repeat right";
	backColor[2] = "#270c00";
	links[2] = "/about/govdocs.html";
	pictures[3] = "url(/about/img/browsinghome2.png) no-repeat right";
	backColor[3] = "#502913";
	links[3] = "/about/browsing.html";
	pictures[4] = "url(/about/img/documentdelivery2.png) no-repeat right";
	backColor[4] = "#e5c5b0";
	links[4] = "/about/documentdelivery.html";
	pictures[5] = "url(/about/img/lib197.png) no-repeat right";
	backColor[5] = "#822830";
	links[5] = "/about/lib197.html";
	pictures[6] = "url(/about/img/ocls.png) no-repeat right";
	backColor[6] = "#5a7087";
	links[6] = "/about/ocls.html";
	
	var randNo = Math.floor(7 * Math.random()); //Then number here is the total number of pictures
	var welcome = document.getElementById('welcome');
	var welcomelink = document.getElementById('welcomelink').getElementsByTagName('a')[0];

	welcome.style.background = pictures[randNo];
	welcome.style.backgroundColor = backColor[randNo];
	welcomelink.href = links[randNo];
}

/*Adjusts the width of the news section (index) depending on browser width.
	A default size (smallest) is defined in the CSS files in case a browser
	does not support javascript.*/
function newsBoxSize(news) {	
	if(window.innerWidth)
		if(window.innerWidth > 965)
			news.style.width = window.innerWidth - 575 + "px";
		else
			news.style.width = 390 + "px";
	else if(document.all)
		if(document.body.clientWidth > 965)
			news.style.width = document.body.clientWidth - 575 + "px";
		else
			news.style.width = 390 + "px";
}

/*Resises the greybox height for the About pages due to undefined height.*/
function greyboxSize(greybox) {
	greybox.style.display = "block";
	
	var divList = greybox.getElementsByTagName('div');
	for(i = 0; i < divList.length; i++)
		if(divList[i].className == "bottomright")
			break;
	for(s = 0; s < divList.length; s++)
		if(divList[s].className == "story")
			break;
	for(l = 0; l < divList.length; l++)
		if(divList[l].className == "links")
			break;
	
	if(divList[s].offsetHeight > divList[l].offsetHeight)	
		divList[i].style.height = divList[s].offsetHeight + 40 + "px";	
	else
		divList[i].style.height = divList[l].offsetHeight + 40 + "px";	
}

/*Opens specified catagory on collapsed list pages.*/
function checkAnchor() {
	var hash = window.location.hash;
	
	if(hash) {
		hash = hash.replace(/#/, "");
		hash = hash.replace(/&/, "&amp;");
		liList = document.getElementsByTagName('li');
		
		for(var i = 0; i < liList.length; i++) {
			var check = liList[i].getElementsByTagName('a')[0];
			
			if(check)
				if(check.innerHTML == hash) {
					toggleDisplay(check);
					break;
				}
		}
	}
}

/*Finds the Y position of given object.*/
function findPosY(obj) {
	var curtop = 0;
	
	if(obj.offsetParent)
		while(1) 
		{
			curtop += obj.offsetTop;
			
			if(!obj.offsetParent)
				break;
				
			obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
		
	return curtop;
}

/*This function correctly applies alpha transparency to PNG images embedded
	directly into HTML pages when called.  This applies to IE version < 7.*/
function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (version < 7) && (document.body.filters)) 
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }    
}

/*This function opens up a popup window with the specified parameters provided.*/
function popup(location, height, width) {
	if(typeof(newpopup) != 'undefined')
		newpopup.close();

	newpopup = window.open(location, 'poppy', 'height='+height+', width='+width+'');
	newpopup.focus();		
	
	return false;
}

/*This function passes the current page to the contact web team page.*/
function passPage(obj) {
	obj.href += "#" + document.location.href;
}

/*This function handles all window resizing for all pages.*/
function resize() {
	//resizes news section on index page
	var news = document.getElementById('box');
	if(news)
		newsBoxSize(news);
		
	//resizes grey box because of undefined height
	var greybox = document.getElementById('greybox');
	if(greybox)
		greyboxSize(greybox);
}

/*This function handles all window onload events for all pages.*/
function loaded() {
	var inside = document.getElementById('inside');
	
	if(inside)
		if(inside.className == "secondnav")
			adjustInside(); //adjusts the footer if second nav bar present
			
	//Disables highlighting of the navbar		
	var target = document.getElementById('navbar');
	
	if(target) {
		if(typeof target.onselectstart!="undefined")
			target.onselectstart=function(){return false}
		else if(typeof target.style.MozUserSelect!="undefined")
			target.style.MozUserSelect="none"
		else
			target.onmousedown=function(){return false}
	}
	//end	
	
	//Resize the greybox on the about pages
	var greybox = document.getElementById('greybox');
	
	if(greybox)
		greyboxSize(greybox);
}
