/**
 * Toolkit class handling the map results pagination
 */
function ResultsPaginator(results, myMap)
{
    this.resultsPerPage = 10;
    this.resultLinks = [];
    this.results = results;
    this.resultsTotal = 0;
    this.resultsContainerId = 'org_links';
    this.myMap = myMap;
    this.page = 0;
}

ResultsPaginator.prototype = new Object();

/**
 * Clears all result links
 */
ResultsPaginator.prototype.clearLinks = function(resultLink) {
    this.resultLinks = [];
}

/**
 * Appends a link to the list of total results
 */
ResultsPaginator.prototype.pushLink = function(resultLink) {
    this.resultLinks.push(resultLink);
}

ResultsPaginator.prototype.refresh = function() {
    var totalResults = this.resultsTotal;
    $('afta_total_results').innerHTML = totalResults;
}

/**
 * Calculates the current page based on a result index
 */
ResultsPaginator.prototype.setPageForResultIndex = function(resultIndex)
{
    this.setPage(parseInt(resultIndex / this.resultsPerPage) + 1);
}

/**
 * Sets the results HTML container ID attribute
 */
ResultsPaginator.prototype.setResultsContainerId = function(value)
{
  this.resultsContainerId = value;
}

/**
 * Updates the map's markers with the results on the input page number
 */
ResultsPaginator.prototype.getNewPage = function(page)
{
    this.myMap.updateResults(page, true);
}

/**
 * Builds the frontend code/markup for the input page number (result links, pages available, next/previous links, etc.)
 */
ResultsPaginator.prototype.setPage = function(page) 
{
    //var totalResults = this.resultLinks.length;
    var totalResults = this.resultsTotal;
    if (this.resultLinks.length){
      var totalPages = parseInt(totalResults / this.resultsPerPage);
      if (totalResults % this.resultsPerPage > 0) {
      	totalPages += 1;
      }
            
      //there will now only ever be 10 records returned to the pager from the database call
      var firstResultIndex = 0;
      var lastResultIndex = 9;

      var offset = (page - 1) * 10 + 1;
      var resultsOnPage = this.resultLinks.length;
      $('afta_first_result_nr').innerHTML = offset;
      $('afta_last_result_nr').innerHTML = offset + resultsOnPage - 1;

      var linksElement = $(this.resultsContainerId);
      linksElement.innerHTML = '';
      this.results.clearActiveResults();
      for (var i = firstResultIndex; i <= resultsOnPage - 1; i++) {
          linksElement.innerHTML += this.resultLinks[i];
          this.results.pushActiveResultIndex(i);
      }

      if (totalPages > 1) {
        make_pagination('afta_pagination_links_list', 'javascript: myMap.resultsPaginator.getNewPage(', page, totalPages, ')');
      } else {
        $('afta_pagination_links_list').innerHTML = '';
      }      
      $('afta_results_pagination_none').hide();
      $('afta_results_pagination_info').show();
      if (this.myMap.mapBehaviour == 'search') {
        $('afta_search_results_anchor').show();
      }
    } else {
      $('afta_pagination_links_list').innerHTML = '';
      $('org_links').innerHTML = '';
      $('afta_results_pagination_none').show();
      $('afta_results_pagination_info').hide();
      if (this.myMap.mapBehaviour == 'search') {
        $('afta_search_results_anchor').hide();
      }
    }
    this.results.refresh(false);
}

/**
 * Accessor methods for the total number of results
 */
ResultsPaginator.prototype.setTotal = function(total) {
    this.resultsTotal = total;
}

ResultsPaginator.prototype.getTotal = function() {
    return this.resultsTotal;
}