const tabs = require("tabs");
const windows = require("windows");
const self = require("self");
const pageMod = require("page-mod");
const sideBar = require('sidebar').sidebar;
const miniMap = require("minimap");

function shouldICare(tab) {
    // FIXME: this needs to be changed when working on background tabs
    // support (generate minimap even for non-active tab)
    // FIXME: does not work with multiple windows anyway...
    if (tab != tabs.activeTab) {
        // console.warn('Call from ' + tab.url + ' is ignored, not the active tab');
        return false;
    }
    return true;
}

function init() {
    let sidebar = this;
    let minimap = new miniMap.Minimap(sidebar);

    pageMod.PageMod({
        include: "*",
        contentScriptFile: self.data.url('content-notifychanges.js'),
        contentScriptWhen: 'start',
        attachTo: ['existing', 'top'],  // FIXME: no frames for now
        onAttach: function(worker) {

            worker.port.on('resize', function(e) {
                if (shouldICare(worker.tab)) {
                    minimap.drawMinimap();
                }
            });

            worker.port.on('load', function(e) {
                if (shouldICare(worker.tab)) {
                    minimap.drawMinimap();
                }
            });

            worker.port.on('DOMContentLoaded', function(e) {
                if (shouldICare(worker.tab)) {
                    minimap.drawMinimap();
                }
            });

            worker.port.on('scroll', function(e) {
                if (shouldICare(worker.tab)) {
                    minimap.drawScroller();
                }
            });

            worker.port.on('mutation', function(e) {
                if (shouldICare(worker.tab)) {
                    minimap.drawMinimap();
                }
            })
        }
    });    

    tabs.on('activate', function(tab) {
        // Redraw on activate since for the moment we only have one main
        // canvas that needs to be redrawn everytime we change tabs.
        minimap.drawMinimap();
    });
}

function createSideBar(window) {
    let sidebars = sideBar({
        'url': self.data.url('sidebar.html'),
        'title': 'Minimap Scroller Sidebar',
        'right': true,
        'width': '200px',
        'onDomReady': init
    });

    let current = sidebars.getSidebar(window);
    if (current) {
        current.show();
    } else {
        console.warn('Could not find sidebar for window');
        console.warn(window);
    }
}

for each (var window in windows.browserWindows) {
    createSideBar(window);
}
windows.browserWindows.on('open', function(window) {
    createSideBar(window);
});
@@ -2077,48 +2077,11 @@
 %0A%7D%0A%0A
-function createSideBar(window) %7B%0A    let
+var
  sid
@@ -2098,20 +2098,16 @@
 deBar(%7B%0A
-    
     'url
@@ -2136,28 +2136,24 @@
 bar.html'),%0A
-    
     'title':
@@ -2165,32 +2165,11 @@
 imap
- Scroller Sidebar',%0A    
+',%0A
     
@@ -2183,20 +2183,16 @@
 : true,%0A
-    
     'wid
@@ -2208,244 +2208,95 @@
 px',
-%0A        'onDomReady': init%0A    %7D);%0A%0A    let current = sidebars.getSidebar(window);%0A    if (current) %7B%0A        current.show();%0A    %7D else %7B%0A        console.warn('Could not find sidebar for window');%0A        console.warn(window);%0A    %7D%0A%7D
+ // FIXME: remember user preference%0A    'minWidth': '150px',%0A    'onDomReady': init%0A%7D);
 %0A%0Afo
@@ -2343,35 +2343,55 @@
 dows) %7B%0A    
-create
+let sidebar = sidebars.get
 Side
-B
+b
 ar(window);%0A
@@ -2390,17 +2390,67 @@
 indow);%0A
-%7D
+    if (sidebar) %7B%0A        sidebar.show();%0A    %7D%0A%7D%0A
 %0Awindows
@@ -2503,19 +2503,39 @@
     
-create
+let sidebar = sidebars.get
 Side
-B
+b
 ar(w
@@ -2542,11 +2542,60 @@
 indow);%0A
+    if (sidebar) %7B%0A        sidebar.show();%0A    %7D%0A
 %7D);

Stop instanciating useless extra Sidebar objets. Since there is already some window tracking code in sidebar.js, we don't need to instanciate it multiple times.