// 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),
        
        fromAcct = t.Account,
        fromName = blobVault.addressBook.getName(fromAcct),

        toAcct = t.Destination,
        toName = blobVault.addressBook.getName(toAcct),
        
        
        // 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 || fromAcct) in noBut ? '' : editButtons) +
                  '</td>' +
                  '<td class="addr" data-acct='+ toAcct + ' data-name="' + toName + '">' +
                    '<span>' + (toName || toAcct) + '</span>' +
                    ((toName || toAcct) 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,20 +1190,18 @@
         
-from
+to
 Acct = t
@@ -1201,23 +1201,27 @@
 cct = t.
-Account
+Destination
 ,%0A      
@@ -1222,20 +1222,18 @@
         
-from
+to
 Name = b
@@ -1265,19 +1265,25 @@
 ame(
-from
+to
 Acct),%0A
+        
 %0A   
@@ -1279,34 +1279,36 @@
        %0A        
-to
+from
 Acct = t.Destina
@@ -1296,35 +1296,31 @@
 romAcct = t.
-Destination
+Account
 ,%0A        to
@@ -1309,34 +1309,36 @@
 ccount,%0A        
-to
+from
 Name = blobVault
@@ -1350,34 +1350,36 @@
 essBook.getName(
-to
+from
 Acct),%0A        %0A
@@ -1378,25 +1378,16 @@
        %0A
-        %0A
         
@@ -2069,17 +2069,16 @@
         
-(
 (fromNam
@@ -2078,29 +2078,16 @@
 fromName
- %7C%7C fromAcct)
  in noBu
@@ -2317,27 +2317,15 @@
     
-(
 (toName
- %7C%7C toAcct)
  in 

Revert "more fixes to history page"