//maked all added functions work onload. at the end of each function you want to work onload add addLoadEvent(function); to the end of the function.
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
ge = function(id){ return document.getElementById(id) }

// The following two functions make it possible to have web standard popups
window.onload = function() {
  if (!document.getElementsByTagName) return false;
  var lnks = document.getElementsByTagName("a");
  for (var i=0; i<lnks.length; i++) {
    if (lnks[i].className == "popup") {
      lnks[i].onclick = function() {
        popUp(this.getAttribute("href"));
        return false;
      }
    }
    if (lnks[i].className == "galleryPopup") {
      lnks[i].onclick = function() {
        popUp2(this.getAttribute("href"));
        return false;
      }
    }
    if (lnks[i].className == "popupThreeRings") {
      lnks[i].onclick = function() {
        popUp3(this.getAttribute("href"));
        return false;
      }
    }
    if (lnks[i].className == "timelinePopup") {
      lnks[i].onclick = function() {
        popUp4(this.getAttribute("href"));
        return false;
      }
    }
  }
}
function popUp(winURL) {
  window.open(winURL,"popup");
}
function popUp2(winURL) {
  window.open(winURL,"galleryPopup");
}
function popUp3(winURL) {
  window.open(winURL,"popupThreeRings","width=550,height=550");
}
function popUp4(winURL) {
  window.open(winURL,"timelinePopup","width=810,height=435");
}

//-- Create a :Hover psuedo attribute for IE for header and footer -- //
liHover = function() {
	if (!ge('headerNav')) return false;
	if (!ge('footerNav')) return false;
	if(ieBelowSix != 1) return false;
	var hdrCat = ge("headerNav").getElementsByTagName("li");
	var ftrCat = ge("footerNav").getElementsByTagName("li");
	for (var i=0; i<hdrCat.length; i++) {
		hdrCat[i].onmouseover=function() {
			this.className+=" hdrhover";
		}
		hdrCat[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" hdrhover\\b"), "");
		}
	}
	for (var j=0; j<ftrCat.length; j++) {
		ftrCat[j].onmouseover=function() {
			this.className+=" ftrhover";
		}
		ftrCat[j].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" ftrhover\\b"), "");
		}
	}
}
addLoadEvent(liHover);

//Function to set colored rows in the cascading header menu
function headerRows() {
	//check to see if the elements needed to run the function are available
	if (!document.getElementById) return false;
	if (!document.getElementsByTagName) return false;
	if (!ge('headerNav')) return false;
	var uls = ge('headerNav').getElementsByTagName('ul'); //get all of the ul elements in headerNav
	//loop through all of the ul's
	for (var i = 0; i < uls.length; i++) {
		var rows = uls[i].getElementsByTagName('li'); //get all of the li elements in the ul's in headerNav
		//loop through all of the li's
		for (var j = 0; j < rows.length; j++) {
			if (j % 2) { //if its even set the class of the a tag to row 2
				rows[j].childNodes[0].className = 'row2';
			} else { //if its od set the class of the a tag to row 1
				rows[j].childNodes[0].className = 'row1';
			}		
		}
	}	
}
addLoadEvent(headerRows);

//Function to set colored rows in the cascading footer menu
function footerRows() {
	//check to see if the elements needed to run the function are available
	if (!document.getElementById) return false;
	if (!document.getElementsByTagName) return false;
	if (!ge('footerNav')) return false;
	var uls = ge('footerNav').getElementsByTagName('ul'); //get all of the ul elements in footerNav
	//loop through all of the ul's
	for (var i = 0; i < uls.length; i++) {
		var rows = uls[i].getElementsByTagName('li'); //get all of the li elements in the ul's in footerNav
		//loop through all of the li's
		for (var j = 0; j < rows.length; j++) {
			if (j % 2) { //if its even set the class of the a tag to row 2
				rows[j].childNodes[0].className = 'row2';
			} else { //if its od set the class of the a tag to row 1
				rows[j].childNodes[0].className = 'row1';
			}		
		}
	}	
}
addLoadEvent(footerRows);

//Function to set colored rows in the cascading main_2 menu
function main2Rows() {
	//check to see if the elements needed to run the function are available
	if (!document.getElementById) return false;
	if (!document.getElementsByTagName) return false;
	if (!ge('main_2')) return false;
	var uls = ge('main_2').getElementsByTagName('ul'); //get all of the ul elements in main_2
	//loop through all of the ul's
	for (var i = 0; i < uls.length; i++) {
		var rows = uls[i].getElementsByTagName('li'); //get all of the li elements in the ul's in main_2
		//loop through all of the li's
		for (var j = 0; j < rows.length; j++) {
			if (j % 2) { //if its even set the class of the a tag to row 2
				rows[j].className = 'row2';
			} else { //if its od set the class of the a tag to row 1
				rows[j].className = 'row1';
			}		
		}
	}	
}
addLoadEvent(main2Rows);

//Function to allow height of page to always extent to meet footer
function setContentHeight() {
	//check to see if the elements needed to run the function are available
	if (!document.getElementById) return false;
	if (!ge('header')) return false;
	if (!ge('content')) return false;
	if (!ge('footer')) return false;	
	hdrHght = ge('header').offsetHeight; //get the height of the header div
	cntHght = ge('content').offsetHeight; //get the height of the content div
	ftrHght = ge('footer').offsetHeight; //get the height of the footer div
	bdyHght = document.body.offsetHeight; //get the height of the body
	totHght = hdrHght + cntHght + ftrHght; //add the three div heights together
	if (totHght < bdyHght) { //check to see if the height needs to be extended
		difHght = bdyHght - totHght; //get the amount of height that needs to be added in order to extend the content div to the bottom
		if (difHght < 70) {
			ge('content').style.height = ((cntHght + difHght) + 'px'); //set the new height that will take it down to  meet the footer.
		} else {
			ge('content').style.height = ((cntHght + difHght - 70) + 'px'); //set the new height that will take it down to  meet the footer.
		}
	}
}
addLoadEvent(setContentHeight);

//allow a scrollbar to appear when the page width is below the minimum set width
function setWindowWidth() {
	if (!document.getElementById) return false;
	if (!ge('wrap')) return false;
	if (!ge('header')) return false;
	if (!ge('footer')) return false;
	bodyWidth = document.body.offsetWidth;
	//if the window width is less than the minimm width of the site perform the function
	if (bodyWidth < 938) {
		//get half of the window width, round down and add one
		var mrgnLft = Math.floor((bodyWidth/2) + 1);
		//set the margin-left of the following id to the new number
		if (ge('home') != null) {
			ge('wrap').style.marginLeft = (mrgnLft * -1)+'px'
			ge('header').style.marginLeft = (mrgnLft * -1)+'px'
			ge('footer').style.marginLeft = (mrgnLft * -1)+'px'
		} else {			
			ge('wrap').style.marginLeft = ((mrgnLft * -1)+32)+'px'
			ge('header').style.marginLeft = (mrgnLft * -1)+'px'
			ge('footer').style.marginLeft = ((mrgnLft * -1)+32)+'px'
		}
	} else {
		if (ge('home') != null) {
			ge('wrap').style.marginLeft = '-470px'
			ge('header').style.marginLeft = '-470px'
			ge('footer').style.marginLeft = '-470px'
		} else {
			ge('wrap').style.marginLeft = '-470px'
			ge('header').style.marginLeft = '-502px'
			ge('footer').style.marginLeft = '-470px'
		}
	}
}
addLoadEvent(setWindowWidth);

//allow a scrollbar to appear when the page width is below the minimum set width
window.onresize=function() {
	setWindowWidth();
	setContentHeight();
}

//show and hide admissions faqs
function showHideFaqs() {
	if (!ge('admissionsfaqs')) return false;
	questions = ge('admissionsfaqs').getElementsByTagName('h5');
	answers = ge('admissionsfaqs').getElementsByTagName('p');
	showAll = document.getElementById('showallfaq');
	for (var i=0; i<answers.length; i++) {
		answers[i].style.display = 'none';
	}
	for (var i=0; i<questions.length; i++) {
		questions[i].onclick=function() {
			answ = getNextElement(this.nextSibling);
			if (answ.style.display != 'block') {
				answ.style.display = 'block';
			} else {
				answ.style.display = "none";
			}		
		}
	}
	toggleAll();
}
addLoadEvent(showHideFaqs);

function getNextElement(node) {
	if(node.nodeType == 1) {
	return node;
	}
	if (node.nextSibling) {
		return getNextElement(node.nextSibling);
	}
	return null;
}

function toggleAll() {
	answers = ge('admissionsfaqs').getElementsByTagName('p');
	showAll.onclick=function() {
		if (showAll.innerHTML != 'Hide All Answers') {
			for (var i=0; i<answers.length; i++) {
				answers[i].style.display = 'block';
			}
			showAll.innerHTML = 'Hide All Answers';
		} else {
			for (var i=0; i<answers.length; i++) {
				answers[i].style.display = 'none';
			}
			showAll.innerHTML = 'Show All Answers';	
		}
	}
}

//**************************************************Replace align attribute with class**************************************************//
function replaceAlign() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementsByTagName('img')) return false;
  var imgList = document.getElementsByTagName('img');
  for (var i=0; i<imgList.length; i++) {
    var align = imgList[i].getAttribute('align');
		if (align) {
			if (align == 'left') {
				imgList[i].className = 'left';
			}
			if (align == 'right') {
				imgList[i].className = 'right';
			}
			if (align == 'middle') {
				imgList[i].className = 'middle';
			}
    	imgList[i].removeAttribute('align')
    }
  }
}
addLoadEvent(replaceAlign);