/* SpryDataPager.js - Revision: Spry Preview Release 1.4 */

// Copyright (c) 2006. Adobe Systems Incorporated.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   * Redistributions of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//   * Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//   * Neither the name of Adobe Systems Incorporated nor the names of its
//     contributors may be used to endorse or promote products derived from this
//     software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

var Spry; if (!Spry) Spry = {}; if (!Spry.Data) Spry.Data = {};

Spry.Data.Pager = function(ds, options)
{
	this.ds = ds;
	
	this.pageSize = 10;
	this.pagingMax = 7;
	this.pageOffset = 0;
	this.forceFullPages = false;
	this.useZeroBasedIndexes = false;
	this.divLoc = '';
	this.daPager = '';
	
	Spry.Utils.setOptions(this, options);
	
	
	this.adjustmentValue = 1;
	if (!this.useZeroBasedIndexes)
		this.adjustmentValue = 0;

	this.pageStop = this.pageOffset + this.pageSize;

	if (this.pageSize > 0)
		ds.filter(this.getFilterFunc());
}

Spry.Data.Pager.prototype.getFilterFunc = function()
{
	var self = this;
	return function(ds, row, rowNumber) {
		if (rowNumber < self.pageOffset || rowNumber >= self.pageStop)
			return null;
		return row;
	};
};

Spry.Data.Pager.prototype.filterDataSet = function(offset)
{
	if (this.pageSize < 1)
		return;

	var numRows = this.ds.getUnfilteredData().length;
	
	if (this.forceFullPages && offset > (numRows - pageSize))
		offset = numRows - pageSize;

	if (offset < 0)
		offset = 0;

	this.pageOffset = offset;
	this.pageStop = offset + this.pageSize;

	var self = this;

	// Re-apply our non-destructive filter on the ds:
	this.ds.filter(this.getFilterFunc());
};

Spry.Data.Pager.prototype.getNumPages = function()
{
	return parseInt((this.ds.getUnfilteredData().length + this.pageSize - 1) / this.pageSize);
};

Spry.Data.Pager.prototype.getCurrentPage = function()
{
	return parseInt((this.pageOffset + this.pageSize) / this.pageSize) - this.adjustmentValue;
};

Spry.Data.Pager.prototype.goToPage = function(pageNum)
{
	try{ // for NBC. photo gallery
		if ((BrowserDetect.browser =="Explorer")&&(BrowserDetect.version <="6")){
			oInterval="";
			fnStartInterval();
		}
	}catch(e){/*ignore*/}
	pageNum = parseInt(pageNum);

	var numPages = this.getNumPages();
	if ((pageNum + this.adjustmentValue) < 1 || (pageNum + this.adjustmentValue) > numPages)
		return;
	var newOffset = (pageNum - 1 + this.adjustmentValue) * this.pageSize;
	this.filterDataSet(newOffset);	
	
};

Spry.Data.Pager.prototype.goToPageContainingRowNumber = function(rowNumber)
{
	this.goToPage(this.getPageForRowNumber(rowNumber));
};

Spry.Data.Pager.prototype.firstPage = function()
{
	this.goToPage(1 - this.adjustmentValue);
};

Spry.Data.Pager.prototype.lastPage = function()
{
	this.goToPage(this.getNumPages() - this.adjustmentValue);
};

Spry.Data.Pager.prototype.previousPage = function()
{
	this.goToPage(this.getCurrentPage() - 1);
	
};

Spry.Data.Pager.prototype.nextPage = function()
{
	this.goToPage(this.getCurrentPage() + 1);
	
};

Spry.Data.Pager.prototype.getPageForRowNumber = function(rowNumber)
{
	return parseInt(rowNumber / this.pageSize) + 1 - this.adjustmentValue;
};

Spry.Data.Pager.prototype.setPageSize = function(pageSize)
{
	if (this.pageSize == pageSize)
		return;

	if (pageSize < 1)
	{
		// The caller is trying to shut off paging. Remove the filter
		// we installed on the data set.

		this.ds.filter(null);
		this.pageSize = 0;
	}
	else if (this.pageSize < 1)
	{
		this.pageSize = pageSize;
		this.pageOffset = 0;
		this.pageStop = this.pageOffset + this.pageSize;
		this.ds.filter(this.getFilterFunc());
	}
	else
	{
		// The caller is adjusting the pageSize, so go to the page
		// that contains the current pageOffset.

		this.pageSize = pageSize;
		this.goToPage(this.getPageForRowNumber(this.pageOffset));
	}
};

// go to the next page //
Spry.Data.Pager.prototype.showPages = function()
{
	var pages = '';
	var numPages = this.getNumPages();
	var pageNum = this.getCurrentPage();
	var newOffset = pageNum- 1;
	
	if (newOffset > 3){
		pageSec = newOffset - 3;
		pageMax = newOffset + 4;
	}else{
		pageSec = 0;
		pageMax = this.pagingMax;
	}
	if (pageMax > numPages ){
		pageMax = numPages;
		pageSec = numPages - this.pagingMax;
	}
	if (pageSec < 0 ){
		pageSec = 0;
	}
	for(i=pageSec; i<pageMax; i++){
// IE image load override
		if ((BrowserDetect.browser =="Explorer")&&(BrowserDetect.version <="6")){
			if(newOffset == i){	
				pages += '<a href="javascript:void(0)" class="selected" onclick="'+this.daPager+'.goToPage('+(i+1)+')">'+(i+1)+'<\/a>';
			}else{
				pages += '<a href="javascript:void(0)" onclick="'+this.daPager+'.goToPage('+(i+1)+')">'+(i+1)+'<\/a>';
			}
		}
		else {
			if(newOffset == i){	
				pages += '<a href="javascript:void(0)" class="selected" onclick="'+this.daPager+'.goToPage('+(i+1)+')">'+(i+1)+'<\/a>';
			}else{
				pages += '<a href="javascript:void(0)" onclick="'+this.daPager+'.goToPage('+(i+1)+')">'+(i+1)+'<\/a>';
			}
		}
	}	
		
	document.getElementById(this.divLoc).innerHTML = pages;
};
