﻿// JScript 文件

function NewsShowPages(name) { //初始化属性
	this.name = name;      //对象名称
	this.page = 1;         //当前页数
	this.pageCount = 1;  //总记录
	this.argName = 'page'; //参数名
	this.showTimes = 1;    //打印次数
	this.space = "<span style=\"width:5px;\"></span>"; //间隔
}

NewsShowPages.prototype.createHtml = function(mode){ //生成html代码
	var strHtml = '', prevPage = this.page - 1, nextPage = this.page + 1;
	if (mode == '' || typeof(mode) == 'undefined') mode = 0;
	switch (mode) {
		
		case 1 : //模式1 (10页缩略,首页,前页,后页,尾页)
		    if(this.pageCount > 1)
		    {
			    //strHtml += '<span class="count">共'+this.pageCount+'页，第: ' + this.page + '/' + this.pageCount + '页</span>';
			    strHtml += this.space+'<span class="number">';
			    if (prevPage < 1) {
				    //strHtml += this.space+'<span title="转到第1页"><font face=\"webdings\">9</font></span>';
				    strHtml += this.space+'<span title=转到上一页>上一页</span>';
			    } else {
				    //strHtml += this.space+'<span title="转到第1页"><a href="javascript:' + this.name + '.toPage(1);"><font face=\"webdings\">9</font></a></span>';
				    strHtml += this.space+'<span title="转到上一页"><a href="javascript:' + this.name + '.toPage(' + prevPage + ');">上一页</a></span>';
			    }
			    if (this.page % 10 ==0) {
				    var startPage = this.page - 9;
			    } else {
				    var startPage = this.page - this.page % 10 + 1;
			    }
			    if (startPage > 10) strHtml += this.space+'<span title="上 10 页"><a href="javascript:' + this.name + '.toPage(' + (startPage - 1) + ');">...</a></span>';
			    for (var i = startPage; i < startPage + 10; i++) {
				    if (i > this.pageCount) break;
				    if (i == this.page) {
					    strHtml += this.space+'<span title="第 ' + i + '页"><font style=\"font-weight:Bold;color:red;\">' + i + '</font></span>';
				    } else {
					    strHtml += this.space+'<span title="第 ' + i + '页"><a href="javascript:' + this.name + '.toPage(' + i + ');">' + i + '</a></span>';
				    }
			    }
			    if (this.pageCount >= startPage + 10) strHtml += this.space+'<span title="下 10 页"><a href="javascript:' + this.name + '.toPage(' + (startPage + 10) + ');">...</a></span>';
			    if (nextPage > this.pageCount) {
				    strHtml += this.space+'<span title="转到下一页">下一页</span>';
				    //strHtml += this.space+'<span title="转到最后一页"><font face=\"webdings\">:</font></span>';
			    } else {
				    strHtml += this.space+'<span title="转到下一页"><a href="javascript:' + this.name + '.toPage(' + nextPage + ');">下一页</a></span>';
				    //strHtml += this.space+'<span title="转到最后一页"><a href="javascript:' + this.name + '.toPage(' + this.pageCount + ');"><font face=\"webdings\">:</font></a></span>';
			    }
			    strHtml += '</span><br />';
			}
			else
			    strHtml="";
			break;
		
		default :
			strHtml = 'Javascript showPage Error: not find mode ' + mode;
			break;
	}
	return strHtml;
}
NewsShowPages.prototype.createUrl = function (page) { //生成页面跳转url
	if (isNaN(parseInt(page))) page = 1;
	if (page < 1) page = 1;
	if (page > this.pageCount) page = this.pageCount;
	var url = location.protocol + '//' + location.host + location.pathname;
	var args = location.search;
	var reg = new RegExp('([\?&]?)' + this.argName + '=[^&]*[&$]?', 'gi');
	args = args.replace(reg,'$1');
	if (args == '' || args == null) {
		args += '?' + this.argName + '=' + page;
	} else if (args.substr(args.length - 1,1) == '?' || args.substr(args.length - 1,1) == '&') {
			args += this.argName + '=' + page;
	} else {
			args += '&' + this.argName + '=' + page;
	}
	alert(args);
	
	//args = url.replace(".","_"+page+".")
	return url + args;
}
NewsShowPages.prototype.toPage = function(page){ //页面跳转
    //alert(page);
	var turnTo = 1;
	if (typeof(page) == 'object') {
		turnTo = page.options[page.selectedIndex].value;
	} else {
		turnTo = page;
	}
	//self.location.href = this.createUrl(turnTo);
    //	self.location.href = self.location.href.replace(".","_"+page+".");
	var strUrl = self.location.href ;
	//alert(strUrl.lastIndexOf("/"))
	var str = strUrl.substring(strUrl.lastIndexOf("/")+1);
	if(str.indexOf("_") > -1)
	    str = str.substr(0,str.indexOf("_")+1) + page + ".aspx";
	else
	    str = str.substr(0,str.indexOf(".")) + "_" + page + ".aspx";
	
	//alert(str);
	
	self.location.href =strUrl.substring(0,strUrl.lastIndexOf("/")+1)+str;
	
	
}
NewsShowPages.prototype.printHtml = function(mode){ //显示html代码
	//this.getPage();
	//this.checkPages();
	this.showTimes += 1;
	document.write('<div id="pages_' + this.name + '_' + this.showTimes + '" class="pages"></div>');
	document.getElementById('pages_' + this.name + '_' + this.showTimes).innerHTML = this.createHtml(mode);
	
}
NewsShowPages.prototype.formatInputPage = function(e){ //限定输入页数格式
	var ie = navigator.appName=="Microsoft Internet Explorer"?true:false;
	if(!ie) var key = e.which;
	else var key = event.keyCode;
	if (key == 8 || key == 46 || (key >= 48 && key <= 57)) return true;
	return false;
}