/**
 * ByngTable
 * 
 * Providers methods to interact and handles extensions loaded 
 * 
 * @requires Mootools-1.11
 * @requires BaseCode-0.2.2.0
 *
 * @author Ollie Maitland
 * @copyright Byng Systems
 * 
 */

Byng.register('byng.table', 
{
	options : {
		'identifiers' : {'row' : null, 'col' : null}
	},
	
	/**
	 * Initialise a table
	 * 
	 * @param DomElement domElement
	 * @param Object options
	 */
	initialize : function (domElement, options)
	{
		options = options || {};
		
		this.table = domElement;
		if (this.isSortable()) {
			// make table sortable			
			this.setSortable( options.sortable );
		}
		
	},
	
	/**
	 * Add column to a table
	 * 
	 * @param DomElement th
	 * @param Integer targetPosition
	 */
	addColumn : function (th, targetPosition)
	{
		// insert the table header element
		if (targetPosition) { 
			var parentTh = this.table.getElements('thead tr th');
			th.injectAfter(parentTh[targetPosition-1]);
		} else {
			th.injectInside($E('thead tr', this.table));
		}
		
		// condition: there is a footer row
		if (this.table.getElement('tfoot tr')) {
			if (targetPosition) { 
				// clone the last element of the footer row
				var parentTd = this.table.getElements('tfoot tr td');
				var td = parentTd[targetPosition-1].clone();
					td.injectAfter(parentTd[targetPosition-1]);
			} else {
				// clone the last element of the footer row
				var td = this.table.getElements('tfoot tr td').getLast().clone();
				td.injectInside($E('tfoot tr', this.table));
			}
			
		}
		
		// condition: table is sortable
		if (this.isSortable()) {
			// attach the sortable behaviour to this header row
			this.getSorter().attachBehaviour(th, th.cellIndex);
		}
	},
	
	/**
	 * Drop a column from a table
	 * 
	 * @param Integer cellIndex Cell index of the column to drop
	 */
	dropColumn : function (cellIndex)
	{
		
		this.table.getElements('thead th').filter(function(th){
			return (th.cellIndex == cellIndex)
		}).each(function(el){el.remove()});
		
		this.table.getElements('tbody td').filter(function(td){
			return (td.cellIndex == cellIndex)
		}).each(function(el){el.remove()});
		
		this.table.getElements('tfoot td').filter(function(td){
			return (td.cellIndex == cellIndex)
		}).each(function(el){el.remove()});
		
		if (this.isSortable()) {
			this.refreshSortables();
		}
	},
	
	/**
	 * Return the table object
	 * 
	 * @return DomElement
	 */
	getTable : function ()
	{
		return this.table;
	},
	
	/**
	 * Get a single element
	 * 
	 * @param Mixed args
	 */
	getElement : function (args)
	{
		return this.getTable().getElement(args || "tbody");
	},
	
	/**
	 * Get a collection of elements
	 * 
	 * @param Mixed args
	 */
	getElements : function (args)
	{
		return this.getTable().getElements(args || "tbody tr");
	},	
	
	/**
	 * Is this table sortable
	 * 
	 * @return Boolean
	 */
	isSortable : function()
	{
		return (this.setSortable ? true : false);
	},
	
	/**
	 * Is the table exportable
	 * 
	 * @return Boolean
	 */
	isExportable : function ()
	{
		return (this.toFile ? true : false);
	},
	
	/**
	 * Is the table hydratable with table data
	 * 
	 * @return Boolean
	 */
	isHydratable : function ()
	{
		return (this.hydrate ? true : false);
	}
});

var ByngTable = Byng.source('byng.table');
