var currentLocation;

function showPage() {
  window.location.hash = this.identifier;
  currentLocation = this.identifier;
  getRows(this.identifier);
}

function getRows(start) {
   var url = 'getposts.php?start=' + start;
   var script = document.createElement('script');
   script.type = 'text/javascript';
   script.src = url + '&limit=20&callback=printRows';
   document.getElementsByTagName('head')[0].appendChild(script);
}

function createTableCell(value,tr) {

   // Tworzy element <td>
   var cell = document.createElement("td");

   // Tworzy węzeł tekstowy
   var text = document.createTextNode(value);

   // Dołącza utworzony węzeł tekstowy do komórki <td>
   cell.appendChild(text);

   // Dołącza komórkę <td> do wiersza <tr>
   tr.appendChild(cell);

   return tr;
}

function printRows(rowsObj) {

   var main = aaElem('main');

   // Usuwanie dawnej zawartości
   var oTable = aaElem('dataTable');
   if (oTable) main.removeChild(oTable);

   var table     = document.createElement("table");
   table.id = "dataTable";
   var tableBody = document.createElement("tbody");

   // Wiersze tabeli
   for(var i = 0; i < rowsObj.length; i++) {
        // Tworzy element <tr>
        var row = document.createElement("tr");
        row = createTableCell(rowsObj[i].id, row);

        // Znacznik anchor i tytuł
        var cell = document.createElement("td");
        cell.innerHTML = "<a href='" + rowsObj[i].guid + "'>" + rowsObj[i].title + 
                         "</a>";
        row.appendChild(cell);

        row = createTableCell(rowsObj[i].comments,row);

        tableBody.appendChild(row);
   }

   table.appendChild(tableBody);

   aaElem('main').appendChild(table);

}

function checkLocation() {
   var hash = window.location.hash;
   if (hash) {
     index = hash.split("#")[1];
     if (currentLocation != index) {
        var node = Navigation.homeNode;
        while (true) {
           if (node.identifier == index)
               break;
            node = node.next;
        }
        Navigation.setCurrentPage(node);
     }
   }
   setTimeout(checkLocation,700);
}

aaManageEvent(window,'load',function() {

   var past;
   var index;
   var hash = window.location.hash;
   if (hash) {
      index = hash.split("#")[1];
   }

   for (var i = 0; i <= 10; i++) {
      var id = i*20;
      var nav = Navigation.addNavigationNode(past,id,showPage);
      if (!past) {
        Navigation.setHome(nav);
        if (!index)
           Navigation.setCurrentPage(nav);
      }
      past = nav;
      if (id == index)
        Navigation.setCurrentPage(nav);
   }

  aaManageEvent(aaElem('next'),'click',aaBindObjMethod(Navigation,Navigation.nextPage));
  aaManageEvent(aaElem('previous'),'click',aaBindObjMethod(Navigation,Navigation.
  prevPage));
  aaManageEvent(aaElem('home'),'click', Navigation.returnHome);

   // Ustawienie zegara, aby sprawdzał lokalizację strony
   setTimeout(checkLocation,200);
});


