/*
Copyright 2004 Joshua Paine
Contact 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, hundred, labels;
	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;
	hundred = 100;
	labels = this.getElementsByTagName('label');
	if(labels) labels = labels.length;
	if(labels && browser=="Internet Explorer" && version < 6) hundred = 90;
	width = "" + Math.floor((hundred-((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('ol','Columns',fspColumns,'load',false);
document.register('td','Columns',fspColumns,'load',false); // Someone needed this.
