/* 
	Name: formatting.js
	Description: A collection of functions which use ECMAScript to unobtrusively modify the layout of HTML pages
	Notes: Presently this file is primarily used to allow the creation of equal height divs, as most layout should be performed using CSS
*/


/*
	Applet: EqualHeightDiv
	README: The ultimate goal of this applet is to iterate through a web page and find all div elements with a class name of "js_EqualHeightDiv" and format them with equal height.
		In order to use this function, the appropriate class names should be added to the divs, and the script should be loaded
		If desired, this script should be expanded so that groups of divs could be created by slightly modifying or appending to the class name, allowing for multiple matched sets of divs
*/

/* TODO -
	Accessing elements by classname may be faster than element type
*/

/* Settings */

var ClassName = "js_EqualHeightDiv";

EqualHeightDiv=function() {
	var divs, contDivs, maxHeight, divHeight, d;
	divs=document.getElementsByTagName('div');
	contDivs=[];
	maxHeight=0;
	/* Iterate through divs, check to see if they match, and store tallest */
	for(var i=0; i<divs.length;i++){
		if(/\bjs_EqualHeightDiv\b/.test(divs[i].className)){

			d=divs[i];
			contDivs[contDivs.length]=d;
			if (d.offsetHeight) {
				divHeight=d.offsetHeight;
			}
			else if (d.style.pixelHeight) {
				divHeight=d.style.pixelHeight;
			}
			maxHeight=Math.max(maxHeight,divHeight);
		}
	}
	
	/* Set heights of all elements to tallest */
	for (var i=0;i<contDivs.length;i++){
		contDivs[i].style.height=maxHeight+"px";
	}
}

function init() {
       // Set flag to only run once to allow for degredation
        if (arguments.callee.done) return;
       	arguments.callee.done = true;
		EqualHeightDiv();
}

/* Mozilla */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", init, false);
}

/* IE */
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
  if (this.readyState == "complete") {
    init(); // call the onload handler
  }
};
/*@end @*/


/* Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
  var _timer = setInterval(function() {
    if (/loaded|complete/.test(document.readyState)) {
      clearInterval(_timer);
      init(); // call the onload handler
    }
  }, 10);
}

/* Others */
window.onload=init;