/********************************************************************
*					pagination.js
*				--------------------
*		开始时间：2007.10.9
*		作 者：江敬城
*		Email：min43@163.com
*		编 号：pagination.js，v1.0，2007.10.10 10:58，江敬城
*
********************************************************************/

// 分页类：Pagination
function Pagination(baseUrl, pName, recCount, numPerPage, currPage, showPageNum, showJumpBox, showDropDownBox)
{
	this.baseUrl         = baseUrl;                              // 基本的链接地址
	this.pName           = pName;                                // 分页变量的名称
	this.recCount        = recCount;                             // 总记录数
	this.numPerPage      = numPerPage;                           // 每页显示的数目
	this.currPage        = currPage;                             // 当前页次
	this.showPageNum     = showPageNum;                          // 分页中一次最多显示几个页次
	this.showJumpBox     = showJumpBox;                          // 是否显示跳转输入框
	this.showDropDownBox = showDropDownBox;                      // 是否显示下拉框 
	
	this.halfPageNum     = Math.floor(this.showPageNum/2);            // 当前页左右两边显示的分页数
	this.totalPage       = Math.ceil(this.recCount/this.numPerPage);  // 分页总数
	this.prevPage        = this.currPage - 1;                         // 上一页的页次
	this.nextPage        = this.currPage + 1;                         // 下一页的页次
	
	
	this.writeHTML = function(){
		document.write(this.toHTML());
	}
	
	this.toHTML = function(){
		var htmlStr = "";
		var i;
		this.baseUrl = this.getRealBaseUrl(this.baseUrl, this.pName);
		
		htmlStr += "Total: <strong>" + this.totalPage + "</strong>";
		
		if(!this.totalPage) return htmlStr;
		
		htmlStr += "&nbsp;Page:<strong>" + this.currPage + "</strong>/" + this.totalPage + " List:" + this.numPerPage + "/page";
		htmlStr += "&nbsp;&nbsp;<a href='" + this.getUrl(1) + "'>First</a> ";
		if(this.prevPage && this.prevPage <= this.totalPage) htmlStr += "<a href='" + this.getUrl(this.prevPage) + "' class='s'>Prev</a> ";
		else htmlStr += "Prev ";
		
		
		if(this.totalPage > this.showPageNum)
		{
			if(this.currPage > (this.halfPageNum + 1))
			{
				htmlStr += "...";
				
				for(i = (this.currPage - this.halfPageNum); i < this.currPage; i++)
				{
					htmlStr += "<a href='" + this.getUrl(i) + "' >" + i + "</a> ";
				}
				
				htmlStr += "<a class='on'>[" + this.currPage + "]</a> ";
				
				var endPage = (this.currPage + this.halfPageNum) >= this.totalPage ? this.totalPage : (this.currPage + this.halfPageNum);
				
				for(i = this.currPage + 1; i <= endPage; i++)
				{
					htmlStr += "<a href='" + this.getUrl(i) + "' >" + i + "</a> ";
				}
				
				if(endPage < this.totalPage)
				{
					htmlStr += "...";
				}
			}
			else
			{
				for(i = 1; i < this.currPage; i++)
				{
					htmlStr += "<a href='" + this.getUrl(i) + "' >" + i + "</a> ";
				}
				
				htmlStr += "<a class='s'>[" + this.currPage + "]</a> ";
				
				for(i = this.currPage + 1; i <= (this.currPage + this.halfPageNum); i++)
				{
					htmlStr += "<a href='" + this.getUrl(i) + "' >" + i + "</a> ";
				}
				
				htmlStr += "...";
			}
		}
		else
		{
			for(i = 1; i <= this.totalPage; i++)
			{
				if(this.currPage == i)
				{
					htmlStr += "<a class='on'>[" + i + "]</a> ";
				}
				else
				{
					htmlStr += "<a href='" + this.getUrl(i) + "' >" + i + "</a> ";
				}
			}
		}
		
		if(this.nextPage && this.nextPage <= this.totalPage) htmlStr += "<a href='" + this.getUrl(this.nextPage) + "' class='s'>Next</a> ";
		else htmlStr += "Next ";
		htmlStr += "<a href='" + this.getUrl(this.totalPage) + "'>Last</a>";
		
		if(this.showDropDownBox)
		{
			htmlStr += "&nbsp;&nbsp;<select onChange='window.location = this.options[this.selectedIndex].value;'>";
			for(i = 1; i <= this.totalPage; i++)
			{
				if(this.currPage == i)
				{
					htmlStr += "<option value=\"" + this.getUrl(i) + "\" selected=\"selected\">Page " + i + "</option>";
				}
				else
				{
					htmlStr += "<option value=\"" + this.getUrl(i) + "\">Page " + i + "</option>";
				}
			}
			htmlStr += "</select>";
		}
		
		return htmlStr;
	}
	
	this.getUrl = function(page){
		return this.baseUrl + page;
	}
	
	this.getRealBaseUrl = function(baseUrl, pName){
		if(baseUrl.indexOf("?") == -1)
			return baseUrl + "?" + pName + "=";
		else
			return baseUrl + "&" + pName + "=";
	}
}


/*
//使用示例
var p = new Pagination("index.php?gid=12", 'p', 131, 10, 2, 9, 1, 1);
p.writeHTML();// 等价于 document.write($p->toHTML());
*/
