(function() {
  var Cu = Components.utils;
  var Ci = Components.interfaces;
  var Cc = Components.classes;

  var jsm = { };
  Cu["import"]("resource://livemargins/utils.jsm", jsm);

  var _progListener = new jsm.utils.browserLoadListener(function() {
    document.getElementById("app-icon").style.display = "none";
    document.getElementById("loading-icon").style.display = "block";
  }, function() {
    document.getElementById("loading-icon").style.display = "none";
    document.getElementById("app-icon").style.display = "block";
  }, function() {
    document.getElementById("loading-icon").style.display = "none";
    document.getElementById("app-icon").style.display = "block";
  });

  function showBookmarkTips() {
    // Only show popup when webapp is not bookmarked.
    if (widget.args.bookmark == 1 ||
      !jsm.utils.getPref("showBookmarkTip")) {
      return;
    }

    window.setTimeout(function() {
      var popup = document.getElementById("bookmark-popup");
      popup.openPopup(document.getElementById("bookmark-button"),
                      "after_start", 10, 0, false, false);
      jsm.utils.setPref("showBookmarkTip", false);

      // fade out
      window.setTimeout(function() {
        var start = 10;
        var _interval = window.setInterval(function() {
          if (0 == start) {
            popup.hidePopup();
            window.clearInterval(_interval);
            return;
          }
          start--;
          popup.style.opacity = 0.1 * start + "";
        }, 100);
      }, 5000);
    }, 1000);
  }

  let playerBrowser = document.getElementById("player_browser");
  window.addEventListener("load", function() {
    let flag = Ci.nsIWebProgress.NOTIFY_ALL | Ci.nsIWebProgress.NOTIFY_STATE_ALL;
    playerBrowser.addProgressListener(_progListener, flag);

    showBookmarkTips();
  }, false)

  window.addEventListener("unload", function() {
    let flag = Ci.nsIWebProgress.NOTIFY_ALL | Ci.nsIWebProgress.NOTIFY_STATE_ALL;
    playerBrowser.removeProgressListener(_progListener, flag);
  }, false);

})();

