/*

showPages 1.0
全js
zjc 

*/

function showPages(name,obj,sfunction) { //初始化属性
 this.obj = obj;
 this.sfunction = sfunction;
 this.name = name;      //对象名称
 this.page = 1;         //当前页数
 this.pageCount = 1;    //总页数
 this.argName = 'page'; //参数名
}

showPages.prototype.getPage = function(p){ //丛url获得当前页数,如果变量重复只获取最后一个
 this.page = p;
}
showPages.prototype.checkPages = function(){ //进行当前页数和总页数的验证
 if (isNaN(parseInt(this.page))) this.page = 1;
 if (isNaN(parseInt(this.pageCount))) this.pageCount = 1;
 if (this.page < 1) this.page = 1;
 if (this.pageCount < 1) this.pageCount = 1;
 if (this.page > this.pageCount) this.page = this.pageCount;
 this.page = parseInt(this.page);
 this.pageCount = parseInt(this.pageCount);
}
showPages.prototype.createHtml = function(){ //生成html代码
	var strHtml = '', prevPage = this.page - 1, nextPage = this.page + 1;
     strHtml += '<span class="count">页: <span id="currpage">' + this.page + '</span> / ' + this.pageCount + '</span>&nbsp;&nbsp;';
     strHtml += '<span class="number">';
     if (prevPage < 1) {
       strHtml += '<span title="首页"><img src="http://images.17173.com/vlog/images/first.gif" border="0" align="absmiddle"  title="首页"></span>&nbsp;';
       strHtml += '<span title="上一页"><img src="http://images.17173.com/vlog/images/pre.gif" border="0" align="absmiddle" title="上一页"></span>&nbsp;';
     } else {
       strHtml += '<span title="首页"><a href="javascript:void(0);" onclick="javascript:' + this.name + '.toPage(1,\''+this.obj+'\');"><img src="http://images.17173.com/vlog/images/first.gif" border="0" align="absmiddle" title="首页"></a></span>&nbsp;';
       strHtml += '<span title="上一页"><a href="javascript:void(0);" onclick="javascript:' + this.name + '.toPage(' + prevPage + ',\''+this.obj+'\');"><img src="http://images.17173.com/vlog/images/pre.gif" border="0" align="absmiddle" title="上一页"></a></span>&nbsp;';
     }
     if (this.page != 1) strHtml += '<span title="页 1"><a href="javascript:void(0);" onclick="javascript:' + this.name + '.toPage(1,\''+this.obj+'\');">[1]</a></span>';
     if (this.page >= 5) strHtml += '<span>...</span>';
     if (this.pageCount > this.page + 2) {
       var endPage = this.page + 2;
     } else {
       var endPage = this.pageCount;
     }
     for (var i = this.page - 2; i <= endPage; i++) {
       if (i > 0) {
         if (i == this.page) {
           strHtml += '&nbsp;<span title="页 ' + i + '">[' + i + ']</span>';
         } else {
           if (i != 1 && i != this.pageCount) {
             strHtml += '&nbsp;<span title="页 ' + i + '"><a href="javascript:void(0);" onclick="javascript:' + this.name + '.toPage(' + i + ',\''+this.obj+'\');">[' + i + ']</a></span>';
           }
         }
       }
     }
     if (this.page + 3 < this.pageCount) strHtml += '<span>...</span>';
     if (this.page != this.pageCount) strHtml += '&nbsp;<span title="页 ' + this.pageCount + '"><a href="javascript:void(0);" onclick="javascript:' + this.name + '.toPage(' + this.pageCount + ',\''+this.obj+'\');">[' + this.pageCount + ']</a></span>';
     if (nextPage > this.pageCount) {
       strHtml += '&nbsp;<span title="下一页"><img src="http://images.17173.com/vlog/images/next.gif" border="0" align="absmiddle"  title="下一页"></span>';
       strHtml += '&nbsp;<span title="末页"><img src="http://images.17173.com/vlog/images/last.gif" border="0" align="absmiddle"  title="末页"></span>';
     } else {
       strHtml += '&nbsp;<span title="下一页"><a href="javascript:void(0);" onclick="javascript:' + this.name + '.toPage(' + nextPage + ',\''+this.obj+'\');"><img src="http://images.17173.com/vlog/images/next.gif" border="0" align="absmiddle"  title="下一页"></a></span>';
       strHtml += '&nbsp;<span title="末页"><a href="javascript:void(0);" onclick="javascript:' + this.name + '.toPage(' + this.pageCount + ',\''+this.obj+'\');"><img src="http://images.17173.com/vlog/images/last.gif" border="0" align="absmiddle"  title="末页"></a></span>';
     }
     strHtml += '</span><br />';
  return strHtml;
}

showPages.prototype.toPage = function(page,obj){ //页面跳转
 eval(this.sfunction+'('+page+')');
 this.getPage(page);
 this.checkPages();
 document.getElementById(obj).innerHTML = this.createHtml();
}

showPages.prototype.printHtml = function(){ //显示html代码
 this.getPage(1);
 this.checkPages();
 document.getElementById(this.obj).innerHTML = this.createHtml();
}

showPages.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;
}

