/****************************************************************************
* SKCD  > skPopupManager                                                    *
*---------------------------------------------------------------------------*
* skPopupManager's goal is to register all secondary popups currently       *
* opened in order to access them easily.                                    *
*---------------------------------------------------------------------------*
* (c) IDM. 2003                                                             *
* Project  : sk                                                             *
* Language : javascript                                                     *
* Created  : MikaelLebrun, 18/02/2003                                       *
****************************************************************************/

function skPopupManager() { } //skPopupManager object

skPopupManager.prototype = {

    QueryInterface:function(iid) {
        if (!iid.equals(Components.interfaces.skIPopupManager)
            &&!iid.equals(Components.interfaces.nsISupports)) {
            throw Components.results.NS_ERROR_NO_INTERFACE;
        }
        return this;
    },
    
    /*********************************************************************************************
    FUNCTION : Init()
    This function initialize the skPopupManager component
    ****INPUT: a window object [pxiWindow - the main window in the application, from which all all
                                popups will be managed]
    ****OUPUT: NOTHING
    ********************************************************************************************/
    Init:function(pxiWindow) {
        this.window = pxiWindow;
        this.aManager = new Array(); //Create the manager array
        //this.prompt(this.window, "Test Init", "Oki !");
    },

    /*********************************************************************************************
    FUNCTION : OpenPopup()
    This function open a popup and register it in the popup manager list
    ****INPUT: 1/ a string [psUrl - the location to load in the new popup]
               2/ a string [psProperties - the window properties, to define the popup appearance]
               3/ a string [piType - the popup type]
               4/ a string [psId - the popup id]
    ****OUPUT: a nsIDOMWindowInternal XPCOM object [the new popup]
    ********************************************************************************************/
    OpenPopup:function(psUrl, psProperties, piType, psId, pxiWindow) {
        if(!pxiWindow){
            pxiWindow = this.window;
        }
        var xiDomWindow = pxiWindow.open(psUrl, "_blank", psProperties);
        var iIndex = this.aManager.length;
        this.aManager[iIndex] = new Array();
        this.aManager[iIndex]["type"] = piType;
        this.aManager[iIndex]["window"] = xiDomWindow;
        this.aManager[iIndex]["id"] = psId;
        return xiDomWindow;
        //this.prompt(this.window, "Test Open", "Oki ! : " + this.aManager.length);
    },

    /*********************************************************************************************
    FUNCTION : ClosePopup()
    This function close the popup which position in the popup manager list is passed in argument
    ****INPUT: an integer [piIndex - the popup position in the popup manager list]
    ****OUPUT: NOTHING
    ********************************************************************************************/
    ClosePopup:function(piIndex) {
        if (this.aManager[piIndex]) {
            this.ClosePopupById(this.aManager[piIndex]["id"]);
        }
    },

    /*********************************************************************************************
    FUNCTION : ClosePopupById()
    This function close the popup which id (the parameter given in the popup manager list) is passed 
    in argument
    ****INPUT: a string [psId - the id given to the popup in the popup manager list]
    ****OUPUT: NOTHING
    ********************************************************************************************/
    ClosePopupById:function(psId) {
        var newManager = new Array();
        var iMIdx;
        for (var i=0; i<this.aManager.length; i++) {
            if (this.aManager[i]["id"] == psId) {
                this.aManager[i]["window"].close();
            } else {
                //Build a new array containing only popups non-closed
                iMIdx = newManager.length;
                newManager[iMIdx] = new Array();
                newManager[iMIdx]["type"] = this.aManager[i]["type"];
                newManager[iMIdx]["window"] = this.aManager[i]["window"];
                newManager[iMIdx]["id"] = this.aManager[i]["id"];
            }
        }
        this.aManager = newManager;
        //this.prompt(this.window, "Test ClosePopupById", this.aManager.length);
    },

    /*********************************************************************************************
    FUNCTION : ClosePopupByType()
    This function close the set of popups whose type (the parameter given in the popup manager list)
    is passed in argument
    ****INPUT: an integer [piType - the type given to the popup in the popup manager list]
    ****OUPUT: NOTHING
    ********************************************************************************************/
    ClosePopupByType:function(piType) {
        var newManager = new Array();
        var iMIdx;
        for (var i=0; i<this.aManager.length; i++) {
            if (this.aManager[i]["type"] == piType) {
                this.aManager[i]["window"].close();
            } else {
                //Build a new array containing only popups non-closed
                iMIdx = newManager.length;
                newManager[iMIdx] = new Array();
                newManager[iMIdx]["type"] = this.aManager[i]["type"];
                newManager[iMIdx]["window"] = this.aManager[i]["window"];
                newManager[iMIdx]["id"] = this.aManager[i]["id"];
            }
        }
        this.aManager = newManager;
        //this.prompt(this.window, "Test ClosePopup", this.aManager.length);
    },

    /*********************************************************************************************
    FUNCTION : CloseAllPopups()
    This function close all popups registered in the popup manager list
    ****INPUT: NOTHING
    ****OUPUT: NOTHING
    ********************************************************************************************/
    CloseAllPopups:function() {
        for (var i=0; i<this.aManager.length; i++) {
            this.aManager[i]["window"].close();
        }
        this.aManager = new Array();
        //this.prompt(this.window, "Test CloseAllPopups", this.aManager.length);
    },

    /*********************************************************************************************
    FUNCTION : ReturnPopup()
    This function return the popup which position in the popup manager list is passed in argument
    ****INPUT: an integer [piIndex - the position in the popup manager list]
    ****OUPUT: a nsIDOMWindowInternal XPCOM object [the desired popup]
    ********************************************************************************************/
    ReturnPopup:function(piIndex) {
        if (this.aManager[piIndex]) {
            return this.aManager[piIndex]["window"];
        }
        return null;
    },
 
    /*********************************************************************************************
    FUNCTION : ReturnPopupById()
    This function return the popup which id (the parameter given in the popup manager list) is passed 
    in argument
    ****INPUT: a string [psId - the id given to the popup in the popup manager list]
    ****OUPUT: a nsIDOMWindowInternal XPCOM object [the desired popup]
    ********************************************************************************************/
    ReturnPopupById:function(psId) {
        for (var i=0; i<this.aManager.length; i++) {
            if (this.aManager[i]["id"] == psId) {
                return this.aManager[i]["window"];
            }
        }
        return null;
    },
 
    /*********************************************************************************************
    FUNCTION : ReturnPopupByType()
    This function return a list of popups which type (the parameter given in the popup manager list)
    is passed in argument
    ****INPUT: an integer [piType - the type given to the popup in the popup manager list]
    ****OUPUT: a nsISupportsArray XPCOM object [the list of popups]
    ********************************************************************************************/
    ReturnPopupByType:function(piType) {
        var xiSupportsArray = Components.classes["@mozilla.org/supports-array;1"].createInstance().QueryInterface(Components.interfaces.nsISupportsArray);
        for (var i=0; i<this.aManager.length; i++) {
            if (this.aManager[i]["type"] == piType) {
                xiSupportsArray.AppendElement(this.aManager[i]["window"]);
                //this.prompt(this.window, "Test ReturnPopupOfType", this.aManager[i]["window"]);
            }
        }
        return xiSupportsArray;
    },
 
    /*********************************************************************************************
    FUNCTION : ReturnAllPopups()
    This function return all popups registered in the popup manager list
    ****INPUT: NOTHING
    ****OUPUT: a nsISupportsArray XPCOM object [the list of popups]
    ********************************************************************************************/
    ReturnAllPopups:function() {
        var xiSupportsArray = Components.classes["@mozilla.org/supports-array;1"].createInstance().QueryInterface(Components.interfaces.nsISupportsArray);
        for (var i=0; i<this.aManager.length; i++) {
            xiSupportsArray.AppendElement(this.aManager[i]["window"]);
            //this.prompt(this.window, "Test ReturnPopupOfType", this.aManager[i]["window"]);
        }
        return xiSupportsArray;
    },
    
    prompt:function(pxiWindow, psTitle, psText) {
        var xiPromptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].createInstance().QueryInterface(Components.interfaces.nsIPromptService);
        xiPromptService.alert(pxiWindow, psTitle, psText);
    }    
}

var skPopupManagerModule = {

    registerSelf: function (compMgr, fileSpec, location, type) {
        compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
        compMgr.registerFactoryLocation(this.myCID,
                                        "Sk PopupManager Component",
                                        this.myProgID, 
                                        fileSpec,
                                        location,
                                        type);
    },

    getClassObject: function (compMgr, cid, iid) {
        if (!cid.equals(this.myCID))
            throw Components.results.NS_ERROR_NO_INTERFACE;
        if (!iid.equals(Components.interfaces.nsIFactory))
            throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
        return this.myFactory;
    },

    myCID: Components.ID("ca1d9312-3300-4809-9b18-4e167063ffd2"),

    myProgID: "@idm.fr/sk_popup_manager;1",

    myFactory: {
        createInstance: function (outer, iid) {
            if (outer != null)
                throw Components.results.NS_ERROR_NO_AGGREGATION;
            return (new skPopupManager()).QueryInterface(iid);
        }
    },

    canUnload: function(compMgr) {
        return true;
    }
};

function NSGetModule(compMgr, fileSpec) { return skPopupManagerModule; }
