/**
* Javascipt Library for page management
* @Ray Lvxiaoxi
* dependency
*/

  function Pager( objArray, dataProcessor, pagerid, pageSize ) {
      // property define
      this.beans = objArray;
      if(pageSize) this.pageSize = pageSize;
      else this.pageSize = 15;
      if(dataProcessor) this.dataProcessor = dataProcessor;
      else this.dataProcessor = setDataToTable;
      if(pagerid) this.pagerid = pagerid;
      else this.pagerid = null;

      this.itemCount = this.beans.length;
      this.pageCount = 0;
      this.currentPageIndex = 1;
      this.currentPageSize = 0; 

      this.firstItemIndex = 0;
   	  this.lastItemIndex = 0;   
   
      this.firstItemIndex = (this.currentPageIndex - 1) * this.pageSize;
   	  this.lastItemIndex = this.currentPageIndex * this.pageSize -1;   
   
      // calculate the page count
      this.pageCount = Math.round(this.itemCount/this.pageSize); 
      if(this.itemCount > this.pageCount * this.pageSize)
          this.pageCount++;  
      
      // method define
      this.refreshData = function(){
          this.firstItemIndex = (this.currentPageIndex - 1) * this.pageSize;
   	      this.lastItemIndex = this.currentPageIndex * this.pageSize -1;   
   
          // calculate the page count
          //this.pageCount = Math.round(this.itemCount/this.pageSize); 
          //if(this.itemCount > this.pageCount * this.pageSize)
             // this.pageCount++;     
      }
      
      this.setCurrentPage = function( oIndex ){
          this.currentPageIndex = oIndex;
      }
      
      this.getCurrentPage = function(){
          return this.currentPageIndex;
      }
      
      this.getPageCount = function(){
          return this.pageCount;
      }
      
      this.getItemCount = function(){
          return this.itemCount;
      }
      
      this.getNextPage = function(){
          var nextPage = this.currentPageIndex + 1;
          if(nextPage > this.pageCount)
              nextPage = this.pageCount;
          return nextPage;
      }
      
      this.getPreviousPage = function(){
          var prePage = this.currentPageIndex - 1;
          if(prePage < 1)
              prePage = 1;
          return prePage;
      }
      
      this.getItemArray = function(){
          this.refreshData();
          return this.beans.slice(this.firstItemIndex, this.lastItemIndex + 1);
      }
      
      this.generatePageNavigation = function( pagerContainer ){
          var sNavigationHtml = "";
          if(this.pagerid){
              sNavigationHtml =
              "<table cellSpacing='0' cellPadding='0' width='100%' border='0' id='table17'>" + 
			      "<tr bgColor='#ffffff'>" +
				      "<td noWrap align='left' width='35%' height='1'></td>" +
					  "<td align='right' width='65%' height='1'>" +
					  "<font color='#104194'>共 " + this.itemCount + " 张图片&nbsp;&nbsp;<span id='curpage'>1</span>/" + this.pageCount + " 页" +							
					  "&nbsp;&nbsp;<a href='#pic_title' onclick='gotoPage(1, \"" + this.pagerid + "\");'>" +
					  "<font color='#104194'>首页</font></a>" +
					  "&nbsp;&nbsp;<a href='#pic_title' onclick='gotoPreviousPage(\"" + this.pagerid + "\");'>" +
					  "<font color='#104194'>上一页</font></a>" +
					  "&nbsp;&nbsp;<a href='#pic_title' onclick='gotoNextPage(\"" + this.pagerid + "\");'>" +
					  "<font color='#104194'>下一页</font></a>" +
					  "&nbsp;&nbsp;<a href='#pic_title' onclick='gotoLastPage(\"" + this.pagerid + "\");'>" +
					  "<font color='#104194'>最后一页</font></a> </font></td>" +
			      "</tr>" +
		      "</table>";
          }else{
              sNavigationHtml =
              "<table cellSpacing='0' cellPadding='0' width='100%' border='0' id='table17'>" + 
			      "<tr bgColor='#ffffff'>" +
				      "<td noWrap align='left' width='35%' height='1'></td>" +
					  "<td align='right' width='65%' height='1'>" +
					  "<font color='#104194'>共 " + this.pageCount + " 页&nbsp;" + this.itemCount + " 张图片" +							
					  "&nbsp;&nbsp;<a href='#' onclick='gotoPage(1);'>" +
					  "<font color='#104194'>首页</font></a>" +
					  "&nbsp;&nbsp;<a href='#' onclick='gotoPreviousPage();'>" +
					  "<font color='#104194'>上一页</font></a>" +
					  "&nbsp;&nbsp;<a href='#' onclick='gotoNextPage();'>" +
					  "<font color='#104194'>下一页</font></a>" +
					  "&nbsp;&nbsp;<a href='#' onclick='gotoLastPage();'>" +
					  "<font color='#104194'>最后一页</font></a> </font></td>" +
			      "</tr>" +
		      "</table>";          
          }
          
          if(pagerContainer){
              pagerContainer.innerHTML = sNavigationHtml;
          }else if(document.getElementsByTagName("*").page_nav){
		      document.getElementsByTagName("*").page_nav.innerHTML = sNavigationHtml;
		  }
      }
      
  }
  
  // function for UI to change the page index
    function gotoPage( pageIndex, pagerid ){
        var opager = getPagerInstance(pagerid);
        opager.setCurrentPage(pageIndex);
        var data = opager.getItemArray();
        opager.dataProcessor(data);
        document.getElementById("curpage").innerHTML=pageIndex;
    }
    
    function gotoNextPage( pagerid ){
        var opager = getPagerInstance(pagerid);
        gotoPage(opager.getNextPage(), pagerid);
    }
    
    function gotoPreviousPage( pagerid ){
        var opager = getPagerInstance(pagerid);
        gotoPage(opager.getPreviousPage(), pagerid);  
    }
    
    function gotoLastPage( pagerid ){
        var opager = getPagerInstance(pagerid);
        gotoPage(opager.getPageCount(), pagerid);     
    }
    
    function getPagerInstance( pagerid ){
        if(pagerid){
            return eval(pagerid);
        }else{          
            return eval("pager");
        }
    }