var TNListPager = Class.create();

TNListPager.prototype = {
  initialize: function (viewLimit, idToCount, pagerWidth, currPageNo, options)
  {
    this.idToCount  = idToCount;
    this.viewLimit  = viewLimit || 10;
    this.currPageNo = currPageNo || 0;
    this.pagerWidth = pagerWidth || 9;

    this.options =
    {
      pageFirstID:       'page_first',
      pagePreviousID:    'page_previous',
      pageNextID:        'page_next',
      pageLastID:        'page_last',
      pageHolderID:      'page_holder',
      pagerID:           'item_pager',
      pageSelectedID:    'page_selected',
      pageNotSelectedID: 'page_not_selected',

      countAllID:   'items_count',
      countFirstID: 'first_item_shown',
      countLastID:  'last_item_shown',
      counterID:    'item_counter',

      showCounter: 'yes',
      showPager:   'auto'
    };

    Object.extend(this.options, options);

    this.lastPageNo = 0;
    this.numItems   = 0;
    this.firstShown = -1;
    this.lastShown  = -1;
    this.items = new Array();

    this.setCounterID(this.idToCount);

  },
  setCounterID: function(id)
  {
    this.idToCount  = id;

    this.items      = document.getElementsByClassName(id);
    this.lastPageNo = parseInt((this.items.length - 1)/ this.viewLimit);
    this.lastPageNo = this.lastPageNo < 0 ? 0 : this.lastPageNo;
    this.numItems   = this.items.length;
    
    this.setPageNo(this.currPageNo);
  },
  setPageNo: function(pageNo)
  {
    if(pageNo < 0) pageNo = 0;
    if(pageNo > this.lastPageNo) pageNo = this.lastPageNo;
    this.currPageNo = pageNo;

    var offset = pageNo*this.viewLimit;
    this.firstShown = -1;

    // Hide/show elements
    for(var i = 0; i < this.items.length; i++)
    {
      var show = i >= offset && i-offset < this.viewLimit;
      if(show && this.firstShown < 0) this.firstShown = i + 1;
      if(show) this.lastShown = i + 1;
   
      this.items[i].style.display = show ? '' : 'none';
    }

    if((this.options.showPager == 'auto' && this.numItems > this.viewLimit) ||
       this.options.showPager == 'yes')
    {
      Element.show(this.options.pagerID);

      var h = $(this.options.pageHolderID);
      h.innerHTML = '';

      // prev/next buttons
      $(this.options.pageFirstID).style.display = pageNo > 0 ? '' : 'none';
      $(this.options.pageFirstID).onclick =  function () { return this.setPageNo(0); }.bind(this);
   
      $(this.options.pagePreviousID).style.display = pageNo > 0 ? '' : 'none';
      $(this.options.pagePreviousID).onclick =  function () { return this.setPageNo(pageNo - 1); }.bind(this);
   
      $(this.options.pageNextID).style.display = pageNo != this.lastPageNo ? '' : 'none';
      $(this.options.pageNextID).onclick = function () { return this.setPageNo(pageNo + 1); }.bind(this);
   
      $(this.options.pageLastID).style.display = pageNo != this.lastPageNo ? '' : 'none';
      $(this.options.pageLastID).onclick =  function () { return this.setPageNo(this.lastPageNo); }.bind(this);

      var toGo       = this.pagerWidth;   
      var loopPageNo = pageNo - parseInt(this.pagerWidth / 2);
      loopPageNo     = loopPageNo < 0 ? 0 : loopPageNo;

      while(toGo > 0 && loopPageNo <= this.lastPageNo)
      {
        var o = $(loopPageNo == pageNo ? this.options.pageSelectedID : this.options.pageNotSelectedID).cloneNode(true);
        o.id = '';
        o.innerHTML = loopPageNo + 1;
        o.onclick = function (showPage) { return this.setPageNo(showPage); }.bind(this, loopPageNo);
        h.appendChild(o);

        loopPageNo++;
        toGo--;
      };
    }

    if((this.options.showCounter == 'auto' && this.numItems > this.viewLimit) ||
       this.options.showCounter == 'yes')
    {
      Element.show(this.options.counterID);

      $(this.options.countAllID).innerHTML   = this.numItems;
      $(this.options.countFirstID).innerHTML = this.firstShown < 1 ? 1 : this.firstShown;
      $(this.options.countLastID).innerHTML  = this.lastShown;
    }
    return false;
  }
};
