/*
* Extends jQuery DataTables plugin
* @author Son Nguyen
* @since 2009-11-05 20:00:32
*/

/* 
* Additional functionality, from the source
* @link http://www.datatables.net/plug-ins/api
*/
$.fn.dataTableExt.oApi.fnReloadAjax = function ( oSettings, sNewSource, fnCallback )
{
	if ( typeof sNewSource != 'undefined' )
	{
		oSettings.sAjaxSource = sNewSource;
	}
	this.oApi._fnProcessingDisplay( oSettings, true );
	var that = this;
	
	$.getJSON( oSettings.sAjaxSource, null, function(json) {
		/* Clear the old information from the table */
		that.oApi._fnClearTable( oSettings );
		
		/* Got the data - add it to the table */
		for ( var i=0 ; i<json.aaData.length ; i++ )
		{
			that.oApi._fnAddData( oSettings, json.aaData[i] );
		}
		
		oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
		that.fnDraw( that );
		that.oApi._fnProcessingDisplay( oSettings, false );
		
		/* Callback user function - for event handlers etc */
		if ( typeof fnCallback == 'function' )
		{
			fnCallback( oSettings );
		}
	} );
}

// defaults settings for DataTable plugin
// if could change the default DOM directly, would be nice, for now, simple string assignment

var gDTJQ_Layout = '<"fg-toolbar ui-widget-header ui-corner-tl ui-corner-tr ui-helper-clearfix"rf>t<"fg-toolbar ui-widget-header ui-corner-bl ui-corner-br ui-helper-clearfix"ilp>';

var gDTJQ_PerPage = '_MENU_ per page';

jQuery.fn.dataTableExt.oSort['formatted-num-desc'] = function(x,y){
 x = x.replace(/[^\d\-\.\/]/g,'');
 y = y.replace(/[^\d\-\.\/]/g,'');
 if(x.indexOf('/')>=0)x = eval(x);
 if(y.indexOf('/')>=0)y = eval(y);
 return x/1 - y/1;
}
jQuery.fn.dataTableExt.oSort['formatted-num-asc'] = function(x,y){
 x = x.replace(/[^\d\-\.\/]/g,'');
 y = y.replace(/[^\d\-\.\/]/g,'');
 if(x.indexOf('/')>=0)x = eval(x);
 if(y.indexOf('/')>=0)y = eval(y);
 return y/1 - x/1;
}

/*
* Global variable to store the instance of DataTable object
*/
gDTJQ_Objs = {};

/*
* Filter with a link inside a table
*/
function applyFilter(elm,pStr) {
	if (/[A-Za-z0-9]+/.test(pStr)) { // validate first
		//alert($(elm).parents('table').attr('id'));
		gDTJQ_Objs[$(elm).parents('table').attr('id')].fnFilter('"'+pStr+'"',null,false); // exact match
	} // fi
}