title: Pagination using Javascript
body: <p>Trying to implement pagination for jQuery accordion using javascript. I found this <a href="http://pastebin.com/wZ8ztedJ" rel="nofollow">link</a> for a javascript class to implement accordion pagination. However, it's not behaving  as expected. I played with it for a while but with no result.  Can someone please help me figure where the fault is? I'd appreciated so much. Here I created <a href="http://jsfiddle.net/7mdu7faf/" rel="nofollow">JSfiddle</a> for it.</p>
  
  <p>Javascript code</p>
  
  <pre><code>     var paginatorHandle = null;
  jQuery(document).ready(function () {
  jQuery("#dalist").accordion({
      autoHeight: false
  });
  
  paginatorHandle = jQuery("#dalist").paginateAccordion({
      "currentPage": 0,
          "itemsPerPage": 3,
          "paginatorControl": jQuery("#accordionPaginator")
  });
  
  // initial paginate call
  paginatorHandle.paginate();
  
  jQuery("#accordionPaginator .nextPage").click(function () {
      paginatorHandle.nextPage();
  });
  
  jQuery("#accordionPaginator .previousPage").click(function () {
      paginatorHandle.previousPage();
  });
  
  jQuery("#accordionPaginator .goToPage").change(function () {
      var pageIndex = parseInt($(this).val(), radix);
      paginatorHandle.goToPage(pageIndex);
     });
  });
  
  
  //this is the main class
  
  function AccordionPaginator(element, currentPage, itemsPerPage, paginatorControl) {
  this.element = element;
  this.currentPage = currentPage;
  this.itemsPerPage = itemsPerPage;
  this.paginatorControl = paginatorControl;
  
  // does the actual pagination (shows/hides items)
  this.paginate = function () {
      var index = this.currentPage * this.itemsPerPage;
  
      element.accordion("activate", index);
      element.children().hide();
  
      if (index &lt; 0) {
          this.element.children("div:first").show();
          this.element.children("h3:first").show();
      } else {
  
          this.element.children("div:eq(" + index + ")")
              .show();
  
          this.element.children("h3:eq(" + index + "),h3:gt(" + index + ")")
              .filter(":lt(" + this.itemsPerPage + ")")
              .show();
      }
  
      this.refreshControl();
  };
  
  // increments the currentPage var (if possible) and calls paginate
  this.nextPage = function () {
      if (this.currentPage + 1 &gt;= this.getMaxPageIndex()) {
          return;
      }
  
       this.currentPage++;
      this.paginate();
  };
  
  // decrements the currentPage var (if possible) and calls paginate
  this.previousPage = function () {
      if (this.currentPage - 1 &lt; 0) {
          return;
      }
  
      this.currentPage--;
      this.paginate();
  };
  
  // sets currentPage var (if possible) and calls paginate
  this.goToPage = function (pageIndex) {
      if ((pageIndex &lt; 0) || (pageIndex &gt;= this.getMaxPageIndex())) {
          return;
      }
  
      this.currentPage = pageIndex;
      this.paginate();
      };
  
  // returns the maximum of pages possible with the current number of items
  this.getMaxPageIndex = function () {
      var items = this.element.children("h3");
      var fullPages = items.length / this.itemsPerPage;
      var restPage = items.length % (this.itemsPerPage &gt; 0 ? 1 : 0);
      return fullPages + restPage;
  };
  
  // fills up the paginator control
  this.refreshControl = function () {
      if (this.paginatorControl === null) {
          return;
      }
  
      var pageList = this.paginatorControl.children(".goToPage");
      pageList.empty();
      for (var i = 0; i &lt; this.getMaxPageIndex(); i++) {
          pageList.append("&lt;option value=\"" + i + "\"&gt;" + (i + 1) + "&lt;/option&gt;");
      }
      pageList.val(this.currentPage);
  };
  }
  
  jQuery.fn.extend({
  paginateAccordion: function (options) {
      var currentPage = options.currentPage ?parseInt(options.currentPage, radix) : 0;
      var itemsPerPage = options.itemsPerPage ? parseInt(options.itemsPerPage, radix) : 5;
      var paginatorControl = options.paginatorControl;
  
      return new AccordionPaginator(this, currentPage, itemsPerPage, paginatorControl);
      }
  });
  </code></pre>