/**
* TabularWidgetLayout
*
* A table based widget layout.  
*
* @param style_set
*   -style_set_twl_table
*   _style_set_twl_table_body
*   -style_set_twl_row
*   -style_set_twl_cell
* @param column_count The maximum number of columns per row in the table layout
**/
function TabularWidgetLayout( style_set, column_count ) { 

	this.style_set = style_set;
	this.column_count = column_count;

	this.widget_table = document.createElement( 'table' );
	this.widget_table.className = this.style_set + "_twl_table";

	this.table_body = document.createElement( 'tbody' );
	this.table_body.className = this.style_set + "_twl_table_body";

	this.widget_table.appendChild( this.table_body );

	this.row_pointer = 0;
	this.col_pointer = 0;

	//init the first row
	var new_row = document.createElement( 'tr' );
	new_row.className = this.style_set + "_twl_table";
	this.table_body.appendChild( new_row );

}

new TabularWidgetLayout();

/**
* TabularWidgetLayout.addWidget
*
* Add a widget to the end of the layout
*
* @param w The widget object to add
**/
TabularWidgetLayout.prototype.addWidget = function( w ) { 

	//check to see if the col_pointer is >= column_count
	if ( this.col_pointer >= this.column_count ) {
		var new_row = document.createElement( 'tr' );
		new_row.className = this.style_set + "_twl_row";
		this.table_body.appendChild( new_row );
		this.row_pointer++;
		this.col_pointer=0;
	}

	var new_col = document.createElement( 'td' );
	new_col.className = this.style_set + "_twl_cell";

	new_col.appendChild( w.getWidgetDOM() );

	var cur_row = this.table_body.getElementsByTagName( 'tr' )[ this.row_pointer ];
	
	cur_row.appendChild( new_col );

	this.col_pointer++;

}

/**
* TabularWidgetLayout.clearWidgets
*
* Remove all widgets from the layout
**/
TabularWidgetLayout.prototype.clearWidgets = function() { 

	this.widget_table.removeChild( this.table_body );

	this.table_body = document.createElement( 'tbody' );
	this.table_body.className = this.style_set + "_twl_table_body";

	this.widget_table.appendChild( this.table_body );

	this.row_pointer = 0;
	this.col_pointer = 0;

	//init the first row
	var new_row = document.createElement( 'tr' );
	new_row.className = this.style_set + "_twl_table";
	this.table_body.appendChild( new_row );

}

	

/**
* TabularWidgetLayout.getDOM
*
* @returns The DOM table object
**/
TabularWidgetLayout.prototype.getDOM = function() { 

	return this.widget_table;

}

/**
* TabularWidgetLayoutFactory
*
* @param style_set
* @param col_count
**/
function TabularWidgetLayoutFactory( style_set, col_count ) { 

	this.style_set = style_set;
	this.col_count = col_count;

}

new TabularWidgetLayoutFactory();

TabularWidgetLayoutFactory.prototype.build = function() { 

	return new TabularWidgetLayout( this.style_set, this.col_count );

}


