if(typeof window.econda == 'undefined') {
    window.econda = {};
}
var econda = window.econda;

/**
 * A proxy is responsible for transfering data to the econda tracking server.
 * Use Direct proxy to send all request immediately. Currently, this is the only supported
 * proxy.
 * 
 * You can configure the proxy manually:
 * 
 *     // at the beginning of your page/app startup
 *     e4.proxy.configure({
 *         type: "direct",
 *         keepalive: 30
 *     });
 * 
 * If there's no manual configuration, we'll create a default instance with default settings.
 * 
 * @class econda.proxy
 */
econda.proxy = {
    /**
     * Timestamp when page was loaded
     * @static
     * @private
     * @property {Date} __pageInitDate
     */
    __pageInitDate: new Date(),
    
    /**
     * Proxy instance
     * @property {econda.proxy.Direct} __instance
     * @private
     */
    __instance: null,
        
    /**
     * Get singleton instance of current proxy. You should not use this method directly.
     * @method
     */
    getInstance: function()
    {
        if(this.__instance == null) {
            this.createDefaultInstance();
        }
        return this.__instance;
    },
    
    /**
     * Set proxy configuration
     * @method
     * @param cfg
     */
    configure: function(cfg)
    {
        if(typeof cfg.type != 'string') {
            econda.debug.error("Error in proxy.configure: You must set a proxy type.");
            return;
        }
        var type = cfg.type.toLowerCase();
        var proxy = null;
        
        switch(type) {
        case "direct":
            proxy = new econda.proxy.Direct(cfg);
            break;
        default:
            econda.debug.error("To proxy found for type: " + type);
        }
        this.__instance = proxy;
    },
    
    /**
     * Used to create a default instance if we get no user defined configuration
     * @method
     * @private
     */
    createDefaultInstance: function()
    {
        this.configure({
            type: "direct"
        });
    }
};