// pagination creation plugin
// author: paul irish
// mit license

// to generalize this script, just remove the onclick attributes on the anchors.


(function($) {

  $.fn.makePagination = function(options) {

	var opts = $.extend({}, $.fn.makePagination.defaults, options);
  
  
  
	return this.each(function() {
	  $this = $(this);
	  
	  var totalPages = Math.ceil(opts.total_records / opts.records_per_page);
	  var isOnFirstPage = (opts.requested_page == 1);
	  var isOnLastPage = (opts.requested_page == totalPages);  // 
	  
	  
    var paghtml = '';
    
    // if theres only one page:   empty
    if (totalPages == 1)  {
      $this.html(paghtml);
      return;
    }
      
    // previous link
    if (! isOnFirstPage) paghtml += '<a href="#" class="paginationLink" onclick="locator.requested_page = '+ (locator.requested_page-1) +'; return locator.redoSearch();">';
    paghtml += '<img class="StoreLocator_LeftArrow" border="none" src="../images/arrow_left.gif"/> '+
                RBK.i18n.retailLocator.pagPrev;
    if (! isOnFirstPage) paghtml += '</a>';
    
    paghtml +=  ' | ';
    
    // branching here!
    
    // if theres 2 to 5 pages:       p | 1 2 [3 4] | n
    if (totalPages >= 2 && totalPages <= 5){
      for (var i = 1; i <= totalPages; i++){
        if (i == opts.requested_page) { 
          paghtml += '<b>' + i  + '</b> ';  }
        else { 
          paghtml += '<a href="#" class="paginationLink" onclick="locator.requested_page = '+i+'; return locator.redoSearch();">'+i+'</a> '; }
      }
    };    
    
    // if there are > 5 pages:    p | 1 2 3 ... x | n
    if (totalPages > 5){
      
      if (opts.requested_page == 1){ var start = 1; }
      else { var start = opts.requested_page-1; }
      
      if (opts.requested_page == totalPages) start--;
        
      for (var i = start; i <= start+2; i++){
        if (i == opts.requested_page) { 
          paghtml += i + ' ';  }
        else { 
          paghtml += '<a href="#" class="paginationLink" onclick="locator.requested_page = '+i+'; return locator.redoSearch();">'+i+'</a> '; }
      }
      
      if (opts.requested_page < totalPages-2){ paghtml += ' ... '; }
      if (opts.requested_page < totalPages-1){ paghtml += '<a class="paginationLink" href="#" onclick="locator.requested_page = '+totalPages+'; return locator.redoSearch();">'+totalPages+'</a> ';
      }
    };    
    
    paghtml +=  ' | ';
                        
    // next link            
    if (! isOnLastPage) paghtml += '<a href="#" class="paginationLink" onclick="locator.requested_page = '+ (locator.requested_page+1) +'; return locator.redoSearch();">';
    paghtml +=  RBK.i18n.retailLocator.pagNext+
                '<img class="StoreLocator_RightArrow" border="none" src="../images/arrow_right.gif"/>';
    if (! isOnLastPage) paghtml += '</a>';
	  
	  $this.html(paghtml);
	  
	  
	  });
  };

  $.fn.makePagination.defaults = {
    records_per_page : 5,
    total_records : 100, //required
    requested_page : 1    //required for all pages except first
  };

})(jQuery);

