



var HydroCommunityOutreachSidebar = Class.create({
  		
  initialize: function(element, paginationControlElement, data, formator, gmap ) {
    
	this.pageSize = 3;
	this.numPages = Math.ceil(data.length / this.pageSize);
	this.numPagnationNumberLinks = 3;
	this.currentPage = 1;
	this.formator=formator;
		
    this.data = data;
    this.map = gmap;
        
    this.contentArea = $(element);
    this.controlArea = $(paginationControlElement);
    
    if (!this.contentArea)
    	alert('Problem in page setup: sidebar with element id "' + element + '" is not defined!');
    
    if (!this.controlArea)
    	alert('Problem in page setup: sidebar with element id "' + paginationControlElement + '" is not defined!');
    
       
    // if there is only one page: Don't show pagination!
    
    if (this.numPages > 1){
    	
    	var divider = '<span class="bchcopPaginationDivider">|</span>';
    	
	    this.pageLinks = new Array();
	    this.prevLink = new Element('a', { href: '#'}).update("Prev").addClassName('bchcopDisabledPaginationLink').observe('click', this._previousPageEventHandler.bind(this));
		this.nextLink = new Element('a', { href: '#'}).update("Next").addClassName('bchcopEnabledPaginationLink').observe('click', this._nextPageEventHandler.bind(this));
		  
		this.controlArea.insert(this.prevLink);
		this.controlArea.insert(divider);
		  	
		for (var i=1; i <= Math.min(this.numPagnationNumberLinks, this.numPages); i++){
			var link = new Element('a', { href: '#'}).addClassName(i==1?'bchcopDisabledPaginationLink':'bchcopEnabledPaginationLink').update(i).observe('click', this._pageLinkEventHandler.bind(this));
			this.pageLinks.push(link);
			this.controlArea.insert(link);
			this.controlArea.insert(divider);
		}
		
		this.controlArea.insert(this.nextLink);
		
    }
    
    this.refreshContent();
  },

  
  setPage: function(page){
	  
	  if (page > this.numPages)
		  page = this.numPages;
	  
	  if (page < 1)
		  page = 1;
	  
	  if (page == this.currentPage)
		  return;
	
	  this.currentPage = page;
	  this.refresh();
  },
  
  
  refresh: function() {
	    //console.log('REFRESH: Current Page: %d   Pagesize: %d    # records: %d    maxPage: %d', this.currentPage, this.pageSize, this.data.length, this.numPages);
	    this.refreshControls();
	    this.refreshContent();
  },
  
   
  _setPageEventHandler: function(e){
	  var data = $A(arguments);
	  var newPageNum = data[1];
	  this.setPage(newPageNum);  
  },
  
  _previousPageEventHandler: function(e){
	  this.setPage(this.currentPage-1);  
  },
  
  _nextPageEventHandler: function(e){
	  this.setPage(this.currentPage+1);  
  },
  
  _pageLinkEventHandler: function(e){
	  var link = Event.element(e);
	  var newPage = parseInt(link.innerHTML);
	  this.setPage(newPage);  
  },
  
  
  refreshContent: function(){
	  
	  var startIndex = (this.currentPage-1) * this.pageSize; 
	  	  
	  this.contentArea.update();
	  
	  for (var i=0 ; i < this.pageSize && startIndex+i < this.data.length ; i++ ){
		 this.contentArea.insert(this.formator(this.data[startIndex+i]));  
	  }
	  
	  
  },
  
  refreshControls: function(){
	  
	  var pageNum = Math.max(1, this.currentPage-1);
	  
	  pageNum = Math.min(pageNum, this.numPages - this.pageLinks.length +1 );
	  
	  for (var i=0; i<this.pageLinks.length; i++){
		  this.pageLinks[i].update(pageNum);
		  if (pageNum == this.currentPage){
			  this.pageLinks[i].className = 'bchcopDisabledPaginationLink';
		  }else{
			  this.pageLinks[i].className = 'bchcopEnabledPaginationLink';
		  }
		  pageNum++;
	  }
	 
	  this.prevLink.className = this.currentPage == 1 ? 'bchcopDisabledPaginationLink' : 'bchcopEnabledPaginationLink';
	  this.nextLink.className = this.currentPage == this.numPages ? 'bchcopDisabledPaginationLink' : 'bchcopEnabledPaginationLink';
	  
  }
});
