
var FunctionInvoker = new Class.create(MapleTools,{

		initialize : function()
		{
		this.remoteURL = null;
		this.callbackData = new Object();
		},
	setURL:function(url) { this.remoteURL = url;},
	callFunction : function(fcnInvocation,callbackFunction)
		{
			var p = new Object();
			if(!fcnInvocation['invocationId'])
			{
				var now = new Date();
				fcnInvocation['invocationId'] = "x" + fcnInvocation['functionName'] + now.getTime();
			}
			this.callbackData[fcnInvocation['invocationId']] = {fcn:fcnInvocation,cb:callbackFunction};
			p["fcnInvokeData"] = Object.toJSON(fcnInvocation);
			var params = Object.toQueryString(p);
			this.invokeFunction(this.remoteURL,params,"onObjectUpdateHandler");
		},
		onObjectUpdateHandler : function(updatedObjMap)
		{
			var fcnRet = updatedObjMap['fcnInvokeResult'];
			if(fcnRet)
			{
				var cback = this.callbackData[fcnRet['invocationId']];
				if(cback)
				{
					this.callbackData[fcnRet['invocationId']] = null;
					if(cback['cb'])
					{
						cback.cb(fcnRet);
					}
				}
			}
		},
		invokeFunction: function(url,params,onresultfcnName)
    	{
    	    	 new Ajax.Request(url, {
  				method:'post',
  				parameters: params, 
  				onSuccess: function(transport){
  				
  					var json
  					if( transport.responseText &&  transport.responseText.isJSON())
  					{
     				 	json = transport.responseText.evalJSON();
     				 
	     				for(var p in json)
	     				{
	     					if(json[p] && json[p].toString().isJSON())
	     					{
	     						json[p] = json[p].evalJSON();
	     					}
	     					if(json[p][KMSObject.JSON_KMSOBJECT_MARKER])
	     					{
	     					json[p] = new KMSObject(json[p]);
	     					}
	     						
	     				}
	     			}
	     			else
	     			{
	     				json = transport.responseText;
	     			}
	     					
     				if(transport.request.options.handlerObj)
     				{
      					var o = transport.request.options.handlerObj;
      					o[transport.request.options.handlerFcn](json);
         				}
   				},
   				onFailure:function(transport) {
   					if(transport.request.options.handlerObj)
     				{
   						 transport.request.options.handlerObj.onErrorHandler(transport.status + " " + transport.statusText);
   					}
   					else
   					{
   						windows.alert("ERROR: " + transport.status + " " + transport.statusText);
   					}
   				},
   				onException:function(me,ex)
   				{
   					if(me && me.options.handlerObj)
   					{
    						me.options.handlerObj.onExceptionHandler(ex);
   					}
   					else
   					{
   						window.alert("Exception: " + ex);
   					}
   				},
   				handlerFcn:onresultfcnName,
   				handlerObj:this
			}
			);
		
	   	},
	   	onExceptionHandler : function (ex)
	   	{
	   		if(this.onException)
	   		{
	   			this.onException(ex);
	   		}
	   		else
	   		{
	   			window.alert("Exception in invoker: " + ex);
	   		}
	   	},
	   	onErrorHandler : function(msg)
	   	{
	   		if(this.onError){this.onError(msg);} else{ this.errorMessage(msg);}
	   	},
	   	errorMessage : function(msg)
	   	{
	   		if(window['console']){
	   			window['console'].error(msg);}else if($("errorConsole")){$("errorConsole").insert(msg);	}
	   			
	   	}
	   	

}

);