if(typeof fly == "undefined")
{
	var fly = new Object();
};

fly.CFCProxyJSON = function()
{
};

fly.CFCProxyJSON.prototype = 
{
	init: function(id_str, callback_obj, context_str) 
	{
		this._initCallback = callback_obj;
		
		if (context_str)
		{
			this._context_str = context_str;
		} else
		{  
			this._context_str = "/";
		}

		var newCallback_obj = 
		{
			success: this._settingsLoadResult,
			failure: this._settingsLoadError,
			scope: this
		};
		YAHOO.util.Connect.asyncRequest("POST", this._context_str + "LocalCFCProxyJSON.cfc", newCallback_obj, "method=callMethodJSON&cfcPath=fly.application.Settings&methodToCall=getSettings&methodArguments={sessionID: \"" + id_str + "\"}&username=&password=");
	},
	
	_settingsLoadResult: function(o)
	{
		if (this._settingsLoaded(o))
		{
			if (this._initCallback.scope)
			{
				this._initCallback.success.call(this._initCallback.scope, o);
			} else
			{
				this._initCallback.success(o);
			}
		};
	},
	
	_settingsLoadFailure: function(o)
	{
		if (this._initCallback.scope)
		{
			this._initCallback.failure.call(this._initCallback.scope, o);
		} else
		{
			this._initCallback.failure(o);
		}	
	},

	_settingsLoaded: function(o)
	{
		var validSettings_bool = false;
		//var result_obj = eval("(" + o.responseText + ")");
		var result_obj;
        // Use the JSON Utility to parse the data returned from the server
        try {
            result_obj = YAHOO.lang.JSON.parse(o.responseText);
        }
        catch (e) {
            alert("CFCProxyJSON Parse failed '" + e.message + "' on \n'" + o.responseText + "'");
            return;
        }
		
		if (result_obj.error)
		{
			_settingsLoadFailure(o);
		} else
		{
			this._settings_obj = result_obj.data;
			validSettings_bool = true;
		}
		
		return validSettings_bool;
	},
		
	getService: function(service_str)
	{
		var service = new fly.CFCProxyJSONService(service_str, this._settings_obj.proxy.username, this._settings_obj.proxy.password, this._context_str);
		
		return service;	
	}
};

fly.CFCProxyJSONService = function(service_str, username_str, password_str, context_str)
{
	this._service_str = service_str;
	this._username_str = username_str;
	this._password_str = password_str;
	
	if (context_str)
	{
		this._context_str = context_str;
	} else
	{
		this._context_str = "/";
	}
	
	this._arguments_str = "method=callMethodJSON";
	this._arguments_str += "&username=" + this._username_str;
	this._arguments_str += "&password=" + this._password_str;
	this._arguments_str += "&cfcPath=" + this._service_str;
};


fly.CFCProxyJSONService.prototype = 
{
	callMethod: function(method_str, callback, arguments_obj, formatResult_bool)
	{
		var arguments_str = this.getMethodRequestString(method_str, arguments_obj, formatResult_bool);
		YAHOO.util.Connect.asyncRequest("POST", this._context_str + "LocalCFCProxyJSON.cfc", callback, arguments_str);
	},
	getMethodRequestString: function(method_str, arguments_obj, formatResult_bool)
	{
		if (!formatResult_bool)
		{
			formatResult_bool = true;
		}
	
		var arguments_str = this._arguments_str;
		arguments_str += "&methodToCall=" + method_str;
		if (arguments_obj)
		{
			arguments_str += "&methodArguments=" + encodeURI(YAHOO.lang.JSON.stringify(arguments_obj));
		};
		
		return arguments_str;
	}
};