var Pagination = Class.create();
Pagination.prototype = {
  paginate: function() {
    //display correctly the status indicator. "1/2" means we're on the page number 1 with a total number of 2 pages
    if (this.indicator)
      if (this.total_items > 0) {
        indicator_text = this.page + '/' + this.total_pages;
		    this.indicator.innerHTML = indicator_text;
        this.indicator.up().show();
      } else {
        this.indicator.up().hide();
      }
    //display or not a node which enventually included a text node saying "the cart is sempty"
    if (this.empty_node)
      if (this.total_items == 0) {
        this.empty_node.show();
      } else {
        this.empty_node.hide();
      }
    //display or not the nodes if they are ot not visible
    min = (this.page - 1) * this.per_page;
    max = min + this.per_page;
    this.pagination_node.childElements().each(function(node, index) {
	    if( index >= min && index < max ) {
		    if (node)
			    node.style.display="block";
			} else {
			  if (node) 
				  node.style.display="none";
			}
		});
  },
  refresh: function() {
    //we modified programatically the element nodes and we need to actualise the situation
    this.total_items     = this.pagination_node.childElements().size();
    this.total_pages     = Math.ceil(this.total_items / this.per_page);
    this.paginate();    
  },
  initialize: function(pagination_node, button_class, empty_node, indicator, per_page) {
    this.pagination_node = $(pagination_node);
    // if the button_class is "button" the button will be named "button_next" and "button_back"
    this.button_class    = button_class;
    this.empty_node      = $(empty_node);
    this.indicator       = $(indicator);
    this.per_page        = per_page;
    this.page            = 1;
    this.total_items     = this.pagination_node.childElements().size();
    this.total_pages     = Math.ceil(this.total_items / this.per_page);
    this.paginate();
  },
  next: function(e) {
    //go to the next page
    this.page++;
    if (this.page > this.total_pages)
      this.page = this.total_pages;
    if (e) {
      Event.stop(e);
    }
    this.paginate();
  },
  back: function(e) {
    //go to the previous page
    this.page--;
    if (this.page < 1)
      this.page = 1;
    if (e) {
		  Event.stop(e);
    }
    this.paginate();
  }  
};
