/**
 * 分页脚本
 * 洪晓聪 xcjavagzs@163.com
 *
 * 该脚本依赖于prototype1.4以上版本，必须先引入
 * 
 * 调用方法：
 * var page = new wcmPage(总页数, 当前页数, 页面名称, 分页参数名,其他参数,插入页码位置);
 * page.showPageHtml();
 */

function btnPageGoOnclick(pageCount,pageName,pageParam,otherParam){
	var strPage=$F("tbInputPage").replace(/(^\s*)|(\s*$)/g, "");

	if(isNaN(strPage) || strPage==''){
		alert('请输入页码！');
		return false;
	}
	else{
		var ip = parseInt(strPage);	
		if(ip > pageCount || ip < 1){
			alert('请输入1-'+pageCount+'范围内的页码！');
			return false;
		}
		else{
			var strURL='';
			strURL = pageName + "?" + pageParam + "=" + ip + getPagerParam(otherParam);

			document.location.href=strURL;
		}
	}
}

function tbInputPageOnkeydown(pageCount,pageName,pageParam,otherParam){
	if (event.keyCode == 13)
    {
		btnPageGoOnclick(pageCount,pageName,pageParam,otherParam)
	}
}

function getPagerParam(param){
	if(param=='')
		return '';
	if(param.indexOf('&')!=0)
		return '&' + param;
	return param;
}

var wcmPage = Class.create();
wcmPage.prototype = {

	initialize: function(pageCount,currIndex,pageName,pageParam,otherParam,pagePos) {

		this._pageCount = pageCount;
		this._currIndex = currIndex;
		this._pageName = pageName;
		this._pageParam = pageParam;
		this._otherParam = otherParam;
		this._pagePos = pagePos;
		this._firstText = "首页";
		this._lastText = "末页";
		this._beforeText = "上一页";
		this._afterText = "下一页";
		this._separator = "&nbsp;";
	},
	
	isShowPage:function(){
		if(this._pageCount == null || this._pageCount<=1)
			return false;
		else
			return true;
	},
	
	getLinkHtml:function(pageIndex,text){
		return "<h3><a href=\"" + this._pageName + "?" + this._pageParam + "=" + pageIndex + getPagerParam(this._otherParam) + " \">" + text + "</a></h3>" + this._separator;
	},
	
	getFirst:function(){
		if(this._currIndex==1)
			return "<h3>" + this._firstText + "</h3>" + this._separator;
		else
			return this.getLinkHtml(1,this._firstText);
	},
	
	getLast:function(){
		if(this._currIndex==parseInt(this._pageCount))
			return "<h3>" + this._lastText + "</h3>" + this._separator;
		else
			return this.getLinkHtml(parseInt(this._pageCount),this._lastText);
	},
	
	getBefore:function(){
		if(this._currIndex>1)
			return this.getLinkHtml(parseInt(this._currIndex)-1,this._beforeText);
		else
			return "<h3>" + this._beforeText + "</h3>" + this._separator;
	},
	
	getAfter:function(){
		if(this._currIndex < parseInt(this._pageCount))
			return this.getLinkHtml(parseInt(this._currIndex)+1,this._afterText);
		else
			return "<h3>" + this._afterText + "</h3>" + this._separator;
	},
	
	getPageTextBox:function(){
		var str = "";
		str += "<h6>当前显示第"+parseInt(this._currIndex)+"/" + this._pageCount + "页&nbsp;转到</h6>";
		str += "<h4><input name=\"tbInputPage\" id=\"tbInputPage\" type=\"text\" class=\"news_srk\" value=\"\" onkeydown=\"tbInputPageOnkeydown('"+this._pageCount+"','"+this._pageName+"','"+this._pageParam+"','" + this._otherParam + "')\" /></h4><h6>页&nbsp;</h6>";
		str += "<h2><a href=\"#\" onclick=\"btnPageGoOnclick('"+this._pageCount+"','"+this._pageName+"','"+this._pageParam+"','" + this._otherParam + "');return false;\">go</a></h2>";
		return str;
	},
	
	showPageHtml:function(){
		if(!this.isShowPage())
			return;
		var str = "&nbsp;";
		str += this.getFirst();
		str += this.getBefore();
		str += this.getAfter();
		str += this.getLast();
		str += this.getPageTextBox();

		$(this._pagePos).innerHTML = str;
		
		//document.write(str);
		
	}
}