﻿function RecordListPageControl(id, pageSize)
{
  this.id = id;
  this.pageSize = pageSize;
  this.totalRows = -1;
  this.currentPage = 0;
  
  this.onPageChange = new REvent();
  
  RegisterControl.statics.getInstance().add(this);
  
  this.onPageChange.addEventListener(new REventListener(this.onPageChangeListener, this));
}

RecordListPageControl.prototype.attach = function(node)
{
  var table = document.createElement( "table" );
  table.id = RegisterControl.statics.encodeId(this);
  table.className = 'layoutTable';
  var tbody = document.createElement( "tbody" );
  table.appendChild( tbody );
  var tr = document.createElement( "tr" );
  tbody.appendChild( tr );
  
  var td = document.createElement( "td" );
  td.id = RegisterControl.statics.encodeId(this, "recordsCell");
  td.style.padding = "0 4px 0 0";
  tr.appendChild( td );
  
  td = document.createElement( "td" );
  tr.appendChild( td );
  var button = new StyleButton(this.getId()+'_firstPage', this.getId()+'_firstPage', null, RecordListPageControl.statics.onFirstPageClick, 'First Page', 'WebResource.axd?d=bjB1kThl5P8nlZxTJNmQFL5dRRCDIWLn7ZVo9sunYGDIb6f-edYidyloNxyeJDhISRfZ0Fq5qJI6Goz1K9zynDAzTKPmnwFdrkFlbyWHS9nKkdj77w3gent9aIdxUUILL2WfKzM3Jlr8tW_XKpMz3A2&t=633294381740000000', StyleButton.statics.Size["SHORT"]);
  button.PlaceInDom(td, Mixin_DomInsertable.statics.INSERTMETHOD_APPENDASCHILD);
  button.GetDomNode().instanceId = this.getId();

  td = document.createElement( "td" );
  tr.appendChild( td );
  button = new StyleButton(this.getId()+'_previousPage', this.getId()+'_previousPage', null, RecordListPageControl.statics.onPreviousPageClick, 'Previous Page', 'WebResource.axd?d=bjB1kThl5P8nlZxTJNmQFL5dRRCDIWLn7ZVo9sunYGDIb6f-edYidyloNxyeJDhISRfZ0Fq5qJI6Goz1K9zynDAzTKPmnwFdrkFlbyWHS9lmZCHQ_Ty0_HhrAABfMcu38W-R38HCCZJiKhExXyCGaQ2&t=633294381740000000', StyleButton.statics.Size["SHORT"]);
  button.PlaceInDom(td, Mixin_DomInsertable.statics.INSERTMETHOD_APPENDASCHILD);
  button.GetDomNode().instanceId = this.getId();
  
  td = document.createElement( "td" );
  td.id = RegisterControl.statics.encodeId(this, "pageCell");
  td.style.padding = "0 4px 0 4px";
  tr.appendChild( td );
  td.appendChild(document.createTextNode( "Page "));
  var pageSelect = document.createElement( "select" );
  pageSelect.instanceId = this.getId();
  pageSelect.id = RegisterControl.statics.encodeId(this, "pageSelect");
  pageSelect.onchange = RecordListPageControl.statics.onPageChange;
  td.appendChild(pageSelect);
  td.appendChild(document.createTextNode(" of "));
  var pageTotal = document.createElement( "span" );
  pageTotal.id = RegisterControl.statics.encodeId(this, "pageTotal");
  td.appendChild(pageTotal);
  
  td = document.createElement( "td" );
  tr.appendChild( td );
  button = new StyleButton(this.getId()+'_nextPage', this.getId()+'_nextPage', null, RecordListPageControl.statics.onNextPageClick, 'Next Page', 'WebResource.axd?d=bjB1kThl5P8nlZxTJNmQFL5dRRCDIWLn7ZVo9sunYGDIb6f-edYidyloNxyeJDhISRfZ0Fq5qJI6Goz1K9zynDAzTKPmnwFdrkFlbyWHS9l8mfvO_f8bjQwO9li-UTCMgObyDCkKsL9YV3elELAsNg2&t=633294381740000000', StyleButton.statics.Size["SHORT"]);
  button.PlaceInDom(td, Mixin_DomInsertable.statics.INSERTMETHOD_APPENDASCHILD);
  button.GetDomNode().instanceId = this.getId();

  td = document.createElement( "td" );
  tr.appendChild( td );
  button = new StyleButton(this.getId()+'_lastPage', this.getId()+'_lastPage', null, RecordListPageControl.statics.onLastPageClick, 'Last Page', 'WebResource.axd?d=bjB1kThl5P8nlZxTJNmQFL5dRRCDIWLn7ZVo9sunYGDIb6f-edYidyloNxyeJDhISRfZ0Fq5qJI6Goz1K9zynDAzTKPmnwFdrkFlbyWHS9kCrcEcJXL2jA_XxZrg2sDAsDDDB2gAAbfLIqTI7hitZQ2&t=633294381740000000', StyleButton.statics.Size["SHORT"]);
  button.PlaceInDom(td, Mixin_DomInsertable.statics.INSERTMETHOD_APPENDASCHILD);
  button.GetDomNode().instanceId = this.getId();

  node.appendChild( table );
}

RecordListPageControl.prototype.setCurrentRows = function(totalRows, startIndex)
{
  var page = Math.floor(startIndex / this.pageSize);
  if (this.totalRows != totalRows || this.currentPage != page)
  {
    this.totalRows = totalRows;
    this.currentPage = page;
    this.updateRecordsCell();
    this.updatePageCell();
    
    // put the page back in range if it is out
    var totalPages = this.getTotalPages();
    if (this.currentPage>=totalPages)
        this.setPage(totalPages-1);
  }
}

RecordListPageControl.prototype.getStartRecord = function()
{
  return (this.currentPage) * this.pageSize;
}

RecordListPageControl.prototype.getTotalPages = function()
{
  var totalPages = Math.ceil(this.totalRows / this.pageSize);
  if (1 > totalPages)
    totalPages = 1;
  return totalPages;
}

RecordListPageControl.prototype.updateRecordsCell = function()
{
  var recordsCell = document.getElementById(RegisterControl.statics.encodeId(this, "recordsCell"));
  var startRecord = this.getStartRecord() + 1;
  var endRecord = ((this.currentPage+1) * this.pageSize);
  if (endRecord > this.totalRows)
    endRecord = this.totalRows;
  if (startRecord > endRecord)
    startRecord = endRecord;
  recordsCell.innerHTML = "Records <b>" + startRecord + "</b> to <b>" + endRecord + "</b> of " + this.totalRows;
}

RecordListPageControl.prototype.updatePageCell = function()
{
  var totalPages = this.getTotalPages();
  var pageSelect = document.getElementById(RegisterControl.statics.encodeId(this, "pageSelect"));
  pageSelect.options.length = totalPages;
  for (var i = 1; i <= totalPages; i++)
  {
    pageSelect.options[i-1].text = i;
  }
  if (this.currentPage<totalPages)
    pageSelect.options.selectedIndex = this.currentPage;

  var pageTotal = document.getElementById(RegisterControl.statics.encodeId(this, "pageTotal"));
  pageTotal.innerHTML = totalPages;
}

RecordListPageControl.prototype.setPage = function(page)
{
  this.currentPage = page;
  var pageSelect = document.getElementById(RegisterControl.statics.encodeId(this, "pageSelect"));
  if (pageSelect)
    this.currentPage = pageSelect.options.selectedIndex;
}

RecordListPageControl.prototype.onPageChangeListener = function(recordIndex)
{
  this.updateRecordsCell();
}

RecordListPageControl.prototype.getId = function()
{
  return this.id;
}

RecordListPageControl.prototype.jumpPage = function()
{
  var pageSelect = document.getElementById(RegisterControl.statics.encodeId(this, "pageSelect"));
  this.currentPage = pageSelect.options.selectedIndex;
  this.onPageChange.trigger(this.getStartRecord());
}

RecordListPageControl.prototype.setPage = function(page)
{
  if(this.currentPage != page)
  {
    this.currentPage = page;
    var pageSelect = document.getElementById(RegisterControl.statics.encodeId(this, "pageSelect"));
    if (pageSelect)
      pageSelect.options.selectedIndex = this.currentPage;
    this.onPageChange.trigger(this.getStartRecord());
  }
}

RecordListPageControl.prototype.firstPage = function()
{
  this.setPage(0);
}
RecordListPageControl.prototype.lastPage = function()
{
  this.setPage(this.getTotalPages()-1);
}
RecordListPageControl.prototype.nextPage = function()
{
  var page = this.currentPage + 1;
  if (page<this.getTotalPages())
    this.setPage(page);
}
RecordListPageControl.prototype.previousPage = function()
{
  var page = this.currentPage - 1;
  if (0<=page)
    this.setPage(page);
}

RecordListPageControl.statics = new Object();

RecordListPageControl.statics.onPageChange = function() { RecordListPageControl.statics.callFunction(this.instanceId, "jumpPage"); }
RecordListPageControl.statics.onFirstPageClick = function() { RecordListPageControl.statics.callFunction(this.instanceId, "firstPage"); }
RecordListPageControl.statics.onLastPageClick = function() { RecordListPageControl.statics.callFunction(this.instanceId, "lastPage"); }
RecordListPageControl.statics.onNextPageClick = function() { RecordListPageControl.statics.callFunction(this.instanceId, "nextPage"); }
RecordListPageControl.statics.onPreviousPageClick = function() { RecordListPageControl.statics.callFunction(this.instanceId, "previousPage"); }

RecordListPageControl.statics.callFunction = function(id, functionName)
{
  var params = new Array();
  var instance = RegisterControl.statics.decodeId(id, params);
  instance[functionName].apply(instance);
}
