
/*********************************************************************************
 * 
 * 							EDIT AJAX LAUNCHER
 * 
 * @author Rhonda Kammer
 *********************************************************************************
 * This class is meant to function as a general ajax request launcher or in plain
 * text act as a bridge between javascript tools and emporis php logic. This class
 * eliminates the need to reformulate an ajax call inside the edit tool.
 * 
 * The ajax launcher sends the information to the proper requesting javascript method.
 * It knows which method because this is brought in through parameter method. It also
 * knows which tool class is instantiated because in the future all tools will have
 * the handle tool_handle when they are instantiated by the tool factory.
 *
 *
 * @param string url should be 
 * @param array input_data which the edit_ajax should share with the edit_ajax_processor
 * @param string tool the name of the tool making the request
 * @param string method the name of the method making the call. This will most likely
 * 					be from an edit tool. Any method taken in this parameter must be 
 * 					public. See edit_callout_class.prototype.make_edit_callout in edit_callout
 *
 ********************************************************************************/

function ajax_launcher( url, input_data, tool, method ){
	
	
	//if( method == "get_object_id" ) alert( "ajax launcher called: tool: " + tool + " method: " + method + " url: " + url );
		
	var _ajax_url 			= url;
	var _input_data			= input_data;
	var _response_data 		= "";
	var _tool_name 			= tool;
	var _tool_handle 		= "";
	var _calling_method		= method;
	var _function_name 		= "";
	
	//TODO transfer functionality to method below after tool_handle rename
	//var funcname = "tool_handle." + _calling_method + "(response_data)";
	//var funcname2 = "tool_handle." + _calling_method;
	
		
	/********************************
	 * 
	 *   ajax_launcher constructor
	 * 
	 *******************************/	    		
	
	set_function_name();
	
	
	var _answer_function = new Function( _function_name);
	 
	 //var answer_func = new Function(funcname);
	 do_ajax_call();
	 
	 
	//alert( "ajax launcher called: " + _function_name + " answer func: " + _answer_function );
	 
	/*********************************
	 * 
	 *  private function definitions
	 * 
	 ********************************/

	function do_ajax_call( ){ 
		
		$.ajax({			
		   type: "POST",
		   url: _ajax_url,
		   data: _input_data,
		   dataType: "json",
		   success: function( response_data ){
			
						//alert( "Ajax launcher: " + response_data );			
						
						if( typeof _answer_function == "function" && typeof ec == "object"){
													
							eval(_function_name);
						}			 				 					     				     		
		   			}		
		});		
	}
	
	
	function set_function_name(){
		
		// TODO calling tool can append its own handle to method name
		if( _tool_name == "edit_callout" ){			
			_function_name = "ec." + _calling_method + "(response_data)";			
		}
		if( _tool_name == "edit_search" ){			
			_function_name = "es." + _calling_method + "(response_data)";			
		}
		if( _tool_name == "edit_pinpoint" ){			
			_function_name = _calling_method + "(response_data)";
		}
		if(_tool_name == "edit_translation"){
			_function_name = _calling_method + "(response_data)";
		}
		if(_tool_name == "edit_dropdown"){
			_function_name = _calling_method + "(response_data)";
		}
		if(_tool_name == "edit_value"){
			_function_name = _calling_method + "(response_data)";
		}
		return true;
	}
	
	
}//cl al




















