// +----------------------------------+
// | GridLayout object                |
// | Copyright (C) 2008 Craig Manley  |
// +----------------------------------+
// $Id: GridLayout.js,v 1.3 2008/11/22 16:31:41 cmanley Exp $

var GridLayout = new (function() {

	// Replaces all elements in the given container with a table containing those elements.
	this.layout = function(container, cols) {
		if (!container.hasChildNodes()) {
			return false;
		}
		var children = [];
		// If the container contains a single table element with 'GridLayout' as dummy class name, then just redo the layout.
		if ((container.childNodes.length == 1) && (container.childNodes[0].nodeType != 3) && (container.childNodes[0].nodeType != 8) && (container.childNodes[0].nodeName == 'TABLE') && (container.childNodes[0].className == 'GridLayout')) {
			var table = container.childNodes[0];
			if ((table.tBodies.length == 0) || (table.tBodies[0].rows.length == 0)) {
				return false;
			}
			var rows = table.tBodies[0].rows;
			if (rows[0].cells.length == cols) {
				return true;
			}
			for (var r=0; r < rows.length; r++) {
				var row = rows[r];
				for (var c=0; c < row.cells.length; c++) {
					var cell = row.cells[c];
					if (!cell.childNodes.length) {
						continue;
					}
					children.push(cell.childNodes[0].cloneNode(true));
				}
			}
		}
		else {
			// Clone all child nodes that contain anything besides white-space and comments.
			for (var i = 0; i < container.childNodes.length; i++) {
				if ((container.childNodes[i].nodeType == 8) || ((container.childNodes[i].nodeType == 3) && (container.childNodes[i].nodeValue.replace(/^\s+|\s+$/g,'').length == 0))) {
					continue;
				}
				children.push(container.childNodes[i].cloneNode(true));
			}
			if (children.length == 0) {
				return false;
			}
		}
		container.style.display = 'none';
		// Do like Magda Goebbels.
		while (container.hasChildNodes()) {
			container.removeChild(container.firstChild);
		}
		// Create a nice table layout of the cloned child nodes.
		var table = document.createElement('table');
		table.setAttribute('cellpadding','0');
		table.setAttribute('cellspacing','0');
		table.setAttribute('width','100%');
		table.setAttribute('border','0');
		table.className = 'GridLayout';
		table.style.marginLeft = 'auto';
		table.style.marginRight = 'auto';
		// Make all cells equal in width.
		var colw = Math.round(100 / cols);
		var colgroup = document.createElement('colgroup');
		for (var i = 0; i < cols; i++) {
			var col = document.createElement('col');
			col.setAttribute('width', colw + '%');
			colgroup.appendChild(col);
		}
		table.appendChild(colgroup);
		// Create the body, rows and cells.
		var tbody = document.createElement('tbody');
		var y = 0;
		for (var i = 0; i < children.length; i += cols) {
			var tr = document.createElement('tr');
			for (var y = 0; y < cols; y++) {
				var td = document.createElement('td');
				td.setAttribute('width', colw + '%');
				td.setAttribute('valign', 'top');
				td.setAttribute('align', 'left');
				td.style.whiteSpace = 'nowrap';
				if (i + y < children.length) {
					td.appendChild(children[i + y]);
				}
				tr.appendChild(td);
			}
			tbody.appendChild(tr);
		}
		table.appendChild(tbody);
		container.appendChild(table);
		// Force MSIE to redraw the changes.
		if ((navigator.appVersion.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Opera') == -1)) {
			container.innerHTML += '';
		}
		container.style.display = '';
		return true;
	}

})();
