/*
 * Copyright (c) 2010 Nils Schneider
 * Distributed under the MIT/X11 software license, see the accompanying
 * file license.txt or http://www.opensource.org/licenses/mit-license.php.
 */

function AccountList(obj, app) {
	this.list = obj;
	
	this.clear = function() {
		this.list.children().remove();
	}

	this.updateRow = function(row, balance) {
		var balanceClass = "";
		if(balance != 0)
			balanceClass = (balance<0?'debit':'credit');

		row.children('td:last-child').removeClass().addClass("right").addClass(balanceClass).text(balance.formatBTC());
	}

	this.parseList = function(accounts, error) {
		if (error) 
			return;

		for (var account in accounts) {
			var balance = accounts[account];

			var row = this.list.children('tr[name="' + account + '"]');

			if (row.length == 0) {
				row = $('<tr></tr>');

				var html ='<td class="left">' + account.prettyAccount() + '</td>';
					html += '<td></td>';

				row.append(html);

				row.attr('name', account);
				row.click( function() {
						app.selectAccount($(this).attr('name'));
						});

				this.list.append(row);
			}

			this.updateRow(row, balance);
		}
	}

	this.refresh = function() {
		app.bitcoin.listAccounts(jQuery.proxy(this, 'parseList'));
	}
}

/*
 * Copyright (c) 2010 Nils Schneider
 * Distributed under the MIT/X11 software license, see the accompanying
 * file license.txt or http://www.opensource.org/licenses/mit-license.php.
 */

function AccountList(obj, app) {
	this.list = obj;
	
	this.clear = function() {
		this.list.children().remove();
	}

	this.updateRow = function(row, balance, timestamp) {
		var balanceClass = "";
		if(balance != 0)
			balanceClass = (balance<0?'debit':'credit');

		row.children('td:last-child').removeClass().addClass("right").addClass(balanceClass).text(balance.formatBTC());
		row.attr('update', timestamp);
	}

	this.parseList = function(accounts, error) {
		if (error) 
			return;

		var timestamp = new Date().getTime();

		for (var account in accounts) {
			var balance = accounts[account];

			var row = this.list.children('tr[name="' + account + '"]');

			if (row.length == 0) {
				row = $('<tr></tr>');

				var html ='<td class="left">' + account.prettyAccount() + '</td>';
					html += '<td></td>';

				row.append(html);

				row.attr('name', account);
				row.click( function() {
						app.selectAccount($(this).attr('name'));
						});

				this.list.append(row);
			}

			this.updateRow(row, balance, timestamp);
		}

		this.list.children().not('[update="' + timestamp + '"]').remove();
	}

	this.refresh = function() {
		app.bitcoin.listAccounts(jQuery.proxy(this, 'parseList'));
	}
}

remove deleted accounts from list