// Ensure winOnLoad() runs when window is loaded
window.onload = function() {
	if (window.winOnLoad) window.winOnLoad();
}

var slideTime = 700; // Time (in ms) for scroller to scroll
var previous = 0; // Variable to store previous position
var iframe = false;
var intCurActive = 1;
var sectionSelected = null;

function winOnLoad() {
	winOnResize(); // set initial position
	xAddEventListener(window, 'resize', winOnResize, false);
	xAddEventListener(window, 'scroll', winOnScroll, false);
	iframe = xGetElementById('theframe');
	if (iframe != false) {
		xHeight('theframe', xClientHeight() - 250);
	}
	xMoveTo('menu', 0, 130);
	return;
}

function winOnResize() {
	xMoveTo('menu', 0, xPageY('menu'));
	xShow('menu');
	winOnScroll(); // initial slide
	if (iframe != false) {
		xHeight('theframe', xClientHeight() - 250);
	}
}

function winOnScroll() {
	// If we've scrolled past the top of the page, or we're scrolling back up...
	if ((xScrollTop() > 120 && xHeight('menu') <= xClientHeight()) || (previous > xScrollTop())) {
		// Resize - if trying to move menu higher than 120 pixels down, stop it at 120
		xSlideTo('menu', xLeft('menu'), ((xScrollTop() < 120) ? 120 : xScrollTop()) + 10, slideTime);
		previous = xScrollTop();
	}
}

function toggleMenu (theMenu) {
	// Get references to the link and the menu contents
	var menu = xGetElementById('contents' + theMenu);
	var link = xGetElementById('link' + theMenu);

	// If we can't find the correct element, exit function
	if (menu == null) return false;

	// Work out if menu is hidden or not
	var isHidden = (menu.style.display == 'none');

	// Set variables to do opposite of current state of the menu (hide if showing, show if hidden)
	menu.style.display = isHidden ? 'block' : 'none';
	link.className = isHidden ? 'headingover' : 'heading';
	link.title = isHidden ? 'Click on this menu option to collapse the section' : 'Click on this menu option to expand the section';
	setCookie('sec' + theMenu, (isHidden ? 'yes' : 'no'));
}

function toggleSection (theName) {
	if (sectionSelected && sectionSelected != theName) {
		section = xGetElementById(sectionSelected);
		section.style.display = 'none';
	}

	sectionSelected = theName;
	section = xGetElementById(theName);
	section.style.display = (section.style.display == 'none') ? 'block' : 'none';
}

function setFlagText (theText) {
	var text = xGetElementById('flagtext');
	// If we can't find the correct element, exit function
	if (text == null) return false;

	text.innerHTML = theText;
}

function setCookie(name, value) {
  var curCookie = name + "=" + escape(value) + "; path=/;";
  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

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_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];}
}

function swapDepartment(deptid) {
	document.getElementById('deptbox'+intCurActive).style.display = 'none';
	document.getElementById('deptbox'+deptid).style.display = 'block';
	intCurActive = deptid;
	return false;
}