
/* ***************************** */
/* ******** General Use ******** */
/* ***************************** */

// Object detection
var isW3C = (document.getElementById && document.createTextNode) ? true : false;
var isAll = (document.all) ? true : false;

// Takes the ID of an HTML element and returns an object reference
function getObj(elemID) {
var theObj = (isAll) ? document.all[elemID] : ((isW3C) ? document.getElementById(elemID) : null);
	return theObj;
} // end

// Add an event onload instead of using <body onload="functionName()"> - by Simon Willison
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
} // end

// Put CCTV onload events here
addLoadEvent(function() {
	nav();
	captureResize();
});


// Window popup
function openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
} // end


/* ******************** */
/* **** Navigation **** */
/* ******************** */
// Adjust nav height
function nav() {
if (getObj("nav")) {
	var intContainerHeight = getObj("container").offsetHeight;
	var intMainHeight='';
	if (getObj("main")) {
		intMainHeight = getObj("main").offsetHeight;
	}
	else if (getObj("main-full")) {
		intMainHeight = getObj("main-full").offsetHeight;
	}

	var objNav = getObj("nav");
	var intNavHeight = objNav.offsetHeight;
	var intBaseHeight = (intMainHeight>intNavHeight) ? intMainHeight : intNavHeight;
	var intNavBgImgHeight = 1039;
	var intBodyHeight = document.body.offsetHeight;

	if ( (intContainerHeight>intBaseHeight && intBaseHeight<intNavBgImgHeight) ) {	
		objNav.style.height = intContainerHeight-108 + 'px';
	}
}
} // end

function captureResize() {
	window.onresize = nav;
} // end


// Open and close sub-navigation
var strOpenNavId = ''; // track which sub nav is open
var objNav; // top-level nav object
var objNavSub; // sub nav of top-level nav object

function showHideSub(objId) {
getNavObjs(objId);

	if (objNavSub.className=='closed') { // OPEN BRANCH
		if ((strOpenNavId != objId) && (strOpenNavId != '')) { // close other one first
			hideSubnav(strOpenNavId);
		}
		else if (strOpenNavId == '') { // close other one first if one open by default
			switchClass('nav-links', 'opened', 'closed');
		}

		getNavObjs(objId);
		objNav.className = 'sub-open';
		objNavSub.className = 'opened';
		strOpenNavId = objId;
		adjustNavHeight();
	}
	else { // CLOSE BRANCH
		hideSubnav(objId);
	}
} // end

function hideSubnav(objId) {
getNavObjs(objId);

	objNav.className = 'sub-closed';
	objNavSub.className = 'closed';
	strOpenNavId = '';
return;
} // end

function getNavObjs(obj) {
	objNav = getObj(obj);
	objNavSub = getObj(obj+"-sub");
return;
} // end

// Looks for oldClass in nested ul and switches it to newClass
function switchClass(objId, oldClass, newClass) {
	if (document.getElementById) {
		navRoot = document.getElementById(objId);

		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				for (j=0; j<node.childNodes.length; j++) {
					nodesubnode = node.childNodes[j];
					if (nodesubnode.className == oldClass) {
						nodesubnode.className = newClass;
					}
				}
			}
		}
	 }	
return true;	 
} // end


// Changes height of nav area if an opened nav would go behind footer
function adjustNavHeight() {
var objNav = getObj("nav");
var intNavHeight = objNav.offsetHeight;
var intNavLinksHeight = getObj("nav-links").offsetHeight;
intNavLinksHeight += 57; // add height of button above links

	if ((intNavLinksHeight)>=intNavHeight) {
		objNav.style.height = intNavLinksHeight + 25 +"px";
	}
return;
} // end
