// This tab should list all the transactions you have done with any of your accounts

var HistoryPage = new (function () {
  var hist = {};
  
  this.onShowTab = function () {
    $('#HistoryTable').empty();
    _.each(hist, function (t, hash) {
      HistoryPage.renderTransaction(t);
    });
  };

  //<table class="dataTable" ><tr><th>#</th><th>Ledger</th><th>Source</th><th>Destination</th><th>Amount</th><th>Status</th></tr><tbody id="HistoryTable"></tbody></table>
  this.onHistoryResponse = function (res, noError) {
    res = res.result || res;
    if (noError && res) {
      _.each(
        res.transactions || [],
        function (t) {
          HistoryPage.addTransaction(t, false);
        }
      );
    }
  };
  
  this.addTransaction = function (t, adjust) {
    hist[t.hash] = t;
    HistoryPage.renderTransaction(t, adjust);
  };
  
  this.renderTransaction = function (t, adjust) {
    if (t.TransactionType == 'CreditSet') {
      var amount = ncc.displayAmount(t.LimitAmount.value);
    } else if (t.TransactionType == 'OfferCreate') {
      return;
    } else {
      var amount = ncc.displayAmount(t.Amount);
    }
    
    var oldEntry = $('#' + t.hash),
        
        toAcct = t.Destination,
        toName = blobVault.addressBook.getName(toAcct),
        
        fromAcct = t.Account,
        fromName = blobVault.addressBook.getName(fromAcct),
        
        // no button if name matches one of these
        noBut = { 'you': 1, 'undefined': 1};
        editButtons = (' <button class="edit" onclick="HistoryPage.editName(this)">edit</button>' +
                    '<button class="save" onclick="HistoryPage.saveName(this)">save</button>');
        
        console.log(fromName, toName);
        
        entry = ( '<td>' + (t.inLedger || t.ledger_current_index || t.ledger_closed_index) + '</td>' +
                  '<td>' + t.TransactionType + '</td>' +
                  '<td class="addr" data-acct='+ fromAcct + ' data-name="' + fromName + '">' +
                    '<span>' + (fromName || fromAcct) + '</span>' +
                    (fromName in noBut ? '' : editButtons) +
                  '</td>' +
                  '<td class="addr" data-acct='+ toAcct + ' data-name="' + toName + '">' +
                    '<span>' + (toName || toAcct) + '</span>' +
                    (toName in noBut ? '' : editButtons) +
                  '</td>' +
                  '<td>' + amount + '</td>' +
                  '<td>' + t.status + '</td>' );

    if (oldEntry.length) {
      oldEntry.html(entry);
    } else {
      $('#HistoryTable').prepend('<tr id="' + t.hash + '">' + entry + '</tr>');

      if (adjust) {
        if (t.TransactionType == 'CreditSet' && t.Account == ncc.accountID) {
          ncc.changeBalance('XNS', -t.Fee);
          return;
        }

        var curr = t.Amount.currency || 'XNS',
            amt = t.Amount.value || t.Amount;

        if (t.Account == ncc.accountID) {
          ncc.changeBalance(curr, -amt);
          ncc.changeBalance('XNS', -t.Fee);
        }

        if (t.Destination == ncc.accountID) {
          ncc.changeBalance(curr, amt);
        }
      }
    }
  }

  this.editName = function (cellElem) {
    var cell = $(cellElem).parent(),
        content = cell.find('span'),
        saveButton = cell.find('button.save'),
        editButton = cell.find('button.edit'),
        name = content.text(),
        input = $('<input>').val(name)
                            .keydown(function (e) { if (e.which == 13) saveButton.click(); });
    
    saveButton.show();
    editButton.hide();
    content.html(input);
    input.select();
  };
  
  this.saveName = function (cellElem) {
    var cell = $(cellElem).parent(),
        addr = cell.attr('data-acct'),
        newName = cell.find('input').val();
    
    blobVault.addressBook.setEntry(newName, addr);
    blobVault.save();
    
    HistoryPage.onShowTab();
  };

})();


@@ -1190,18 +1190,20 @@
         
-to
+from
 Acct = t
@@ -1203,27 +1203,23 @@
 cct = t.
-Destination
+Account
 ,%0A      
@@ -1220,18 +1220,20 @@
         
-to
+from
 Name = b
@@ -1265,25 +1265,19 @@
 ame(
-to
+from
 Acct),%0A
-        
 %0A   
@@ -1273,36 +1273,34 @@
 Acct),%0A%0A        
-from
+to
 Acct = t.Account
@@ -1284,39 +1284,43 @@
      toAcct = t.
-Account
+Destination
 ,%0A        fromNa
@@ -1305,36 +1305,34 @@
 nation,%0A        
-from
+to
 Name = blobVault
@@ -1348,28 +1348,26 @@
 ook.getName(
-from
+to
 Acct),%0A     
@@ -1362,32 +1362,41 @@
 Acct),%0A        %0A
+        %0A
         // no bu
@@ -2070,16 +2070,17 @@
         
+(
 (fromNam
@@ -2076,24 +2076,37 @@
   ((fromName
+ %7C%7C fromAcct)
  in noBut ? 
@@ -2328,23 +2328,35 @@
         
+(
 (toName
+ %7C%7C toAcct)
  in noBu

Revert "Revert "more fixes to history page""