/**
 *
 * Copyright (c) 2007 Wu peng
 * silenceapple@gmail.com
 * http://www.aragoncg.com/
 */

(function() {
  jQuery.fn.tablePager = function(pageBy, options) {
    pageBy = isNaN(parseInt(pageBy)) ? 10000 : parseInt(pageBy)
    rowsLength = 0;
    pages = 0;
    currentPage = 1;
    var settings = jQuery.extend({
    previous:"tp_previous",
    next:"tp_next",
    pageSeqBox1:"#tp_seqbox1",
    pageSeqBox2:"#tp_seqbox2",
    pageSeqBoxClass:"tp-seqbox",
    debug: false
    }, options);

    function init(tbody) {
      var firstRowIdx = getFirstRowIdx(1);
      var lastRowIdx = getLastRowIdx(firstRowIdx);
      createPageSequence();
      // show next page rows. Firstly hide all rows.
      display(tbody, firstRowIdx, lastRowIdx);
      if (settings.debug)  debug("init");
    }

    function debug(name) {
      if (window.console && window.console.log) {
        var firstRowIdx = getFirstRowIdx(currentPage);
        var lastRowIdx = firstRowIdx + pageBy -1;
        console.log(name+">>>>>>>>>>>>>");
        if (arguments.length > 1) {
          console.log(arguments[1]);
        }
        console.log("Total page: "+pages);
        console.log("Current page: "+currentPage);
        console.log("FirstRowsIdx in Current Page: "+firstRowIdx);
        console.log("LastRowsIdx in Current Page: "+lastRowIdx);
      } else {
        alert("Faild to Debug: not find Firebug!");
      }
    }

    function countPages(rowsLen) {
      rowsLength = rowsLen;
      var _p = rowsLength/pageBy;
      if ((_p+"").indexOf(".") != -1) {
        pages = parseInt(_p)+1;
      } else {
        pages = _p;
      }
    }
   
    function createPageSequence() {
      if (pages > 1) {
        var con1 = "<table><tr><td><a href='javascript:void(0);' id='1_"+settings.previous+"'>< Prev</a></td>";
        for (i=0;i<pages;i++) {
          con1 +=      "<td><a href='javascript:void(0);' class='page_num' page_num='tp_" + (i+1) + "'><b>" + (i+1) + "</b></a></td>";
        }
        con1 +=        "<td><a href='javascript:void(0);' id='1_"+settings.next+"'>Next ></a></td></tr></table>";
        if (settings.debug) debug("seq box string", con1);
        //jQuery(settings.pageSeqBox1).append(con1);
        jQuery(settings.pageSeqBox1).html(con1);

        var con2 = "<table><tr><td><a href='javascript:void(0);' id='2_"+settings.previous+"'>< Prev</a></td>";
        for (i=0;i<pages;i++) {
          con2 +=      "<td><a href='javascript:void(0);' class='page_num' page_num='tp_" + (i+1) + "'><b>" + (i+1) + "</b></a></td>";
        }
        con2 +=        "<td><a href='javascript:void(0);' id='2_"+settings.next+"'>Next ></a></td></tr></table>";
        if (settings.debug) debug("seq box string", con2);
        //jQuery(settings.pageSeqBox2).append(con2);
        jQuery(settings.pageSeqBox2).html(con2);
      }
    }

    function getFirstRowIdx(pageNum) {
     if (pageNum > 1){
      return pageBy*(pageNum-1);
      }else{ return 1;}

    }

    function getLastRowIdx(firstRowIdx) {
      return firstRowIdx + pageBy - 1;
    }

    function display(tbody, firstRowIdx, lastRowIdx, action) {
      jQuery("tr", tbody).hide();
      jQuery("tr:lt("+(lastRowIdx+1)+"):gt("+(firstRowIdx-1)+")", tbody).show();
      if (settings.debug)  debug(action);
    }

    function showPage(tbody, pageNum) {
      if (pages >= pageNum > 0) {
         var firstRowIdx = getFirstRowIdx(pageNum);
         var lastRowIdx = getLastRowIdx(firstRowIdx);
         // show next page rows. Firstly hide all rows.
         display(tbody, firstRowIdx, lastRowIdx, "show");
         currentPage = pageNum;
      }
    }

    function showNextPage(tbody) {
      if (currentPage < pages) {
        var firstRowIdx = getFirstRowIdx(currentPage + 1);
        var lastRowIdx = getLastRowIdx(firstRowIdx);
        // show next page rows. Firstly hide all rows.
        display(tbody, firstRowIdx, lastRowIdx, "next");
        currentPage += 1;
      }
    }

    function showPreviousPage(tbody) {
      if (currentPage > 1) {
        var firstRowIdx = getFirstRowIdx(currentPage-1);
        var lastRowIdx = getLastRowIdx(firstRowIdx);
        // show next page rows. Firstly hide all rows.
        display(tbody, firstRowIdx, lastRowIdx, "previous");
        currentPage -= 1;
      }
    }
    return this.each(function() {
      $this = $(this);
      jTbody = $("tbody", this);
      trs = $("tr", jTbody[0]);
      countPages(trs.length);
      init(jTbody[0]);
      $("#1_"+settings.previous).addClass("tp-previous").bind("click", function(){showPreviousPage(jTbody[0])});
      $("#1_"+settings.next).addClass("tp-next").bind("click", function(){showNextPage(jTbody[0])});
      $("#2_"+settings.previous).addClass("tp-previous").bind("click", function(){showPreviousPage(jTbody[0])});
      $("#2_"+settings.next).addClass("tp-next").bind("click", function(){showNextPage(jTbody[0])});

      $("a.page_num").bind("click", function(){// this should run after init function.
        var pageNum = parseInt($(this).attr("page_num").replace("tp_",""));
        showPage(jTbody[0], pageNum);
      });
    });
  };
})(jQuery);


