/*global $, Whisper, Backbone, textsecure, extension*/
/*
 * vim: ts=4:sw=4:expandtab
 */

// This script should only be included in background.html
(function () {
    'use strict';

    window.Whisper = window.Whisper || {};

    window.setUnreadCount = function(count) {
        if (count > 0) {
            extension.navigator.setBadgeText(count);
        } else {
            extension.navigator.setBadgeText("");
        }
    };

    window.isFocused = function() {
        return inboxFocused;
    };
    window.isOpen = function() {
        return inboxOpened;
    };

    window.drawAttention = function() {
        if (inboxOpened && !inboxFocused) {
            extension.windows.drawAttention(inboxWindowId);
        }
    };

    /* Inbox window controller */
    var inboxFocused = false;
    var inboxOpened = false;
    var inboxWindowId = 'inbox';
    var appWindow = null;
    window.openInbox = function() {
        if (inboxOpened === false) {
            inboxOpened = true;
            extension.windows.open({
                id: 'inbox',
                url: 'index.html',
                focused: true,
                width: 580,
                height: 440,
                minWidth: 600,
                minHeight: 360
            }, function (windowInfo) {
                inboxWindowId = windowInfo.id;
                appWindow = windowInfo;

                windowInfo.onClosed.addListener(function () {
                    inboxOpened = false;
                    appWindow = null;
                });

                appWindow.contentWindow.addEventListener('blur', function() {
                    inboxFocused = false;
                });
                appWindow.contentWindow.addEventListener('focus', function() {
                    inboxFocused = true;
                });

                // close the panel if background.html is refreshed
                extension.windows.beforeUnload(function() {
                    // TODO: reattach after reload instead of closing.
                    extension.windows.remove(inboxWindowId);
                });
            });
        } else if (inboxOpened === true) {
            extension.windows.focus(inboxWindowId, function (error) {
                if (error) {
                    inboxOpened = false;
                    openInbox();
                }
            });
        }
    };

    var open;
    window.openConversation = function(conversation) {
        if (inboxOpened === true) {
            var appWindow = chrome.app.window.get(inboxWindowId);
            appWindow.contentWindow.openConversation(conversation);
        } else {
            open = conversation;
        }
        openInbox();
    };
    window.getOpenConversation = function() {
        var o = open;
        open = null;
        return o;
    };
})();

@@ -421,32 +421,33 @@
       %7D%0A    %7D;%0A%0A
+%0A
     window.isFoc
@@ -1296,29 +1296,25 @@
         
-inbox
+app
 Window
-Id
  = windo
@@ -1322,11 +1322,8 @@
 Info
-.id
 ;%0A  
@@ -1336,38 +1336,44 @@
         
-app
+inbox
 Window
+Id
  = 
-w
+appW
 indow
-Info
+.id
 ;%0A%0A     
@@ -1383,26 +1383,25 @@
         
-w
+appW
 indow
-Info
 .onClose
@@ -2481,74 +2481,8 @@
 ) %7B%0A
-            var appWindow = chrome.app.window.get(inboxWindowId);%0A
     

Clean up references to appWindow