/* 
/////////////////////////////////
Version history:
/////////////////////////////////
1.0   
	- inital Release
	
1.1 12.10.2007
	- changed params of onComlpeteFunction, first param is now the received html, second one is the used command object

*/

/* 
/////////////////////////////////
Todo:
/////////////////////////////////
- caching geht nicht

*/

/* 
/////////////////////////////////
Doku:
/////////////////////////////////
- todo

*/


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CLASS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var zxTablePager = zxBaseClass.extend (
{
	///////////////////////////////////////////////////////////////////////////////////////////////
	// PUBLIC
	///////////////////////////////////////////////////////////////////////////////////////////////

	initialize: function( customDefaultOptions )
	{
		this.parent();
		
		this.setDefaultCustomOptions( customDefaultOptions );
		this.setDefaultOptions( new zxTablePagerDefaultOptions() );
		
		this._ajaxRunning = false;

		this._lastPageNumber = 1;
		this._lastPageSize = 10;
		this._lastAsc = true;
		this._lastOrderBy = 'ID';
		this._lastCommandObject = null;
	},
	
	
	displayPage: function ( pageNumber )
	{
		pageNumber = pageNumber - 1;

		// no more than one ajax request allowed for this pager
		if ( this._ajaxRunning ) return;

		var ajaxFunction = this.getOption( 'ajaxFunction' );
		if ( ajaxFunction == null ) return;
					
		var buildCommandObjectFuntion = this.getOption( 'buildCommandObjectFunction' );
		if ( buildCommandObjectFuntion == null ) return;
		
		var commandObject = buildCommandObjectFuntion();
		
		// save last displayed page number
		this._lastPageNumber = pageNumber;

		this._updateCommandObject( commandObject );

		// get data from server
		this._ajaxRunning = true;
		ajaxFunction( commandObject, this._ajaxCallBack.bind(this) );
	},
	
	
	setSortDirection: function( orderBy, asc )
	{
		// no more than one ajax request allowed for this pager
		if ( this._ajaxRunning ) return;

		// start at page 1 after sort
		this._lastPageNumber = 0;

		var ajaxFunction = this.getOption( 'ajaxFunction' );
		if ( ajaxFunction == null ) return;
					
		var buildCommandObjectFuntion = this.getOption( 'buildCommandObjectFunction' );
		if ( buildCommandObjectFuntion == null ) return;
		
		var commandObject = buildCommandObjectFuntion();

		// save last sort direction
		this._lastAsc = asc;
		this._lastOrderBy = orderBy;

		this._updateCommandObject( commandObject );

		// get data from server
		this._ajaxRunning = true;
		ajaxFunction( commandObject, this._ajaxCallBack.bind(this) );
	},
	

	updatePageSize: function ()
	{
		// no more than one ajax request allowed for this pager
		if ( this._ajaxRunning ) return;

		// get display per page number
		var selectNode = $( this._displayPerPageSelectNodeId );
		if ( !selectNode ) return;

		var displayPerPage = selectNode.getValue();
		
		// start at page 1 after page size change
		this._lastPageNumber = 0;

		// get data from server
		this._ajaxRunning = true;
		this.ajaxFunction( displayPerPage, this._lastPageNumber, this._lastOrderBy, this._lastAsc, this._ajaxCallBack.bind(this) );

		// save last displayed page size
		this._lastPageSize = displayPerPage;
	},
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// PRIVATE
	///////////////////////////////////////////////////////////////////////////////////////////////
	
	_ajaxCallBack: function( html )
	{
		this._ajaxRunning = false;
		
		this._updateAjaxContent( html );

		var onCompleteFunction = this.getOption( 'onCompleteFunction' );
		if ( onCompleteFunction != null ) 
		{
			onCompleteFunction( html, this._lastCommandObject );
		}
	},
	
	_updateCommandObject: function( commandObject )
	{
		var commandObjectPageSizeEntry = this.getOption( 'commandObjectPageSizeEntry' );
		var commandObjectPageNumberEntry = this.getOption( 'commandObjectPageNumberEntry' );
		var commandObjectAscEntry = this.getOption( 'commandObjectAscEntry' );
		var commandObjectOrderByEntry = this.getOption( 'commandObjectOrderByEntry' );

		if ( commandObjectPageSizeEntry in commandObject )
		{
			commandObject[commandObjectPageSizeEntry] = this._lastPageSize;
		}

		if ( commandObjectPageNumberEntry in commandObject )
		{
			commandObject[commandObjectPageNumberEntry] = this._lastPageNumber;
		}
		
		if ( commandObjectAscEntry in commandObject )
		{
			commandObject[commandObjectAscEntry] = this._lastAsc;
		}

		if ( commandObjectOrderByEntry in commandObject )
		{
			commandObject[commandObjectOrderByEntry] = this._lastOrderBy;
		}

		this._lastCommandObject = commandObject;
	},
	
	_updateAjaxContent: function( html )
	{
		var containerNodeId = this.getOption( 'ajaxResultContainerNodeId' );
		var containerNode = $( containerNodeId );
		if ( !containerNode ) return;

		// display data in page
		containerNode.setHTML( html );
	}
});

var zxTablePagerDefaultOptions = new Class (
{
	///////////////////////////////////////////////////////////////////////////////////////////////
	// PUBLIC
	///////////////////////////////////////////////////////////////////////////////////////////////

	initialize: function()
	{
		this.ajaxFunction = null;
		this.buildCommandObjectFunction = null;
		this.onCompleteFunction = null;
		this.ajaxResultContainerNodeId = null;
		this.pageSizeSelectNode = null;

		this.pageSizeDefault = 10;
		this.sortColumn = 'ID';
		this.sortAsc = true;

		this.commandObjectPageSizeEntry = 'pageSize';
		this.commandObjectPageNumberEntry = 'pageNumber';
		this.commandObjectAscEntry = 'asc';
		this.commandObjectOrderByEntry = 'orderBy';
	}
});

