function gRegisterGlobalCommands() {
  var commandTable      = gTagger.getCommandTable();

  commandTable.registerCommand('cmd_New', gNewCommand);
  commandTable.registerCommand('cmd_Exit', gExitCommand);
  commandTable.registerCommand('cmd_OpenDesc', gOpenDescFile);
  commandTable.registerCommand('cmd_Close', gCloseCommand);
  commandTable.registerCommand('cmd_Save', gSaveCommand);
  commandTable.registerCommand('cmd_SaveAs', gSaveAsCommand);

  commandTable.registerCommand('cmd_ShowJSConsole', gShowJSConsoleCommand);
  commandTable.registerCommand('cmd_ShowOptions', gShowOptionsCommand);
  commandTable.registerCommand('cmd_ShowAddonsManager', gShowAddonsManagerCommand);

  commandTable.registerCommand('cmd_ShowToolbarsCustomizer', gShowToolbarsCustomizerCommand);

  commandTable.registerCommand('cmd_Restart', gRestartCommand);
  commandTable.registerCommand('cmd_ReloadChrome', gReloadChromeCommand);
}

var gReloadChromeCommand = {
  isCommandEnabled: function(aCommand) {
    return true;
  },

  doCommand : function(aCommand) {
		Components.classes["@mozilla.org/chrome/chrome-registry;1"]
			.getService(Components.interfaces.nsIXULChromeRegistry)
			.reloadChrome();
	}
};

var gRestartCommand = {
  isCommandEnabled: function(aCommand) {
    return true;
  },

  doCommand : function(aCommand) {
		if(goCloseWindow()) {
			var appStartup = Components.interfaces.nsIAppStartup;
			Components.classes["@mozilla.org/toolkit/app-startup;1"]
				.getService(appStartup)
				.quit(appStartup.eRestart | appStartup.eAttemptQuit);
		}
	}
};

var gExitCommand = {

  isCommandEnabled: function(aCommand) {
    return true;
  },

  doCommand : function(aCommand) {
    if(goCloseWindow()) 
			Components.classes['@mozilla.org/toolkit/app-startup;1']
				.getService(Components.interfaces.nsIAppStartup)
				.quit(Components.interfaces.nsIAppStartup.eAttemptQuit);
  }
};

var gOpenDescFile = {
  isCommandEnabled: function(aCommand) { 
    return !gTagger.ready;
  },
  doCommand : function(aCommand) {
   var file = goOpenFile(
       gTagger.getString("application.openfile"),
       0, "coldeeditor.filepicker.workdir",
			 [{name:"XML file", ext:"*.xml"}, {name:"all", ext:"*.*"}]);
   if(file) {
     gTagger.fileOpened(file);
   }
  }
};

var gNewCommand = {
  isCommandEnabled: function(aCommand) { 
    return !gTagger.ready;
  },
  
  doCommand : function(aCommand) {
		var file = {};
		window.openDialog('chrome://just/content/new.xul', 'Nouvelle description','chrome,modal,centerscreen', gTagger);
  }
  
};

var gCloseCommand = {
  isCommandEnabled: function(aCommand) { 
    return true;
  },
  
  doCommand : function(aCommand) {
		throw("Not implemented");
  }
  
};

var gSaveCommand = {
  isCommandEnabled: function(aCommand) { 
    return gTagger.ready;
  },
  
  doCommand : function(aCommand) {
		gTagger.save();
  }
  
};

var gSaveAsCommand = {
  isCommandEnabled: function(aCommand) { 
    return false;
  },
  
  doCommand : function(aCommand) {
		throw("Not implemented");
  }
};

var gShowJSConsoleCommand = {
  isCommandEnabled: function(aCommand) { 
    return true;
  },
  
  doCommand : function(aCommand) {
    var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService();
    var windowManagerInterface = windowManager.QueryInterface(Components.interfaces.nsIWindowMediator);
    var topWindow = windowManagerInterface.getMostRecentWindow("global:console");
    
    if (topWindow)
      topWindow.focus();
    else
      window.open("chrome://global/content/console.xul", "_blank", "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar");

  }
};

var gShowOptionsCommand = {
  isCommandEnabled: function(aCommand) { 
    return false;
  },
  
  doCommand : function(aCommand) {
		throw("Not implemented");
  }
};

var gShowAddonsManagerCommand = {
  isCommandEnabled: function(aCommand) { 
    return true;
  },
  
  doCommand : function(aCommand) {
		const EMTYPE = "Extension:Manager";

		var aOpenMode = "extensions"; 
		var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
			.getService(Components.interfaces.nsIWindowMediator);
		var needToOpen = true;
		var windowType = EMTYPE + "-" + aOpenMode;
		var windows = wm.getEnumerator(windowType);
		while (windows.hasMoreElements()) {
			var theEM = windows.getNext().QueryInterface(Components.interfaces.nsIDOMWindowInternal);
			if (theEM.document.documentElement.getAttribute("windowtype") == windowType) {
				theEM.focus();
				needToOpen = false;
				break;
			}
		}

		if (needToOpen) {
			const EMURL = "chrome://mozapps/content/extensions/extensions.xul?type=" + aOpenMode;
			const EMFEATURES = "chrome,dialog=no,resizable=yes";
			window.openDialog(EMURL, "", EMFEATURES);
		}
  }
};

var gShowToolbarsCustomizerCommand = {
  isCommandEnabled: function(aCommand) { 
    return true;
  },
  
  doCommand : function(aCommand) {
    var toolbox = document.getElementById("mainToolbox");
		var menubar = document.getElementById("mainMenubar");

		if(!("customizeDone" in toolbox))
			toolbox.customizeDone = function () {
				var menubar = document.getElementById("mainMenubar");
				for (var i = 0; i < menubar.childNodes.length; ++i)
					menubar.childNodes[i].setAttribute("disabled", false);

				var cmd = document.getElementById("cmd_ShowToolbarsCustomizer");
				cmd.removeAttribute("disabled");
			};

		for (var i = 0; i < menubar.childNodes.length; ++i)
			menubar.childNodes[i].setAttribute("disabled", true);

		var cmd = document.getElementById("cmd_ShowToolbarsCustomizer");
		cmd.setAttribute("disabled", "true");

		window.openDialog("chrome://global/content/customizeToolbar.xul", "CustomizeToolbar",
				"chrome,all,dependent", toolbox);
  }
};

