﻿/**
 * gjh
 * Js分页
 */

pager = function()
{};

pager.prototype =
{
	//记录总数 
	recordCount : 0,
	
	//每页大小
	pageSize : 15,
	
	//总页数
	pageCount : 0,
	
	//当前页码
	pageIndex : 1,
	
	//是否显示总记录数,如[总记录数：100]
	showRecordCount : true,
	
	//是否显示总页数,如[总页数：10]
	showPageCount : true,
	
	//是否显示当前页，如[当前页：1/10,2/10]
	showCurrentPage : true,
	
	//是否显示当前页记录编号，如[记录：1-15/16-30条]
	showCurrentPageRecord : true,
	
	//是否显示用户自己输入页码
	showTextJump : true,
	
	//是否显示首页和尾页
	showFirstAndLastPage : true,
	
	//是否显示上一页和下一页
	showPrevAndNextPage : true,
	
	//是否显示数据索引
	showNumericPage	 : true,
	
	//首页，尾页，上一页，下一页连接的类型 text:文字 image:图片
	firstLastPrevNextType : 'text',
	
	//首页文本
	firstPageText : '首页',	  
	//下一页文本
	nextPageText : '下一页',
	//上一页文本
	prevPageText : '上一页',
	//尾页文本
	lastPageText : '尾页',
	//按钮的值
	buttonText : 'go',
	//首页图片
	firstPageImage : 'FirstPage.gif',
	//下一页图片
	nextPageImage : 'NextPage.gif',
	//上一页图片
	prevPageImage : 'PrevPage.gif',
	//尾页图片
	lastPageImage : 'LastPage.gif',
	//按钮图片
	buttonImage : 'go.gif',
	//图片路径
	imagePath : 'images/',
	//放置分页字符串的容器ID
	pagerContainerId : 'pager',
	//自定义信息
	customInfo : '',
	//函数名称
	functionName : 'paging',
	//文本框样式
	inputTextClass : 'r_pagertext',
	//文本框ID
	inputTextId : 'txtInputPageNo',

	
	//获取分页总数
	getPageCount : function()
	{
		var count = 0;
		if( typeof this.recordCount != "number"
			|| this.recordCount <= 0)
		{
			return 0;			
		}
		
		if( typeof this.pageSize != "number"
			|| this.pageSize == 0)
		{
			return 0;
		}
		
		count = parseInt(this.recordCount / this.pageSize);
		var mod = this.recordCount % this.pageSize;
		if(mod != 0)
		{
			count += 1;
		}
		return count;
		
	},
	//验证PageIndex
	checkPageIndex : function()
	{
		if(typeof this.pageIndex != "number")
		{
			this.pageIndex = 1;
		}
		if(this.pageIndex <= 0)
		{
			this.pageIndex = 1;
		}
		if(this.pageIndex > this.pageCount)
		{
			this.pageIndex = this.pageCount;
		}
	},
	
	//获取总记录数
	createRecordCount : function()
	{
		var str = '';
		if (this.showRecordCount) 
		{
			str = '[总记录数:' + this.recordCount + ']';
		}
		return str;
	},
	
	//获取总页数
	createPageCount : function()
	{
		var str = '';
		if(this.showPageCount)
		{
			str = '[总页数:' + this.pageCount + ']';
		}
		return str;
	},
	//获取当前页
	createCurrentPage : function()
	{
		var str = '';
		if (this.showCurrentPage)
		{
			str = '[当前页:' +
			this.pageIndex +
			'/' +
			this.pageCount + ']';
		}
		return str;	
	},
	//获取当前页的记录编号
	createCurrentPageRecord : function()
	{
		var str = '';
		if (this.showCurrentPageRecord) 
		{
			var maxRecordNo = this.pageIndex * this.pageSize;
			var minRecordNo = (this.pageIndex - 1) * this.pageSize + 1;
			if (minRecordNo <= 0) {
				minRecordNo = 1;
			}
			if (minRecordNo > this.recordCount) {
				minRecordNo = this.recordCount;
			}
			
			if (maxRecordNo <= 0) {
				maxRecordNo = 1;
			}
			if (maxRecordNo > this.recordCount) {
				maxRecordNo = this.recordCount;
			}
			
			str = '[当前记录:' +
			minRecordNo +
			'-' +
			maxRecordNo +
			']';			
		}
		return str;	
	},
	//获取文本框内输入的页码
	getInputPageIndex : function()
	{
		var o = document.getElementById(this.inputTextId);
		if(o)
		{
			if(parseInt(o.value))
			{
				return parseInt(o.value);
			}
			else
			{
				return 0;
			}
		}
		else
		{
			return 0;
		}
	},
	//获取文本输入页码跳转
	createTextJump : function()
	{
		var str = '';
		if (this.showTextJump) {
			str = '<input id="' 
				+ this.inputTextId 
				+ '"' +
				' type="text"' +
				' size="2"' +
				' value="' +
			this.pageIndex + '"' +
			' class="' + this.inputTextClass + '"' +
			'>' ;
			
			if (this.firstLastPrevNextType.toLowerCase() == "image") 
			{
				str += '<a id="aPaging" href="javascript:'
					+ this.functionName 
					+ '();">'
					+ '<img src="' 
					+ this.imagePath 
					+ this.buttonImage 
					+ '" border="0">'
					+ '</a>';
			}
			else 
			{
				str += '<input id="btnPaging"' +
				' type="button"' +
				' onclick="'
				+ this.functionName 
				+ '();"'
				+ ' value="' +
				this.buttonText +
				'">';
			}
		}
		return str;

	},
	

	//获取首页
	createFirstPage : function()
	{
		var str = '';
		if(this.showFirstAndLastPage 
		    && this.pageIndex > 1)
		{
		    if(this.firstLastPrevNextType.toLowerCase() == 'image')
		    {
		        str = '&nbsp;<a href="javascript:' 
					+ this.functionName + '(1);">'
		            + '<img  border="0" src="' 
		            + this.imagePath 
		            + this.firstPageImage 
		            + '"'
		            + '>'
		            + '</a>&nbsp;';
		    }
		    else
		    {
		        str = '<a href="javascript:'
		            + this.functionName
		            + '(1);">'
		            + '[' + this.firstPageText + ']'
		            + '</a>';
		    }
		}
		
		return str;
	},
	
	//获取上一页
	createPrevPage : function()
	{
	    var str = '';
	    if(this.showPrevAndNextPage && this.pageIndex > 1)
	    {
	        var prevPage = this.pageIndex - 1;
	        if(this.firstLastPrevNextType.toLowerCase() == 'image')
	        {
	            str = '<a href="javascript:' 
					+ this.functionName 
					+ '(' + prevPage + ');">'
		            + '<img  border="0" src="' 
		            + this.imagePath 
		            + this.prevPageImage 
		            + '"'
		            + '>'
		            + '</a>&nbsp;';
	        }
	        else
	        {
	            str = '<a href="javascript:'
		            + this.functionName
		            + '(' + prevPage + ');">'
		            + '[' + this.prevPageText + ']'
		            + '</a>';
	        }
	    }
	    
	    return str;
	},
	
	//获取下一页
	createNextPage : function()
	{
	    var str = '';
	    if(this.showPrevAndNextPage 
			&& this.pageIndex < this.pageCount)
	    {
	        var nextPage = this.pageIndex + 1;
	        if(this.firstLastPrevNextType.toLowerCase() == 'image')
	        {
	            str = '&nbsp;<a href="javascript:' 
					+ this.functionName 
					+ '(' + nextPage + ');">'
		            + '<img  border="0" src="' 
		            + this.imagePath 
		            + this.nextPageImage 
		            + '"'
		            + '>'
		            + '</a>&nbsp;';
	        }
	        else
	        {
	            str = '<a href="javascript:'
		            + this.functionName
		            + '(' + nextPage + ');">'
		            + '[' + this.nextPageText + ']'
		            + '</a>';
	        }
	    }
	    
	    return str;
	},
	
	//获取尾页
	createLastPage : function()
	{
	    var str = '';
	    if(this.showFirstAndLastPage 
			&& this.pageIndex < this.pageCount)
	    {
	        var lastPage = this.pageCount;
	        if(this.firstLastPrevNextType.toLowerCase() == 'image')
	        {
	            str = '<a href="javascript:' 
					+ this.functionName 
					+ '(' + lastPage + ');">'
		            + '<img  border="0" src="' 
		            + this.imagePath 
		            + this.lastPageImage 
		            + '"'
		            + '>'
		            + '</a>&nbsp;';
	        }
	        else
	        {
	            str = '<a href="javascript:'
		            + this.functionName
		            + '(' + lastPage + ');">'
		            + '[' + this.lastPageText + ']'
		            + '</a>';
	        }
	    }
	    
	    return str;
	},
	
	//获取数字页码
	createNumericPage : function()
	{
		var str = '';
		if(this.showNumericPage)
		{
			var startPage 	= 0;
			var endPage 	= 0;
			startPage = this.pageIndex - 4;
			if(startPage < 1)
			{
				startPage  = 1;
			}
			if(startPage > this.pageCount - 9)
			{
				startPage = this.pageCount - 9;
				if(startPage < 1)
				{
					startPage = 1;
				}
			}
			
			endPage = this.pageIndex + 5;
			if(endPage < 10)
			{
				endPage = 10;
			}
			if(endPage > this.pageCount)
			{
				endPage = this.pageCount;
			}
			
			for(var i = startPage; i<=endPage ; i++)
			{
				var sStyle = '';
				if(i == this.pageIndex)
				{
					sStyle = ' style="color:red";';
				}
				str += '<a href="javascript:'
		            + this.functionName
		            + '(' + i + ');"'
					+ sStyle + '>'
		            + '[' + i + ']'
		            + '</a>';
			}
			
			return str;				
		}	
	},
	
	//获取分页字符串
	createPagingHtml : function()
	{
		var pagingHtml = '';
		this.pageCount = this.getPageCount();
		this.checkPageIndex();
		if(this.pageCount > 0)
		{
			var leftHtml = '';
			var rightHtml = '';
			leftHtml = '<span id="spanPagingLeft" style="float:left;">'
				+ this.customInfo
				+ this.createRecordCount()
				+ this.createPageCount()
				+ this.createCurrentPage()
				+ this.createCurrentPageRecord()
				+ '</span>';
				
			rightHtml = '<span id="spanPagingRight" style="float:right;">'
				+ this.createFirstPage()
				+ this.createPrevPage()
				+ this.createNumericPage()
				+ this.createNextPage()
				+ this.createLastPage()
				+ this.createTextJump()
				+ '</span>';
			
			pagingHtml = leftHtml + rightHtml;			
		}
		
		return pagingHtml;
	},
	
	//设置分页字符串
	setPagingHtml : function()
	{
		var oPagingContainer = document.getElementById(this.pagerContainerId);
		if(oPagingContainer)
		{
			oPagingContainer.innerHTML = this.createPagingHtml();
		}
	}
};


