/*
 * @fileOverview Background service running for the wallet
 */
require(['backend/services', 'backend/lobby', 'backend/obelisk', 'backend/wallet', 'backend/gui', 'backend/ctxmenus'],
function(Services, LobbyService, ObeliskService, WalletService, GuiService, CtxMenusService) {

function DarkWalletService() {
    var self = this;

    // Backend services
    var lobbyService = new LobbyService(this);
    var obeliskService = new ObeliskService(this);
    var walletService = new WalletService(this);
    var ctxMenusService = new CtxMenusService(this);
    var guiService = new GuiService(this);

    /***************************************
    /* Hook up some utility functions
     */
    this.loadIdentity = function(idx) {
        return walletService.loadIdentity(idx);
    }

    // Get an identity from the keyring
    this.getIdentity = function(idx) {
        return walletService.getIdentity(idx);
    }
    this.getCurrentIdentity = function() {
        return walletService.getCurrentIdentity();
    }
    this.getLobbyTransport = function() {
        return lobbyService.getLobbyTransport();
    }

    // Start up history for an address
    this.initAddress = function(walletAddress) { return walletService.initAddress(walletAddress) }

    /***************************************
    /* Global communications
     */

    this.connect = function() {
        obeliskService.connect('ws://85.25.198.97:8888', function() {
            walletService.handleInitialConnect();
        });
    }
    this.getKeyRing = function() {
        return walletService.getKeyRing();
    }

    this.getClient = function() {
        return obeliskService.getClient();
    }
    this.getObeliskClient = function() {
        return obeliskService;
    }
    this.getServices = function() {
        return Services;
    }
}

/***************************************
/* Communications
 */
var sendInternalMessage = function(msg) {
    chrome.runtime.sendMessage(chrome.runtime.id, msg)
};

var addListener = function(callback) {
    chrome.runtime.onMessage.addListener(callback);
};


/***************************************
/* Service instance that will be running in the background page
 */
var service = new DarkWalletService();


/***************************************
/* Bindings for the page window so we can have easy access
 */

window.connect = service.connect;

window.loadIdentity = service.loadIdentity;
window.getIdentity = function(idx) { return service.getIdentity(idx); };
window.getCurrentIdentity = service.getCurrentIdentity;

window.getKeyRing = service.getKeyRing;
window.getLobbyTransport = service.getLobbyTransport

window.getClient = service.getClient;
window.getServices = service.getServices;

window.initAddress = function(_w) {return service.initAddress(_w)};

window.addListener = addListener
window.sendInternalMessage = sendInternalMessage;
});


@@ -308,29 +308,8 @@
 () %7B
-%0A    var self = this;
 %0A%0A  
@@ -1332,32 +1332,42 @@
 nect = function(
+connectUri
 ) %7B%0A        obel
@@ -1366,55 +1366,102 @@
     
-obeliskService.connect('ws://85.25.198.97:8888'
+connectUri = connectUri %7C%7C 'ws://85.25.198.97:8888';%0A        obeliskService.connect(connectUri
 , fu
@@ -2386,16 +2386,43 @@
 onnect =
+ function(_server) %7B return
  service
@@ -2429,16 +2429,27 @@
 .connect
+(_server) %7D
 ;%0A%0Awindo

allow specifying server to connect on the backend api connect function.