var gz_pluginManager_mb = {
  name: 'Shockwave Flash',  // name of the plugin to look for
  menu: 'Flash',            // label for the checkbox

  observe: function(subject, topic, data) {
    switch (topic) {
      case 'plugins-list-updated':
        this.plugin = this.pme.pm.getPluginByName(this.name);
        setTimeout(function(self) { self.updateButton(); }, 50, this);
        break;
      default:
        break;
    }
  },

  updateButton: function() {
    if (!this.plugin)
      return;

    var hasTimer = this.pme.hasValidTimer(this.plugin);

    var style = 'font-style: ' + (hasTimer ? 'italic' : 'normal') + ';';

    if (!this.plugin.disabled)
      style += 'font-weight: bold; color: #ac5050;';

    this.button.setAttribute('style', style);

    this.button.setAttribute('checked', !this.plugin.disabled);
  },

  startup: function() {
    this.pme = gz_pluginManager;
    this.plugin = this.pme.pm.getPluginByName(this.name);

    if (!this.plugin) {
      alert('Failed to get plugin for ' + this.name);
      return;
    }

    var menubar = document.getElementById('toolbar-menubar-gzExtra');

    if (!menubar) {
      var menubar = document.getElementById('toolbar-menubar');

      var spacer = document.createElement('spacer');
      spacer.setAttribute('flex', 1);
      menubar.appendChild(spacer);
    }

    if (!menubar)
      return;

    this.button = document.createElement('checkbox');
    this.button.setAttribute('label', this.menu);
    this.button.setAttribute('filename', this.plugin.filename);
    this.button.setAttribute('oncommand', 'gz_pluginManager.toggle(this);');

    this.updateButton();

    menubar.appendChild(this.button);

    var self = this;

    this.focusedElement = null;
    this.button.addEventListener('mousedown', function(event) {
        self.focusedElement = document.commandDispatcher.focusedElement;
      }, false);
    this.button.addEventListener('mouseup', function(event) {
        if (self.focusedElement) self.focusedElement.focus();
        self.focusedElement = null;
      }, false);

    this.os = Cc["@mozilla.org/observer-service;1"]
                .getService(Ci.nsIObserverService);

    this.os.addObserver(this, "plugins-list-updated", false);

    window.addEventListener('unload', function() { self.shutdown(); }, false);
  },
  shutdown: function() {
    this.os.removeObserver(this, "plugins-list-updated");
  }
};

setTimeout(function() { gz_pluginManager_mb.startup(); }, 50);
