/*********************************************************************************/
/**			copyright (c) PlanetCalc.com, 2007  			**/
/**		    	Table renderer classes					*/
/*********************************************************************************/
function AjaxTable( initialData, url, placeholderId, loading_msg, idPropertyName ) {
	this.Recordset = new Recordset( initialData, idPropertyName );
	this.savedScroll = null;	
	this.qc = this.Recordset.QueryContext;
	this.URL = url;
	var me = this;
	this.Renderer = new RecordsetRenderer( placeholderId , this.qc.items );
	
	this.OnSort= function( id ) {
		me.qc.sortcolumn = id;
		me.qc.sortdirection=(me.qc.sortdirection=="ASC")?"DESC":"ASC";
		me.Reload( );		
	}

	this.OnNavigate= function( id ) {
		me.qc.from = Number(id)*me.qc.items;	
		me.Reload();
	}
	this.OnStartLoading = function () {
		me.savedScroll = BSGetScroll();
		me.Renderer.RenderMessage( me.qc.items, loading_msg );
	}
	
	this.ReloadEx = function ( obj, url ) {
		this.URL = url;
		for( var propId in obj ) {
			this.qc[ propId ] = obj[ propId ];
		}
		this.Reload();
	}
	this.Reload = function () {
		me.OnStartLoading();
		BSMakePOSTRequest( me.URL, me, me.qc ); 
	}

	this.ReloadFrom = function ( obj, url ) {
		var initialURL = this.URL;	
		this.ReloadEx( obj, url );
		this.URL = initialURL;
	}
	this.Render = function () {
		this.Renderer.Render( this.Recordset, this.OnSort, this.OnNavigate ); 
	}
	this.Update = function ( obj ) {
		me.Recordset.LoadFromObject( obj )
		if (null != me.savedScroll)
			window.scrollTo(me.savedScroll.x, me.savedScroll.y);
		me.Render();
	}
	this.OnResponse = function ( obj ) {
		me.Update( obj );
	}                                            
}

function AjaxTableNoEmptyRows ( initialData, url, placeholderId, loading_msg ) {
	this.inheritFrom = AjaxTable;
	this.inheritFrom(initialData, url, placeholderId, loading_msg);

	this.Renderer = new AjaxTableNoEmptyRowsRenderer(placeholderId, this.Recordset.QueryContext.items);
	this.OnStartLoading = function ( ) { }
}

function AjaxSetMessages ( initialData, url, placeholderId, loading_msg ) {
	this.inheritFrom = AjaxTable;
	this.inheritFrom(initialData, url, placeholderId, loading_msg);

	this.OnSort = null;
	this.Renderer = new CommentsRenderer(placeholderId, this.Recordset.QueryContext.items);
	this.OnStartLoading = function ( ) { }
}


function AjaxSetPersonalInfo ( initialData, url, placeholderId, loading_msg ) {
	this.inheritFrom = AjaxTable;
	this.inheritFrom(initialData, url, placeholderId, loading_msg);

	this.OnSort = null;
	this.Renderer = new PersonalInfoRenderer(placeholderId, this.Recordset.QueryContext.items);
	this.OnStartLoading = function ( ) { }
}


function AjaxTableArtefactsByExample ( initialData, url, placeholderId, loading_msg ) {
	this.inheritFrom = AjaxTable;
	this.inheritFrom(initialData, url, placeholderId, loading_msg);

	this.Renderer = new AjaxTableNoEmptyRowsRenderer(placeholderId, this.Recordset.QueryContext.items);
	this.OnStartLoading = function ( ) { }
	this.OnSort = null;
	this.OnNavigate = null;
}

function AjaxTableHandbookData ( initialData, url, placeholderId, loading_msg ) {
	this.inheritFrom = AjaxTable;
	this.inheritFrom(initialData, url, placeholderId, loading_msg);
	this.LinkedRecordset = new Recordset("");
	var me = this;
	
	this.OnResponse = function ( obj ) {
		if (obj.linked) {
			me.LinkedRecordset.LoadFromArray( obj.linked );
		} 
		me.Update( obj );
	}                                            
	this.GetRecord = function ( id ) {
		var record = this.Recordset.GetRecord( id );
		return record?record:this.LinkedRecordset.GetRecord( id );
	}
}

