// -----------------------------------------------------------------------------------
// Wykorzystane źródło: 
// http://www.mariuszlipinski.pl/2007/12/prezentacja-duych-tabel-na-stronach.html
// Dzięki!
// -----------------------------------------------------------------------------------

function TableRewinder
   (tableName, vCols, vRows, leftOffset, topOffset, leftHeader, topHeader) {

   // 1.wartoć atrybutu id tabeli, na której operujemy
   this.tableName = tableName;

   // 2.ile kolumn, wliczając okno i nagłówek ma tabela
   this.visibleCols = vCols;

   // 3.ile wierszy, wliczając okno i nagłówek ma tabela
   this.visibleRows = vRows;

   // 4.przesunięcie okna wzgledem pierwszej kolumny tresci tabeli
   this.leftOffset = leftOffset;

   // 5.przesunięcie okna wzgledem pierwszego wiersza tresci tabeli
   this.topOffset = topOffset;

   // 6.ile kolumn tabeli stanowi lewy nagłówek
   this.leftHeader = leftHeader;

   // 7.ile wierszy tabeli stanowi górny nagłówek
   this.topHeader = topHeader;

   this.getTable = function() {
      return document.getElementById(this.tableName);
   };

   this.visibleLeftmost = function() {
      return this.leftHeader + this.leftOffset;
   };

   this.visibleRightmost = function() {
      return this.leftOffset + this.visibleCols - 1;
   };

   this.visibleTopmost = function() {
      return this.topHeader + this.topOffset;
   };

   this.visibleBottommost = function() {
      return this.topOffset + this.visibleRows - 1;
   };

   this.canGoLeft = function() {
      return this.visibleLeftmost() > this.leftHeader;
   };

   this.canGoRight = function() {
      return this.visibleRightmost() < this.getTable().rows[0].cells.length - 1;
   };

   this.canGoUp = function() {
      return this.visibleTopmost() > this.topHeader;
   };

   this.canGoDown = function() {
      return this.visibleBottommost() < this.getTable().rows.length - 1;
   };

   this.toggleRows = function(hideRowNum, showRowNum) {
      this.getTable().rows[hideRowNum].style.display = 'none';
      this.getTable().rows[showRowNum].style.display = '';
   };

   this.toggleColumns = function(hideColNum, showColNum) {
      for(i = 0 ; i < this.getTable().rows.length; i++) {
         rowColumns = this.getTable().rows[i].cells;

         rowColumns[hideColNum].style.display = 'none';
         rowColumns[showColNum].style.display = '';
      }
   };

   this.rewindLeft = function() {
      if(this.canGoLeft()) {
         this.toggleColumns(this.visibleRightmost(), this.visibleLeftmost() - 1);

         this.leftOffset -= 1;
      }
   };

   this.rewindRight = function() {
      if(this.canGoRight()) {
         this.toggleColumns(this.visibleLeftmost(), this.visibleRightmost() + 1);

         this.leftOffset += 1;
      }
   };

   this.rewindUp = function() {
      if(this.canGoUp()) {
         this.toggleRows(this.visibleBottommost(), this.visibleTopmost() - 1);

         this.topOffset -= 1;
      }
   };

   this.rewindDown = function() {
      if(this.canGoDown()) {
         this.toggleRows(this.visibleTopmost(), this.visibleBottommost() + 1);

         this.topOffset += 1;
      }
   };
}





