/*
Columns
FairSky Pages Component
-----------------------
Copyright 2004 Joshua Paine
Created by Joshua Paine of FairSky Networks <http://fairsky.us/>
Contact at <http://fairsky.us/contact>
Updated 2004-10-11

The latest version should be available at
<http://demo.fairsky.us/javascript/columns.html>

You may copy, reuse, or produce derivative works of this code
only under the terms of the Linky License v0.1 or later.
The Linky License can be found at <http://fairsky.us/linky>.
The main points of the license are:
	1) Do not alter or remove this notice.
	2) Notify me of your usage through one of the means listed
	   at <http://fairsky.us/contact>.
*/

function fspColumnsBlank(tag)
{
	d = document.createElement(tag);
	s = d.style;
	s.display = "block";
	s.clear = "both";
	s.visibility = "hidden";
	s.height = "1px";
	s.overflow = "hidden";
	s.lineHeight = "1px";
	s.fontSize = "1px";
	t = document.createTextNode('_');
	d.appendChild(t);
	return d;
}

function fspColumns()
{
	var regex, numCols, gutter, c, total, x, y, width, s, d, t, tag;
	regx = /Fsp\.Columns:([0-9]+)/;
	numCols = this.className.match(regx);
	if(numCols && numCols[1]) numCols = 1*numCols[1];
	else numCols = 2;
	regx = /Columns\.Gutter:([0-9]+)/;
	gutter = this.className.match(regx);
	if(gutter && gutter[1]) gutter = 1*gutter[1];
	else gutter = 3;
	c = this.childNodes;
	total = 0;
	for(x=0; x<c.length; x++) if(c[x].nodeType==1) ++total;
	x = 0;
	y = 0;
	width = "" + Math.floor((100-((numCols-1)*gutter))/numCols) + "%";
	gutter = "" + gutter + "%";
	if(this.tagName.toLowerCase() == 'ul')
	{
		this.style.paddingLeft = '0';
		this.style.marginLeft = '0';
	}
	while(x<c.length)
	{
		if(c[x].nodeType==1)
		{
			s = c[x].style;
			s.cssFloat = "left";
			s.styleFloat = "left";
			s.width = width;
			s.display = "block";
			if(y%numCols == 0)
			{
				if(y)
				{
					if(c[x].tagName.toLowerCase()=='li') tag = 'li';
					else tag = 'span';
					this.insertBefore(fspColumnsBlank(tag),c[x]);
					++x;
				}
				s.clear = "left";
				s.marginLeft = '0';
			}
			else
			{
				s.clear = "none";
				s.marginLeft = gutter;
			}
			if(this.tagName.toLowerCase() == 'ul')
			{
				s.listStylePosition = 'inside';
			}
			++y;
		}
		++x;
	}
	d = fspColumnsBlank('span');
	this.appendChild(d);
}

document.register('p','Columns',fspColumns,'load',false);
document.register('div','Columns',fspColumns,'load',false);
document.register('ul','Columns',fspColumns,'load',false);
document.register('td','Columns',fspColumns,'load',false); // Someone needed this.