const FLOCK_ERROR_CID = Components.ID('{2DB3FC08-E5F8-11DA-8ACF-C9DED1573834}');
const FLOCK_ERROR_CONTRACTID = '@flock.com/error;1';
const FLOCK_ERROR_IID = Components.interfaces.flockIError;

const STRING_BUNDLE_CONTRACTID = '@mozilla.org/intl/stringbundle;1';
const STRING_BUNDLE_IID = Components.interfaces.nsIStringBundleService;
const STRING_BUNDLE_URL = 'chrome://flock/locale/common/flockErrors.properties';


function flockError() {
}

flockError.prototype= {
    errorCode: 0,
    serviceErrorCode: "",
    serviceErrorString: "",
    QueryInterface: function(iid) {
        if (!iid.equals(Components.interfaces.nsISupports) &&
            !iid.equals(FLOCK_ERROR_IID))
            throw Components.results.NS_ERROR_NO_INTERFACE;
        return this;
    }
};

//flockError.prototype.mStringBundle = function 
flockError.prototype.__defineGetter__('errorString', function () { return this.lookUp(this.errorCode) });


/////////////////////////////////////////////////////////////////////////////////////////////////
// internal methods
/////////////////////////////////////////////////////////////////////////////////////////////////

flockError.prototype.lookUp = function(aErrorCode) {
    var msg;
    try {
    this.debug('aErrorcode is ' + aErrorCode);
        //msg = this.mStringBundle.getString(aErrorCode);
        msg = this.mStringBundle.GetStringFromName('flock.service.errorCode_' + aErrorCode);
        this.debug(msg);
    } catch (ex) {
        this.debug(ex);
    }
    
    return msg;
};

flockError.prototype.debug = function(aMsg) {
    debug('+++++FlockErrorSvc ' + aMsg + '\n');
}

flockError.prototype.mStringBundle = Components.classes[STRING_BUNDLE_CONTRACTID].
           getService(STRING_BUNDLE_IID).createBundle(STRING_BUNDLE_URL);

///////////////////////////////////////////////////////////////////////////////////////////////
// Factory Definitions
/////////////////////////////////////////////////////////////////////////////////////////////
var flockErrorModule = {
    registerSelf: function(compMgr, fileSpec, location, type) {
        compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
        compMgr.registerFactoryLocation(FLOCK_ERROR_CID, 
                                        "flockError JS component", 
                                        FLOCK_ERROR_CONTRACTID, 
                                        fileSpec, 
                                        location,
                                        type);
    },

    getClassObject: function(compMgr, cid, iid) {
        if (!cid.equals(FLOCK_ERROR_CID))
            throw Components.results.NS_ERROR_NO_INTERFACE;

        if (!iid.equals(Components.interfaces.nsIFactory))
            throw Components.results.NS_ERROR_NOT_IMPLEMENTED;

        return flockErrorFactory;
    },

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

var flockErrorFactory = {
    createInstance: function(outer, iid) {
        if (outer != null)
            throw Components.results.NS_ERROR_NO_AGGREGATION;
    
        if (!iid.equals(FLOCK_ERROR_IID) &&
            !iid.equals(Components.interfaces.nsISupports))
            throw Components.results.NS_ERROR_INVALID_ARG;

        return new flockError();
    }
}

/* module initialisation */
function NSGetModule(comMgr, fileSpec) { return flockErrorModule; }

