/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * Probably need some copyright here! 
 */

function nsJSAimChatRendezvous() {
    // dump("creating new nsJSAimChatRendezvous\n");
}

nsJSAimChatRendezvous.prototype = {
    // we just implement one function!
    OnBuddyIconProposalReceived : function (screenname, proposal)
    {


     inviteArgsObj = {
     	inviteProposalScreenName: screenname, 
     	inviteProposalObj: proposal,
     	invitedScreenNames: '',
     	inviteToExistingWindow: false,
     	inviteMode: 'incomingchat'
     }
    
     var windowManager = Components.classes['@mozilla.org/rdf/datasource;1?name=window-mediator'].getService();

     var windowManagerInterface = windowManager.QueryInterface( Components.interfaces.nsIWindowMediator);

     var topWindow = windowManagerInterface.getMostRecentWindow(null);

     //topWindow.focus();
     topWindow.openDialog('chrome://aim/content/IMBuddyIconKnockKnock.xul','','modal=yes,titlebar,chrome,dialog=yes, centerscreen',inviteArgsObj);
    
    },

    OnProposalReceived : function (screenname, proposal)
    {

     dump ("got a proposal from " + screenname + "\n");

     inviteArgsObj = {
     	inviteProposalScreenName: screenname, 
     	inviteProposalObj: proposal,
     	invitedScreenNames: '',
     	inviteToExistingWindow: false,
     	inviteMode: 'incomingchat'
     }
    
     var windowManager = Components.classes['@mozilla.org/rdf/datasource;1?name=window-mediator'].getService();

     var windowManagerInterface = windowManager.QueryInterface( Components.interfaces.nsIWindowMediator);

     var topWindow = windowManagerInterface.getMostRecentWindow(null);

     //topWindow.focus();
     topWindow.openDialog('chrome://aim/content/chatInviteBuddy.xul','_blank','chrome,all,dialog=no',inviteArgsObj); 
    
    },

	ChatInvite : function (roomname, language, screennamelist, msg)
	{
    // XXX hardcoding en for now, we have no way of
    // determining what this might be from the URL 
		inviteArgsObj = {
      invitedScreenNames: screennamelist, 
     	inviteToExistingRoom: roomname,
     	inviteLanguage: "en",
			inviteToExistingWindow: false,
     	inviteMode: 'outgoingchat',
			invitationmsg: msg
  }
    
		var windowManager = Components.classes['@mozilla.org/rdf/datasource;1?name=window-mediator'].getService();

		var windowManagerInterface = windowManager.QueryInterface( Components.interfaces.nsIWindowMediator);

		var topWindow = windowManagerInterface.getMostRecentWindow(null);
		topWindow.openDialog('chrome://aim/content/chatInviteBuddy.xul','_blank','chrome,all,dialog=no',inviteArgsObj); 
	},
	
	GoChat : function (roomname, exchange, instance)
	{
                var language = "en";
		chatContentArgsObj = {
     			chatRoomName : roomname,
     			chatLanguage : language,
			incomingChat : false,
			instance : instance,
			exchange : exchange,
			fromurl : true
     		}
    
		var windowManager = Components.classes['@mozilla.org/rdf/datasource;1?name=window-mediator'].getService();

		var	windowManagerInterface = windowManager.QueryInterface( Components.interfaces.nsIWindowMediator);

		var topWindow = windowManagerInterface.getMostRecentWindow(null);

		topWindow.openDialog('chrome://aim/content/chatContent.xul','','chrome,all,dialog=no',chatContentArgsObj); 
	}

}

var module = {
    registerSelf: function (compMgr, fileSpec, location, type) {
        compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentManagerObsolete);
        compMgr.registerComponentWithType(this.myCID,
        	"nsJSAimChatRendezvous",
                "@netscape.com/js/aim/chatrendezvous;1",
                fileSpec, location, true, true, 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;
    },

    canUnload: function () {
    },
    
    myCID: Components.ID("{38E07345-2DC7-11d4-BA1A-0010833594E9}"),
    
    myFactory: {
        createInstance: function (outer, iid) {
            if (outer != null)
                throw Components.results.NS_ERROR_NO_AGGREGATION;
            
            if (!(iid.equals(Components.interfaces.nsIAimChatRendezvousCallback) || iid.equals(Components.interfaces.nsISupports))) {
                throw Components.results.NS_ERROR_INVALID_ARG;
            }

            return new nsJSAimChatRendezvous();
        }
    }
};

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