// License: http://creativecommons.org/licenses/by-nc-sa/2.0/

var gSyncMarksService = {
  syncMarks:null,

////////////////////////////////////////////////////////////////////////////////

  // static call to OnLoad event
  onload:function() { gSyncMarksService.onLoad(); },

////////////////////////////////////////////////////////////////////////////////

  // static call to OnClose event
  onclose:function() { gSyncMarksService.onClose(); },

////////////////////////////////////////////////////////////////////////////////

  // method called when extension is loaded
  onLoad:function() {
    window.removeEventListener("load", gSyncMarksService.onload, true);

    this.syncMarks = new SyncMarks();
    if (this.syncMarks.settings.syncOnLoad) {
      this.syncMarks.sync();
    }
  },

////////////////////////////////////////////////////////////////////////////////

  // method called when extension is unloaded
  onClose:function() {
    if (this.syncMarks.settings.syncOnExit) {
      try {
        var wm = Components.classes['@mozilla.org/appshell/window-mediator;1']
                 .getService(Components.interfaces.nsIWindowMediator);

        var windows = wm.getZOrderDOMWindowEnumerator('navigator:browser', true);

        // get the first open browser
        if (windows.hasMoreElements()) {
          var browser = browsers.getNext();
        }

        // if there are more, we are not the last one...
        if (browsers.hasMoreElements()) {
          return;
        }
      } catch(e) { return; }

      this.syncMarks.sync();
	    window.setCursor("auto");
    }
  },

////////////////////////////////////////////////////////////////////////////////
};

//Event Listeners
window.addEventListener("load", gSyncMarksService.onload, false);
window.addEventListener("close", gSyncMarksService.onclose, false);
