function GridLayout(columns, rows, spacing)
{
	extend(this, Layout);
	
	this.columns = (columns ? columns : 1);
	this.rows = (rows ? rows : 1);
	this.spacing = (spacing ? spacing : 0);
}

GridLayout.prototype = new Layout();

GridLayout.prototype.doLayout = function(container)
{
	var count = container.getWidgetCount();
	var widgetWidth = (container.getWidth() - this.spacing * (count + 1)) / this.columns;
	var widgetHeight = (container.getHeight() - this.spacing * (count + 1)) / this.rows;
	var offsX = 0;
	var offsY = 0;
	
	for(var i = 0; i < count; i++)
	{
		var widget = container.getWidget(i);
		
		widget.setBounds(offsX, offsY, widgetWidth, widgetHeight);
		
		if(i > 0 && ((i + 1) % this.columns) == 0)
		{
			offsY += widgetHeight + this.spacing;
			offsX = 0;
		} else
		{
			offsX += widgetWidth + this.spacing;
		}
	}
}

GridLayout.prototype.getPreferredSize = function(container)
{
	var count = container.getWidgetCount();
	var w = 0, h = 0;
	var done = false;
	
	for(var y = 0; y < this.rows && !done; y++)
	{
		var maxHeight = 0, maxWidth = 0;
		for(var x = 0; x < this.columns && !done; x++)
		{
			var i = y * this.rows + x;
			
			if(i >= count)
			{
				done = true;
			} else
			{
				var widget = container.getWidget(i);
				var size = widget.getPreferredSize();
				
				maxWidth += size.width
				maxHeight = Math.max(maxHeight, size.height);
			}
		}
		
		w = Math.max(w, maxWidth);
		h = Math.max(h, maxHeight);
	}

	return {width:w, height:h * this.rows};
}

GridLayout.prototype.toString = function()
{
	return "GridLayout [columns = " + this.columns + ", rows = " + this.rows + ", spacing = " + this.spacing + "]";
}

