pechakucha.EventPager = function() {
  this.totalPages = Math.ceil(($('ul#city-events li').size()) / 10);
  this.currentPage = 1;
}

pechakucha.EventPager.prototype = {
  init: function () {

    if (this.totalPages == 1) {
      $('#small-pagination').hide();
      return;
    } 
    $("#all-events-mask").css("overflow", "hidden").width(150);  
    $("#all-events-wrapper").width(99999).addClass("clearfix");  
    var pages = [];
    for(var i=1; i<=this.totalPages; i++ )
    {
      var pgs = $("li.page-"+i);
      pages[pages.length] = $("<ul></ul>").addClass("vcalendar").addClass("city-events-list").attr("id", "city-events-"+i).append(pgs.clone()).css("float", "right").css("padding-left", "10px").width(150);
      $("#all-events-wrapper").append(pages[pages.length-1]);
    }
    $('ul#city-events').remove();
    $('#small-pagination a.older-link').click($D(this, this.olderPage));
    $('#small-pagination a.newer-link').click($D(this, this.newerPage));
    //$("#all-events-wrapper").width(pages.length*180);
    $('#small-pagination span.older-link').hide();
    this.initPaginationNav();
    var parent = this;
    $("#all-events-mask").scrollTo(pages.length*140+'px', 100, function(){
      var sel = $("li.selected-event");
      if(sel[0])
      {
        parent.currentPage = sel[0].className.split(" ")[0].split("page-").join("");
        $("#all-events-mask").scrollTo('#city-events-'+parent.currentPage);
      }
      else
      {
        $("#all-events-mask").scrollTo('#city-events-1');
      }
      parent.initPaginationNav();
    });
    
  },
  olderPage: function() {
    this.currentPage++;
    $("#all-events-mask").scrollTo('#city-events-'+this.currentPage, 300, "easeOutQuad");
    this.initPaginationNav();
    return false;
  },
  newerPage: function() {
    this.currentPage--;
    $("#all-events-mask").scrollTo('#city-events-'+this.currentPage, 300, "easeOutQuad");
    this.initPaginationNav();
    return false;
  },
  initPaginationNav: function() {
    
    // show link by default
    $('#small-pagination a').show();
    $('#small-pagination span').hide();
    if(this.currentPage==1) {
      //1st page so hide "newer"
      $('#small-pagination a.newer-link').hide();
      $('#small-pagination span.newer-link').show();
    }
    if(this.currentPage==this.totalPages) {
      //last page so hide "older"
      $('#small-pagination a.older-link').hide();
      $('#small-pagination span.older-link').show();
    }
  }
}

$(function(){

  ep = new pechakucha.EventPager();
  ep.init();
  
  
   
})
