/*******************************************************************************
 * 
 * NonFauxColumns version 2.0
 * Copyright 2007, Jeff Mott <Mott.Jeff@gmail.com>. All rights reserved.
 * 
 * Redistribution and use in source and binary forms with or without
 * modification are permitted provided that the above copyright notice,
 * this condition, and the following disclaimer are retained.
 *
 * THIS SOFTWARE IS PROVIDED AS IS, AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT
 * LIMITED TO PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 ******************************************************************************/
	
function NonFauxColumns() {
	this._init();
}

NonFauxColumns.version = '2.0';

NonFauxColumns._instances = [];
NonFauxColumns._textSizeReferenceElement = null;
NonFauxColumns._textSizeReferenceHeight = -1;

NonFauxColumns.prototype =
{
	_init:
		function ()
		{
			this._boxes = [];
			this._areHeightsEqualized = false;
			this._areWidthsEqualized = false;

			NonFauxColumns._instances.push(this);
			if (NonFauxColumns._textSizeReferenceElement == null)
			{
				NonFauxColumns._textSizeReferenceElement = document.createElement("div");
				NonFauxColumns._textSizeReferenceElement.id = "NonFauxColumns-TextResizeReferenceElement";
				NonFauxColumns._textSizeReferenceElement.appendChild(document.createTextNode("|"));
				NonFauxColumns._textSizeReferenceElement.style.position = "absolute";
				NonFauxColumns._textSizeReferenceElement.style.visibility = "hidden";
				document.body.appendChild(NonFauxColumns._textSizeReferenceElement);
				NonFauxColumns._textSizeReferenceHeight = NonFauxColumns._textSizeReferenceElement.offsetHeight;
				setInterval(NonFauxColumns._checkTextResize, 50);
			}
		},

	addBox:
		function (box) {
			this._boxes.push(box);
		},

	equalizeHeights:
		function ()
		{
			// Find the tallest box in the set
			var tallest = -1;
			for (var i = 0; i < this._boxes.length; i++)
			{
				this._boxes[i].style.height = "auto";
				if (this._boxes[i].offsetHeight > tallest)
					tallest = this._boxes[i].offsetHeight;
			}

			// Set every box in the set to the height of the tallest
			for (var i = 0; i < this._boxes.length; i++)
			{
				this._boxes[i].style.height = tallest + "px";

				// Compensate for padding and border heights
				if (this._boxes[i].offsetHeight > tallest)
					this._boxes[i].style.height = tallest - (this._boxes[i].offsetHeight - tallest) + "px";
			}

			this._areHeightsEqualized = true;
		},

	equalizeWidths:
		function ()
		{
			// Find the widest box in the set
			var widest = -1;
			for (var i = 0; i < this._boxes.length; i++)
			{
				this._boxes[i].style.width = "auto";
				if (this._boxes[i].offsetWidth > widest)
					widest = this._boxes[i].offsetWidth;
			}

			// Set every box in the set to the width of the widest
			for (var i = 0; i < this._boxes.length; i++)
			{
				this._boxes[i].style.width = widest + "px";

				// Compensate for padding and border heights
				if (this._boxes[i].offsetWidth > widest)
					this._boxes[i].style.width = widest - (this._boxes[i].offsetWidth - widest) + "px";
			}

			this._areWidthsEqualized = true;
		}
};

NonFauxColumns._checkTextResize =
	function ()
	{
		if (NonFauxColumns._textSizeReferenceElement.offsetHeight != NonFauxColumns._textSizeReferenceHeight)
		{
			NonFauxColumns._autoEqualize();
			NonFauxColumns._textSizeReferenceHeight = NonFauxColumns._textSizeReferenceElement.offsetHeight;
		}
	};

NonFauxColumns._autoEqualize =
	function ()
	{
		for (var i = 0; i < NonFauxColumns._instances.length; i++)
		{
			if (NonFauxColumns._instances[i]._areHeightsEqualized)
				NonFauxColumns._instances[i].equalizeHeights();
			if (NonFauxColumns._instances[i]._areWidthsEqualized)
				NonFauxColumns._instances[i].equalizeWidths();
		}
	};

if (window.addEventListener)
	window.addEventListener('resize', NonFauxColumns._autoEqualize, false);
else if (window.attachEvent)
	window.attachEvent('onresize', NonFauxColumns._autoEqualize);
else
	window.onresize = NonFauxColumns._autoEqualize;
