/*/
/ ChatZilla - Clutter
/ http://rdmsoft.com/
/*/

const nsIWindowMediator = Components.interfaces.nsIWindowMediator;
const nsIDOMWindowInternal = Components.interfaces.nsIDOMWindowInternal;

plugin.id = "clutter";

plugin.init =
function init(glob)
{
    plugin.major = 0;
    plugin.minor = 3;
    plugin.version = plugin.major + "." + plugin.minor;
    plugin.description = "Adds stuff to the tools menu."
}

plugin.disable =
function disable()
{

    uncmdary = [
        {name: 'cmd-jsconsole'},
        {name: 'cmd-aboutconfig'},
        {name: 'cmd-thememanager'},
        {name: 'cmd-extensionmanager'},
        {name: 'cmd-proxysetup'},
        {name: 'cmd-securitysetup'}
    ]

    client.commandManager.removeCommands(uncmdary);

    display(plugin.id+' - plugin disabled');

    return true;
}

plugin.enable =
function enable()
{
    var cmdary =
    [
     ["cmd-jsconsole", plugin.showJSConsole, 0, ""],
     ["cmd-aboutconfig", plugin.showAboutConfig, 0, ""],
     ["cmd-thememanager", plugin.showThemes, 0, ""],
     ["cmd-extensionmanager", plugin.showExtensions, 0, ""],
     ["cmd-proxysetup", plugin.showProxySetup, 0, ""],
     ["cmd-securitysetup", plugin.showSecuritySetup, 0, ""]
    ];
    client.commandManager.defineCommands(cmdary);

    var menuItems =
        [
         ["cmd-jsconsole", {label: "JavaScript Console..."}],
         ["cmd-aboutconfig", {label: "Advanced Configuration..."}],
         ["cmd-thememanager", {label: "Themes..."}],
         ["cmd-extensionmanager", {label: "Extensions..."}],
         ["cmd-proxysetup", {label: "Proxy Settings...", enabledif: false}],
         ["cmd-securitysetup", {label: "Security...", enabledif: false}]
        ];

    if("mainmenu:tools" in client.menuManager.menuSpecs)
        client.menuManager.appendMenuItems("mainmenu:tools", menuItems);
    else if("mainmenu:chatzilla" in client.menuManager.menuSpecs)
        client.menuManager.appendMenuItems("mainmenu:chatzilla", menuItems);
    else
    {
        display(plugin.id + " - Root menu not found.", MT_ERROR);
        return false;
    }

    display(plugin.id+' - plugin enabled');

    return true;
}

plugin.showJSConsole =
function showJSConsole()
{
    plugin.toOpenWindowByType("global:console",
                              "chrome://global/content/console.xul");
}

plugin.showAboutConfig =
function showAboutConfig()
{
    openDialog("chrome://global/content/config.xul");
}

plugin.showThemes =
function showThemes()
{
    plugin.goOpenExtensions("themes");
}
plugin.showExtensions =
function showExtensions()
{
    plugin.goOpenExtensions("extensions");
}
plugin.showProxySetup =
function showProxySetup()
{

}
plugin.showSecuritySetup =
function showSecuritySetup()
{

}

plugin.toOpenWindowByType =
function toOpenWindowByType(inType, uri)
{
    var wm = Components.classes['@mozilla.org/appshell/window-mediator;1'];
    wm = wm.getService(nsIWindowMediator);
    var topWindow = wm.getMostRecentWindow(inType);

    if (topWindow)
        topWindow.focus();
    else
        window.open(uri, "_blank",
                    "chrome,extrachrome,menubar,resizable," +
                    "scrollbars,status,toolbar");
}

plugin.goOpenExtensions =
function goOpenExtensions(aOpenMode)
{
    var EMTYPE = "Extension:Manager";

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

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