
function showPage() {
  window.location.hash = 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 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 adresu 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);
   
}

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

   var past;
   var home;
   var current;
   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) {
        home = nav;
      }
      past = nav;
      if (id == index)
        current = nav;
   }

   Navigation.setHome(home); 
   
   if (!index) {
      Navigation.setCurrentPage(home);
   } else {
      Navigation.setCurrentPage(current);
   }

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

});

