var mpnTable = {};

mpnTable.create = function(rows, tableSelector, tableConfig, onRowSelected, onRowDeselected, afterTableDraw) {
	var table;	
	
	if($.fn.dataTable.isDataTable(tableSelector)) {
		//Clear all rows from existing table.
		table = $(tableSelector).DataTable();
		table.rows()
			.remove()
			.draw();
	}
	else {	
		//Create new table.
		table = $(tableSelector).DataTable(tableConfig);
		
		//Add event handler to highlight selected row on click and fire callback.
		$(tableSelector + " tbody").on('click keypress', 'tr', function(event) {
			var SPACEBAR = 0,
				ENTER = 13;
			
			if (event.type === 'click'
				|| (event.type === 'keypress'
					&& (event.keyCode === ENTER || event.keyCode === SPACEBAR))) {
				if ( $(this).hasClass('selected') ) {
		            $(this).removeClass('selected');
		            
		            //Execute client callback.
		            onRowDeselected(table);
		        }
		        else {
		            table.$('tr.selected').removeClass('selected');
		            $(this).addClass('selected');
		            
		            //Execute client callback.
		            onRowSelected(table);
		        }
			}
		});
	}
	
	//Add new rows.
	table.rows
		.add(rows)
		.draw();
	
	//Execute client callback.
	afterTableDraw();
};
