Merge "Add listeners for plugin management on first established widget and remove listeners when no widget is available any longer."
diff --git a/dev/js/src/plugin/server.js b/dev/js/src/plugin/server.js
index 82a4193..b0d8f52 100644
--- a/dev/js/src/plugin/server.js
+++ b/dev/js/src/plugin/server.js
@@ -38,27 +38,9 @@
     },
 
     /*
-     * Initialize the plugin manager by establishing
-     * the global 'message' hook.
+     * Initialize the plugin manager
      */
     _init : function () {
-
-      // TODO:
-      //   It is better to establish the listener
-      //   only in case there is a widget
-
-      var that = this;
-      this._listener = this._receiveMsg.bind(that);
-      window.addEventListener("message", this._listener);
-
-      // Every second increase the limits of all registered widgets
-      var myTimer = setInterval(function () {
-        for (var i in limits) {
-          if (limits[i]++ >= maxMessages) {
-            limits[i] = maxMessages;
-          }
-        }
-      }, 1000);
       return this;
     },
 
@@ -67,6 +49,25 @@
      */
     addWidget : function (element, src) {
 
+      // Is it the first widget?
+      if (!this._listener) {
+
+        /*
+         * Establish the global 'message' hook.
+         */
+        this._listener = this._receiveMsg.bind(this);
+        window.addEventListener("message", this._listener);
+        
+        // Every second increase the limits of all registered widgets
+        this._timer = window.setInterval(function () {
+          for (var i in limits) {
+            if (limits[i]++ >= maxMessages) {
+              limits[i] = maxMessages;
+            }
+          }
+        }, 1000);
+      };
+
       // Create a unique random ID per widget
       var id = 'id-' + this._randomID();
 
@@ -118,9 +119,7 @@
 
         // Kill widget
         KorAP.log(0, 'Suspicious action by widget', widget.src);
-        widget.shutdown();
-        delete limits[id];
-        delete widgets[id];
+        this.closeWidget(widget);
         return;
       };
 
@@ -138,16 +137,36 @@
       //   Close
     },
 
+    // Close the widget
+    closeWidget : function (widget) {
+      delete limits[widget.id];
+      delete widgets[widget.id];
+      widget.shutdown();
+
+      // Remove listeners in case no widget
+      // is available any longer
+      if (Object.keys(limits).length == 0)
+        this._removeListener();
+    },
+
     // Get a random identifier
     _randomID : function () {
       return randomID(20);
     },
 
+    // Remove the listener
+    _removeListener : function () {
+      window.clearInterval(this._timer);
+      this._timer = undefined;
+      window.removeEventListener("message", this._listener);
+      this._listener = undefined;
+    },
+
     // Destructor, just for testing scenarios
     destroy : function () {
       limits = {};
       widgets = {};
-      window.removeEventListener("message", this._listener);
+      this._removeListener();
     }
   }
 });