var NetManagerPluginTable = NetManagerBase.extend({
	
	options: {},
	_table: null,
	_tableSort: null,
	_tableRows: [],
	_tableColData: [],
	
	initialize: function(options) {

		// Implement given options
    	this.setOptions(options);
		if (this.options.tableId !== '') {
			if ($defined($(this.options.tableId))) {
				
				if (this.options.popUp) {
					this._table = $E('.NetManagerWizardContent').getElement('#'+this.options.tableId);
				} else {
					this._table = $(this.options.tableId);
				}
				if ($defined(this._table)) {
					this._tableSort = new sortableTable(this._table, {overCls: 'hover', onStart: false, sortOn: 2});
				
					if ($defined($(this._table).getElement('tbody'))) {
						this._tableRows = $(this._table).getElement('tbody').getElements('tr');
						
						if (this._tableRows.length > 0) {
							var rowCounter = 0, colCounter = 0;
							this._tableRows.each(function(row) {
								
								$(row).getElements('td').each(function(tdValue){
									if (!$defined(this._tableColData[colCounter])) {
										this._tableColData[colCounter] = [];
									} else {
										this._tableColData[colCounter].push($(tdValue).getText());	
									}
									colCounter = colCounter + 1;	
								}, this);
								
								rowCounter = rowCounter + 1;
								colCounter = 0;
							}, this);
							
							colCounter = 0;
							var nmUtilArray = new NetManagerUtilArray();
							this._tableColData.each(function(column, index) {
								this._tableColData[colCounter] = nmUtilArray.unique(column);
								colCounter = colCounter + 1;
							}, this);
							
							colCounter = 0;
							this.fancyFilters = [];
							this._table.getElement('#filterRow').getElements('th').each(function(filterHeading, index){
								
								var currentDropdown = $(filterHeading).getElement('select');
								if ($type($(currentDropdown)) == 'element') {
									$(filterHeading).getElement('select').empty();
									var newOption = new Element('Option').setProperty('value','all').setText('All').inject($(currentDropdown));
									this._tableColData[colCounter].each(function(colValue){
										var newOption = new Element('Option').setProperty('value',colValue).setText(colValue).inject($(currentDropdown));
									});
									var options = {};
									options.container = $(filterHeading);
									
									if (this.options.popUp) {
										options.key = $(filterHeading).getElement('select').getProperty('id') + '_popUp';
									} else {
										options.key = $(filterHeading).getElement('select').getProperty('id');
									}
									
									options.width = $(filterHeading).getStyle('width').replace(/[^0-9]/ig,'');
									options.selectId = $(filterHeading).getElement('select').getProperty('id');
									options.fnEvent = this.filterTable.pass([colCounter, currentDropdown.id], this);
									switch(index) {    
										case 1:
											options.containerWidth = 410;
											options.icon = 'iconVoyage';
										break;
										case 2:
											options.containerWidth = 100;
											options.icon = 'iconDate';
										break;
										case 3:
											options.containerWidth = 100;
											options.icon = 'iconDate';
										break;
										case 4:
											options.containerWidth = 130;
											options.icon = 'iconDuration';
										break;
										case 5:
											options.containerWidth = 170;
											options.icon = 'iconVessel';
										break;
									}
									this.fancyFilters[options.key] = this.getDropdown(options);
									
								} else {
									var inputElm = $(filterHeading).getElement('input');
									if ($type(inputElm) == 'element') {
										$(inputElm).addEvent('keyup', this.filterTable.pass([colCounter, inputElm], this));	
									}
								}
								colCounter = colCounter + 1;
							}, this);
						}
					}
				}
			}
		}	
	}, 
	
	filterTable: function(col, elm) {
		var key = $(elm).getProperty('value').toLowerCase();
		if (key == '') {
			key = 'all';
		}
		this._tableSort.clearFilter.pass([col]).bind(this._tableSort);
		
		this._tableSort.filter(col, key);
		if (!$defined($(elm).getParent().getElement('a'))) {
			var clearLink = new Element('a').addClass('clearLink').setProperties({'href':'javascript:void(0);', 'title':'Click here to clear the filter'}).addEvent('click', this._tableSort.clearFilter.bind(this._tableSort)).setText('Clear').inject($(elm).getParent());
		}
	}
	
});

NetManagerPluginTable.implement(new Options);