Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

function thunderProtocol(){}

thunderProtocol.prototype = {
	classDescription: "Thunder Protocol",
	classID         : Components.ID("04836508-ceb3-474f-98bc-fbffc53a027d"),
	contractID      : "@mozilla.org/network/protocol;1?name=thunder",
	QueryInterface  : XPCOMUtils.generateQI([Components.interfaces.nsIProtocolHandler]),

	scheme       : "thunder",
	defaultPort  : 0,
	protocolFlags: Components.interfaces.nsIProtocolHandler.URI_NORELATIVE | 
									Components.interfaces.nsIProtocolHandler.URI_NOAUTH |
									Components.interfaces.nsIProtocolHandler.URI_LOADABLE_BY_ANYONE | 
									Components.interfaces.nsIProtocolHandler.URI_INHERITS_SECURITY_CONTEXT |
									Components.interfaces.nsIProtocolHandler.URI_DOES_NOT_RETURN_DATA,

	allowPort: function(port, scheme){
		return false;
	},

	newURI: function(spec, charset, baseURI){
  	var uri = Components.classes["@mozilla.org/network/simple-uri;1"].createInstance(Components.interfaces.nsIURI);
    uri.spec = spec;
    return uri; 
	},

	newChannel: function(aURI){
		var cs = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
		//cs.logStringMessage(aURI.spec);
		var thunderComponent;
		try{
			//referrer,1,uri,fname,cookie,dummy
			var param = ["", "1", aURI.spec,
								"", "", "dummy" ].join("#@$@#");
			thunderComponent = Components.classes["@thunder.com/thundercomponent;1"].createInstance()
																.QueryInterface(Components.interfaces.IThunderComponent);
			thunderComponent.CallThunder(param, "1");
			//cs.logStringMessage(param);
		}catch(err){
			cs.logStringMessage(err);
		}
		
		function dummyChannel(uri){}
		dummyChannel.prototype = {
			contentLength: 0,
			owner: null,
			securityInfo: null,
			notificationCallbacks: null,
			loadFlags: 0,
			loadGroup: null,
			name: null,
			status: Components.results.NS_OK,

			open: function() {
				throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
			},
			asyncOpen: function(listener, context) {
				throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
			}
		}
		return new dummyChannel(aURI);
		//var ios = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
		//return ios.newChannel("javascript:alert('hello')", null, null);
	}
}

function NSGetModule(compMgr, fileSpec){
	return XPCOMUtils.generateModule([thunderProtocol]);
}