/*
/////////////////////////////////
Javascript File for IBLS
v1 : 09 Aug 06 : Matt Casey : source23.com

All code here is unobtrusive, ie. if it doesn't work for the
user - say if they have turned off javascript - the site is 
still functional

/////////////////////////////////
*/

/*
/////////////////////////////////
	Make <div> elements the same height. 
	Usefullfor column layouts
	Parses docuemnt matches divs with a classname of "matchedheight"
	Finds the height of tallest <div> and assigns that height to rest of group
/////////////////////////////////
*/
function matchHeight(){
	var divs,contDivs,maxHeight,divHeight,d;
	// get all <div> elements in the document
	divs=document.getElementsByTagName('div');
	contDivs=[];
	// initialize maximum height value
	maxHeight=0;
	// iterate over all <div> elements in the document
	for(var i=0;i<divs.length;i++){
		// make collection with <div> elements with class attribute 'matchedheight'
		if(/\bmatchedheight\b/.test(divs[i].className)){
			d=divs[i];
			contDivs[contDivs.length]=d;
			// determine height for <div> element
			if(d.offsetHeight){
				divHeight=d.offsetHeight;
			}else if(d.style.pixelHeight){
				divHeight=d.style.pixelHeight;
			}
			// calculate maximum height
			maxHeight=Math.max(maxHeight,divHeight);
		}
	}
	if(!maxHeight || maxHeight < 20) maxHeight = 20;
	//alert(contDivs.length+' : '+maxHeight);
	// assign maximum height value to all of container <div> elements
	for(var i=0;i<contDivs.length;i++){
		contDivs[i].style.height=maxHeight+'px';
	}
}

/*
/////////////////////////////////
	Auto gen a popup window of specified size, based on a normal <a> tag.
	Usefull for maps, disclaimers etc...
	Parses document looking for <a> tags with a class of "popup"
	Expects to find attributes of w and h to use as height for new window
	eg.  <a href="myfile.html" class="popup" w="500" h="600">
	Will open a new window of 500x600 pixels.
	Non-supporting browsers will fall back to using a standard new window
/////////////////////////////////
*/
function doPopupImage() {
	if (!document.getElementsByTagName) return false;
	var links = document.getElementsByTagName("a");
	for (var i=0; i < links.length; i++) {
		if (links[i].className.match("popup")) {
			links[i].onclick = function() {
				var w = this.getAttribute('w') ? parseInt(this.getAttribute('w')) : 300;
				var h = this.getAttribute('h') ? parseInt(this.getAttribute('h')) : 300;
				window.open(this.href,'','toolbar=0,resizable=1,menubar=0,scrollbars=1,width='+w+',height='+h);
				return false;
			}//end func
		}//end if
	}//end if
}//end func


/*
/////////////////////////////////
	Table striper
	Parses documents looking for tables with class of "stripe"
	Sets class name of alternate rows to be r0, r1 respectivly
	Use CSS to make a style rule for tr.r0 and tr.r1
/////////////////////////////////
*/
function setTableRowColors(){
	if (!document.getElementsByTagName) return false;
	var tables = document.getElementsByTagName("table");
	for (var i=0; i < tables.length; i++) {
		if(tables[i].className.match("stripe")) {
			var rows = tables[i].getElementsByTagName("tr");  
			for(r = 0; r < rows.length; r++){ 
				if(r % 2 == 0){
	       		rows[r].className = "r0";
				}else{
					rows[r].className = "r1";
				}//end if
			}//end for
		}//end if
	}//end for
}//end func


/*
/////////////////////////////////
	WINDOW ONLOADER
	Wrapper function to allow multiple 
	onload events to be attached to the document.
/////////////////////////////////
*/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

/*
/////////////////////////////////
	Add load events to the document, use of function above.
/////////////////////////////////
*/
addLoadEvent(function() {
  /* more code to run on page load */ 
	if(document.getElementsByTagName){
		//Only gets here if browser supports getElementsByTagName eg. version IE 5.5/Firefox/Safari etc.
		matchHeight();
		setTableRowColors();
		//doPopupImage();  //If used
	}
});