/*

showPages v1.1
=================================

 


Example
----------------------
var pg = new showPages('pg');
pg.pageCount = 12; //定义总页数(必要)
pg.argName = 'p';    //定义参数名(可选,缺省为page)
pg.printHtml();        //显示页数


Supported in Internet Explorer, Mozilla Firefox
*/
function showPages(name) { //初始化属性
 this.name = name;      //对象名称
 this.page = 1;         //当前页数
 this.pageCount = 1;    //总页数
 this.argName = 'page'; //参数名
 this.showTimes = 1;    //打印次数
 this.readurl   = "b.html";//阅读页地址
}

showPages.prototype.getPage = function(){ //丛url获得当前页数,如果变量重复只获取最后一个
 var args = location.href;
 var myreg = /(\d+)\.shtml/;
 var chk = args.match(myreg);
 this.page = RegExp.$1;
}
showPages.prototype.getPage1 = function(){ //丛url获得当前页数,如果变量重复只获取最后一个
 var args = location.search;
 var reg = new RegExp('[\?&]?' + this.argName + '=([^&]*)[ && #36;]?', 'gi');
 var chk = args.match(reg); 
 this.page = RegExp.$1;
}
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(mode){ //生成html代码
 var strHtml = '', prevPage = this.page - 1, nextPage = this.page + 1;
 if (mode == '' || typeof(mode) == 'undefined') mode = 0;
 
   if (this.page > 1) {
		var bpage = this.page - 1;
    //strHtml += '<a href="'+this.readurl+'?'+this.argName+'='+bpage+'" target=_top>前一页</a>';
    strHtml += '<a href="/books/'+bookcode+'/'+bpage+'.shtml" target=_top>前一页</a>';
   } else {
    strHtml += '<a href="/books/'+bookcode+'.shtml">前一页</a>';
   }
   strHtml  += '<a href="/books/'+bookcode+'.shtml">返回目录</a>'; 
   if (this.page < this.pageCount) {
		var npage = this.page + 1;
    //strHtml += ' <a href="'+this.readurl+'?'+this.argName+'='+npage+'" target=_top>后一页</a>';
    strHtml += ' <a href="/books/'+bookcode+'/'+npage+'.shtml" target=_top>后一页</a>';
   } else {
    strHtml += ' <a href="/books/'+bookcode+'.shtml">后一页</a>';
   }
 return strHtml;
}
showPages.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 + '=[^&]*[ && #36;]?', '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;
 }
 return url + args;
}
showPages.prototype.toPage = function(page){ //页面跳转
 var turnTo = 1;
 if (typeof(page) == 'object') {
  turnTo = page.options[page.selectedIndex].value;
 } else {
  turnTo = page;
 }
 self.location.href = this.createUrl(turnTo);
}
showPages.prototype.printHtml = function(mode){ //显示html代码
 this.getPage1();
 this.checkPages();
 this.showTimes += 1;
 document.write('<div id="rightdiv lt_buttonstyle"></div>');
 document.getElementById('rightdiv lt_buttonstyle').innerHTML = this.createHtml(mode);
 
}

