// matchcolumns.js - version 1.01 2008-07-21
//
// A JavaScript class for making column divs have equal lengths
//
// base: http://www.webaware.com.au/free/matchcolumns/
//
// copyright © 2008 WebAware Pty Ltd
//---------------------------------------------------------------------
// To make column divs have equal lengths, give each the same class and
// call MatchColumns.MatchClass() with the class name.
// Mix-in classes are supported, e.g. class="container column"
//---------------------------------------------------------------------
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// Full license: http://www.webaware.com.au/free/license.htm
//---------------------------------------------------------------------

// class constructor
function MatchColumns(className) {
	this.className = className;
}

// behind-the-scenes class factory
MatchColumns.MatchClass = function(className) {
	var newInstance = "v_MatchColumns_" + className;
	eval(newInstance + " = new MatchColumns('" + className + "');");

	MatchColumns.HookEvent(window, "load", Function(newInstance + ".Activate()"));
}

// simplistic event hooking for (on)load, (on)unload, etc.
MatchColumns.HookEvent = function(obj, event, hook) {
	if (obj.addEventListener) obj.addEventListener(event, hook, false);
	else if (obj.attachEvent) obj.attachEvent("on" + event, hook);
}

// activate the class - iterate over all divs looking for ones to match lengths of
MatchColumns.prototype.Activate = function() {
	var matchClassname = new RegExp("\\b" + this.className + "\\b");
	var columns = new Array();
	var divs = document.getElementsByTagName('div');
	var maxlen = 0;
	var i;

	// iterate over all <div> elements in the document
	for (i in divs) {
		var d = divs[i];

		// collect all divs with matching class name
		if (matchClassname.test(d.className)) {
			columns[columns.length] = d;

			// calculate max length of columns seen so far
			maxlen = Math.max(maxlen, d.offsetHeight ? d.offsetHeight : d.style.pixelHeight ? d.style.pixelHeight : null);
		}
	}

	// assign maximum height value to all of container <div> elements
	for (i in columns)
		columns[i].style.height = maxlen + "px";
}
