angular.module("omniFactories")
	.factory("Address", ["BalanceSocket", "$rootScope", "AddressManager", "WHOLE_UNIT", 
		function AddressModelFactory(BalanceSocket, $rootScope, AddressManager, WHOLE_UNIT){
			var AddressModel = function(hash,privkey,pubkey){
				var self = this;

				self.initialize = function(){
					if(!BalanceSocket.connected)
						BalanceSocket.connect();
					self.loaded = false;
					self.hash = hash;
					self.privkey = privkey;
					self.pubkey = pubkey;
					self.balance = [];
					self.assets = [];
					self.qr = "https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl="+hash+"&choe=UTF-8";
					
					//AddressManager.getTransactions(hash).then(function(result){
					//	var data = result.data;
					//	self.transactions = data.transactions;
					//})

					BalanceSocket.on("address:book", function(data){
						if (typeof data[hash]!="undefined") {
							processBalanceData(data[hash].balance);
						} else {
							BalanceSocket.emit("address:add", {data:hash});
						}
					});
					BalanceSocket.on("address:"+hash, function(data){
						processBalanceData(data.balance);
					});

					BalanceSocket.emit("address:add", {data:hash});
				}

				var processBalanceData = function(serverBalance) {
					if(self.balance != serverBalance){
						var previousBalance = self.balance;
						serverBalance.forEach(function(balance,index){
							var found = previousBalance.filter(function(prevBalance){
								return prevBalance.symbol == balance.symbol
							})[0];

							var delta = found ? balance.value-found.value : balance.value;
							var dneg = found ? balance.pendingneg - found.pendingneg : balance.pendingneg;
							var dpos = found ? balance.pendingpos - found.pendingpos : balance.pendingpos;

							if (delta != 0 || dpos !=0 || dneg != 0)
								$rootScope.$broadcast('balance:'+balance.symbol, delta, dneg, dpos);
						});

						self.balance = serverBalance;
						$rootScope.$broadcast('balance:changed');
	
						if(!self.loaded){
							self.loaded=true;
							$rootScope.$broadcast("address:loaded", {hash:hash});
						}
					}
				}

				self.transactions = function(showtesteco){
					address=self.hash;
					//console.log("Starting "+address);
					return AddressManager.getTransactions(address).then(function(result){
						var data = result.data;
						//console.log("found data for "+data.address+" its length is "+data.transactions.length);
						txs=data.transactions;
						return txs.map(function(tx){
							if ((tx.currency.propertyid < 2147483648 && tx.currency.propertyid != 2) || showtesteco === 'true') {
								return tx;
							}
						});
					});
				}

				self.estimateFee = function(){
					address=self.hash;
					return AddressManager.estimateFee(address).then(function(result){
						return result.data;
					});
				}

				self.getDisplayBalance = function(assetId){
					var currencyItem = self.balance.filter(function(asset){
						return asset.id == assetId;
					})[0];

					if(currencyItem){
						if(currencyItem.divisible)
							var value=new Big(currencyItem.value).times(WHOLE_UNIT).valueOf();
						
						return value || currencyItem.value;
					} else {
						return 0
					}
				}

				self.getPendingNeg = function(assetId){
					var currencyItem = self.balance.filter(function(asset){
						return asset.id == assetId;
					})[0];
					if(currencyItem){
						if(currencyItem.divisible)
							var value=new Big(currencyItem.pendingneg).times(WHOLE_UNIT).valueOf();
						
						return value || currencyItem.pendingneg;
					} else {
						return 0
					}
				}

				self.getPendingPos = function(assetId){
					var currencyItem = self.balance.filter(function(asset){
						return asset.id == assetId;
					})[0];
					
					if(currencyItem){
						if(currencyItem && currencyItem.divisible)
							var value=new Big(currencyItem.pendingpos).times(WHOLE_UNIT).valueOf();
						
						return value || currencyItem.pendingpos;
					} else {
						return 0
					}
				}

				self.getBalance = function(assetId){
					var currencyItem = self.balance.filter(function(asset){
						return asset.id == assetId;
					});
					if(currencyItem){
						return currencyItem.length > 0 ? currencyItem[0].divisible ? new Big(currencyItem[0].value).times(WHOLE_UNIT) : new Big(currencyItem[0].value) : new Big(0);
					} else {
						return 0
					}
				}

				self.initialize();
			}

			return AddressModel
	}]);

@@ -2963,24 +2963,42 @@
 %09%09%09%09%7D)%5B0%5D;%0A%0A
+%09%09%09%09%09var value=0;%0A
 %09%09%09%09%09if(curr
@@ -3036,32 +3036,34 @@
 m.divisible)
+ %7B
 %0A%09%09%09%09%09%09%09
 var value=ne
@@ -3042,36 +3042,32 @@
 sible) %7B%0A%09%09%09%09%09%09%09
-var 
 value=new Big(cu
@@ -3119,39 +3119,38 @@
 ;%0A%09%09%09%09%09%09
-%0A%09%09%09%09%09%09return 
+%7D else %7B%0A%09%09%09%09%09%09%09
 value
- %7C%7C 
+=
 currency
@@ -3170,38 +3170,80 @@
 %09%09%09%09
-%7D else %7B%0A%09%09%09%09%09%09return 0%0A%09%09%09%09%09%7D
+%09%7D%0A%09%09%09%09%09%7D%0A%09%09%09%09%09if (value %3C 0) %7B%0A%09%09%09%09%09%09value=0;%0A%09%09%09%09%09%7D%0A%09%09%09%09%09return value;
 %0A%09%09%09

Fix display balance to never be < 0