/*
JobLoft.com Paging Control
(c) 2005-06 JobLoft.com
Author: Lee Liu
Don't steal me!
*/


// paging class
function paging()
{
	this.startindex = null;
	this.rowsaffected = null;
	this.pagesize = null;
	this.totalrecords = null;
	this.endindex = null;
	this.currentpage = null;
	this.totalpages = null;
	
}

paging.prototype.setstartindex = function( startindex )
{
	this.startindex = parseInt( startindex );
	this.calcpaging();
}

paging.prototype.setrowsaffected = function( rowsaffected )
{
	this.rowsaffected = parseInt( rowsaffected );
	this.calcpaging();
}

paging.prototype.setpagesize = function( pagesize )
{
	this.pagesize = parseInt( pagesize );
	this.calcpaging();
}

paging.prototype.settotalrecords = function( totalrecords )
{
	this.totalrecords = parseInt( totalrecords );
	this.calcpaging();
}

paging.prototype.calcpaging = function()
{
	if ( this.startindex != null && this.pagesize != null )
		this.currentpage = Math.ceil( this.startindex / this.pagesize );
		
	if ( this.startindex != null && this.rowsaffected != null )
		this.endindex = this.startindex + this.rowsaffected - 1;
	
	if ( this.pagesize == null && this.rowsaffected != null )
		this.pagesize = this.rowsaffected;
	
	if ( this.totalrecords != null && this.pagesize != null )
		this.totalpages = Math.ceil( this.totalrecords / this.pagesize );
		
	// init pages
	if ( this.currentpage != null && this.totalpages != null )
	{
		// update page number
		UpdatePageNumber( this.currentpage );
	
		if ( navstyle == navstyles.simple )
		{
			RenderLinksOther();
			
			if ( this.currentpage == 1 )
				RenderLinksFirstPage();
			
			if ( this.currentpage >= this.totalpages )
				RenderLinksLastPage();
				
		}
	}
		
}

paging.prototype.changepage = function( pagevalue )
{
	switch ( pagevalue )
	{
		case "FirstPage":
			if ( this.currentpage == 1 ) return false;
			this.currentpage = 1;
			this.startindex = 1;
			this.endindex = this.startindex + this.rowsaffected - 1;
			break;
		
		case "PrevPage":
			if ( this.currentpage == 1 ) return false;
			this.currentpage--;
			this.startindex -= this.pagesize;
			this.endindex = this.startindex + this.rowsaffected - 1;
			break;
		
		case "NextPage":
			if ( this.currentpage == this.totalpages ) return false;
			this.currentpage++;
			this.startindex += this.pagesize;
			this.endindex = this.startindex + this.rowsaffected - 1;
			break;
			
		case "LastPage":
			if ( this.currentpage == this.totalpages ) return false;
			this.currentpage = this.totalpages;
			this.startindex = this.pagesize * ( this.totalpages - 1 ) + 1;
			this.endindex = this.startindex + this.rowsaffected - 1;
			break;
		
		default:
			return false;
	}
	
	// update page number
	UpdatePageNumber( xmlpaging.currentpage );
	
	return true;
}
