var draw = false;
var firstDraw = false;
var lH = 0;
var cH = 0;
var rH = 0;
var adjH = 0;

function dataBoxExpand(){
	var sideBoxDivs = this.parentNode.getElementsByTagName("div");
	for(var i=0; i< sideBoxDivs.length; i++){
		if(sideBoxDivs[i].className == "content"){
			var contentNode = sideBoxDivs[i];
		}
	}
	if(contentNode.style.display != "none"){
		new Effect.BlindUp(contentNode, {duration: 0.25});
		this.className = "headerCollapsed";
		//GB 08/04/2007 Fixed (or semi-fixed) further bug with footer
		adjH -= contentNode.getDimensions().height;
	}else{
		new Effect.BlindDown(contentNode, {duration: 0.25});
		this.className = "header";
		//GB 08/04/2007 Fixed (or semi-fixed) further bug with footer
		adjH += contentNode.getDimensions().height;
	}

	//GB 08/04/2007 Fixed (or semi-fixed) further bug with footer
	setWidth();
	attachBoxExpand();
}

function attachBoxExpand (){
	var divs = document.getElementsByTagName("div");
	for(var i=0; i< divs.length; i++){
		if(divs[i].className.substr(0,7) == "sideBox"){
			var subdivs = divs[i].getElementsByTagName("div");
			for(var j=0; j<subdivs.length; j++){
				//if(subdivs[j].className == "header"){
				//GB 2006-11-14 fixed bug
				if(subdivs[j].className == "header" || subdivs[j].className == "headerCollapsed"){
					subdivs[j].onclick = dataBoxExpand;
				}
			}
		}
		//GB - Testing using this routine for other sections of site
        //if(divs[i].className.substr(0,8) == "slideBox"){
        //	var subdivs = divs[i].getElementsByTagName("div");
        //	for(var j=0; j<subdivs.length; j++){
        //		if(subdivs[j].className == "title"){
        //			subdivs[j].onclick = dataBoxExpand;
        //		}
        //	}
        //}
	}
}
function menuRollover(){
	var myChildren = this.getElementsByTagName("ul");
	if(myChildren.length == 1){
		myChildren[0].style.visibility = "visible";
	}
}
function menuRollout(){
	var myChildren = this.getElementsByTagName("ul");
	if(myChildren.length == 1){
		myChildren[0].style.visibility = "hidden";
	}
}
function attachMenuRollover (){
	var menu = document.getElementsByTagName("li");
	for(var i=0; i< menu.length; i++){
		//alert(menu[i].id);
		if(menu[i].className == "topLevel"){
			menu[i].onmouseover = menuRollover;
			menu[i].onmouseout = menuRollout;
		}
	}
}
function f_filterResults(n_win, n_docel, n_body) {
	alert(document.documentElement.scrollWidth + " - " + n_win + " - " + n_docel + " - " + n_body)
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function xDef()
{
  for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])=='undefined') return false;}
  return true;
}
function xGetElementById(e)
{
  if(typeof(e)=='string') {
    if(document.getElementById) e=document.getElementById(e);
    else if(document.all) e=document.all[e];
    else e=null;
  }
  return e;
}
function xNum()
{
  for(var i=0; i<arguments.length; ++i){if(isNaN(arguments[i]) || typeof(arguments[i])!='number') return false;}
  return true;
}
function xStr(s)
{
  for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])!='string') return false;}
  return true;
}
function xClientHeight()
{
  var v=0,d=document,w=window;
  if(d.compatMode == 'CSS1Compat' && !w.opera && d.documentElement && d.documentElement.clientHeight)
    {v=d.documentElement.clientHeight;}
  else if(d.body && d.body.clientHeight)
    {v=d.body.clientHeight;}
  else if(xDef(w.innerWidth,w.innerHeight,d.width)) {
    v=w.innerHeight;
    if(d.width>w.innerWidth) v-=16;
  }
  return v;
}
function xGetComputedStyle(oEle, sProp, bInt) {
  var s, p = 'undefined';
  var dv = document.defaultView;
  if(dv && dv.getComputedStyle){
    s = dv.getComputedStyle(oEle,'');
    if (s) p = s.getPropertyValue(sProp);
  }
  else if(oEle.currentStyle) {
    // convert css property name to object property name for IE
    var i, c, a = sProp.split('-');
    sProp = a[0];
    for (i=1; i<a.length; ++i) {
      c = a[i].charAt(0);
      sProp += a[i].replace(c, c.toUpperCase());
    }
    p = oEle.currentStyle[sProp];
  }
  else return null;
  return bInt ? (parseInt(p) || 0) : p;
}
function xHeight(e,h){
	if(!(e=xGetElementById(e))) return 0;
	if (xNum(h)) {
		if (h<0) h = 0;
		else h=Math.round(h);
	}
	else h=-1;
	var css=xDef(e.style);
	if (e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
		h = xClientHeight();
	}
	else if(css && xDef(e.offsetHeight) && xStr(e.style.height)) {
		if(h>=0) {
		  var pt=0,pb=0,bt=0,bb=0;
		  if (document.compatMode=='CSS1Compat') {
			var gcs = xGetComputedStyle;
			pt=gcs(e,'padding-top',1);
			if (pt !== null) {
			  pb=gcs(e,'padding-bottom',1);
			  bt=gcs(e,'border-top-width',1);
			  bb=gcs(e,'border-bottom-width',1);
			}
			// Should we try this as a last resort?
			// At this point getComputedStyle and currentStyle do not exist.
			else if(xDef(e.offsetHeight,e.style.height)){
			  e.style.height=h+'px';
			  pt=e.offsetHeight-h;
			}
		  }
		  h-=(pt+pb+bt+bb);
		  if(isNaN(h)||h<0) return;
		  else e.style.height=h+'px';
		}
		h=e.offsetHeight;
	}
	else if(css && xDef(e.style.pixelHeight)) {
		if(h>=0) e.style.pixelHeight=h;
		h=e.style.pixelHeight;
	}
	return h;
}
function setWidth(){
	if(!lH){lH=xHeight("searchPanel");}
	if(!cH){cH=xHeight("content");}
	if(!rH){rH=xHeight("subNav");}
	
	var newHeight = Math.max(cH,rH,(xClientHeight()-xHeight("header") - xHeight("footer")));
	//GB 2006-11-28 Fixed a bug with the footer position
	if (draw)
		newHeight = Math.max(newHeight,lH+xHeight("header")+20);
	else
		//newHeight = Math.max(newHeight,lH);
		newHeight = Math.max(newHeight,lH+xHeight("header")+20);
	if((window.innerHeight-xHeight("header") - xHeight("footer")) > newHeight){
		newHeight = window.innerHeight-xHeight("header") - xHeight("footer") - 10;
	}
	if (newHeight+adjH > newHeight)
		newHeight += adjH;
	xHeight("subNav", newHeight);
	document.getElementById("footer").style.visibility = 'visible';
	if(draw){
		if(document.body.clientWidth<910){
			document.getElementById("login").style.position = "absolute";
			document.getElementById("login").style.left = "750px";
			document.getElementById("subNav").style.left = "750px";
			document.getElementById("content").style.marginRight = document.body.clientWidth - 745;
		}else{
			document.getElementById("login").style.position = "relative";
			document.getElementById("login").style.left = "auto";
			document.getElementById("subNav").style.left = "auto";
			document.getElementById("content").style.marginRight = "165px";
		}
		document.getElementById("footer").style.position = "absolute";
		document.getElementById("footer").style.width = "100%";
		document.getElementById("footer").style.top = newHeight + xHeight("header");
	}else{
		//GB 2006-11-28 Fixed a bug with the footer position
		//xHeight("content", newHeight);
	}
}
function switchOn(){
	document.body.id = '';
}
function checkSwitch(seq){
	if(document.body.id == 'switchoff'){
		document.body.id = 'switch';
		window.setTimeout("checkSwitch(0)", 1000);
	}else{
		var timeout = 100;
		if(seq < 6){
			document.body.id = 'switch' + seq;
			timeout = 60
		}else{
			document.body.id = '';
		}
		seq++;
		if(seq == 6){
			timeout = 600;
		}
		if(seq != 7){
			window.setTimeout("checkSwitch(" + seq + ")", timeout);
		}
	}
}

function myOnLoad(){
	//checkSwitch(0);
	setWidth();
	attachBoxExpand();
	attachMenuRollover();
}
function myOnResize(){
	setWidth();
}

window.onload = myOnLoad;
window.onresize = myOnResize;
