function FlowLayout(hSpacing, vSpacing)
{
	extend(this, Layout);
	
	this.hSpacing = (hSpacing ? hSpacing : 0);
	this.vSpacing = (vSpacing ? vSpacing : 0);
}

FlowLayout.prototype = new Layout();

FlowLayout.prototype.doLayout = function(container)
{
	var width = container.getWidth(), height = container.getHeight();
	var offsetX = this.hSpacing, offsetY = this.vSpacing;
	var maxY = 0;
	var count = container.getWidgetCount();
	
	for(var i = 0; i < count; i++)
	{
		var widget = container.getWidget(i);
		var preferredSize = widget.getPreferredSize();
		
		if(offsetX + preferredSize.width > width && offsetX > 0)
		{
			offsetY += maxY + this.vSpacing;
			offsetX = this.hSpacing;
			maxY = 0;
		}
		
		widget.setBounds(offsetX, offsetY, preferredSize.width, preferredSize.height);
		
		offsetX += preferredSize.width + this.hSpacing;
		maxY = Math.max(maxY, widget.getHeight());
	}
}

FlowLayout.prototype.getPreferredSize = function(container)
{
	var count = container.getWidgetCount();
	var w = this.hSpacing, h = this.vSpacing;
	var maxWidth = 0, maxHeight = 0;
	
	for(var i = 0; i < count; i++)
	{
		var widget = container.getWidget(i);
		var preferredSize = widget.getPreferredSize();
		var newWidth = w + preferredSize.width + this.hSpacing;

		if(newWidth > container.getWidth())
		{
			maxWidth = Math.max(maxWidth, w);
			h += maxHeight + this.vSpacing;
			
			w = this.hSpacing;
			maxHeight = 0;
		}
		
		w += preferredSize.width + this.hSpacing;
		maxHeight = Math.max(maxHeight, preferredSize.height);
	}
	
	return {width:maxWidth, height:h};	
}

