
var Table = new Class(
{
	Implements: Options,

	options:
	{
			className: null // default class added to the table
	},

	/*
	 * Constructor for this class
	 *
	 * @param Element element
	 * @param Object options
	 */
	initialize: function (element, options)
	{
		this.table = $(element);
		if ($type(this.table) != 'element')
			return;

		this.tableHead = this.table.getElement('thead');
		this.tableBody = this.table.getElement('tbody');
		this.setOptions(options);

		Table.instances[element] = this;

		if (this.options.className)
			this.table.addClass(this.options.className);
	},

	/*
	 * Returns the current number of rows
	 *
	 * @return int
	 */
	countRows: function ()
	{
		if (this.tableBody)
			return this.tableBody.getElements('tr').length;
		else
			return 0;
	},

	/*
	 * Adds a new row to the table, filling the columns with the data from the passed array
	 *
	 * @param Array elements
	 */
	addNewRow: function (elements)
	{
		if ($type(elements) == "array")
		{
			var row = new Element('tr');
			for (i = 0; i < elements.length; i++)
			{
				if ($type(elements[i]) == 'element')
					row.adopt(new Element('td').adopt(elements[i]));
				else
				{
					row.adopt(new Element('td',
					{
						'html': elements[i]
					}))
				}
			}
			this.tableBody.adopt(row);
			this.refresh();
			return row;
		}
		return false;
	},

	/*
	 * Rerenders the table
	 */
	refresh: function ()
	{
		return;
	},
	
	/*
	 * Clears the table's contents
	 */
	deleteRows: function ()
	{
		this.tableBody.empty();
	},
	
	/*
	 * Returns the tables data as an Array
	 *
	 * @return Array
	 */
	getData: function ()
	{
		var result = new Array();
		this.tableBody.getElements('tr').each(function (row, index)
		{
			var data = new Array();
			row.getElements('td').each(function (cell, index)
			{
				data.push(cell.get('html'));
			});
			result.push(data);
		});
		return result;
	},
	
	/*
	 * Reloads the contents of the table with the contents of an Array
	 *
	 * @param Array data
	 */
	loadData: function (data)
	{
		this.clear();
		if ($type(data) == 'array')
		{
			data.each(function (rowData)
			{
				alert($type(rowData));
			});
		}
	}
});

Table.instances = {};
