/****************************************************************************************************************
 * 
 * 												CLASS EDIT SEARCH (uses edit_callout_class)
 * 
 ****************************************************************************************************************
 * Instantiated by edit_callout_class in embed_search_in_callout() method.
 * conducts a search for objects of type _search_object which are basically any emporis object
 * object is input by template designer in parameter 2 of the publish constructor
 * Instantiates the emporis search class through ajax call. See edit_ajax_launcher and edit_ajax_processor
 *
 ****************************************************************************************************************
 * NOTES:
 *
 **/

function edit_search_class( ){
	
	
	/** ****************************************************************************************************** */
	/** ****************************************  private class variables ************************************ */
	
	var _script_uri 			= ec.get_script_uri();
	var _search_object  		= ec.get_edit_object();
	var _tool_purpose 			= ec.get_tool_purpose();
	var _tool_list				= ec.get_tool_list();
	var _tool_task				= ec.get_tool_task();
	var _property_name			= ec.get_property_name();
	var _sess_id				= ec.get_session_id();
	var _building_object_id		= ec.get_object_id();
	var _nav_id					= ec.get_nav_id();
	var _edit_callout_height	= ec.get_callout_height();
	var _edit_callout_width		= ec.get_callout_width();
	var _city_id  				= '';
	var _edit_search_height		= _edit_callout_height - 105;
	var _search_element_id 		= "";
	var _search_url 			= "";
	var _tool_name 	  			= "edit_search";
	var _search_result_length  	= 0;
	var _found_search_objects 	= "";
	var _search_object_label 	= ""; //was recently renamed from _object_label to _search_object_label
	var _search_object_id 		= ""; //was recently renamed from _object_id to search_object_id
	var _object_menu_div      	= "";
	var _ajax_test_url 			= '';
	var _default_tool_message	= '';	
	var _search_text			= '';
	var _search_delay			= 700; //milliseconds
	var _search_iterations		= 0;
	var _search_keys_pressed	= 0;
	var _buildings_list 		= new Array();
	
	/** ****************************************************************************************************** */
	/** ****************************************  public class variables  ************************************ */
	
	
	/** *************************************************
	 * 
	 *                  CONSTRUCTOR
	 * 
	 ************************************************** */	
	
	
	/** ***************************************************************************************************** */
	/** ****************************************** public class methods ************************************* */
	
	edit_search_class.prototype.embed_search_in_callout = function(){
		embed_search_in_callout();		
		//alert("edit search: " + $('#editCallout_searchPanel_searchTextInputField1').css('font-family'));
		return true;
	};
	
	edit_search_class.prototype.check_input_finished = function(){
		check_input_finished();
		return true;
	};	
	
	/**
	 * Function programmed to run on keypress. 
	 * See edit_callout_template.php
	 */
	edit_search_class.prototype.run_timer = function(){
		//alert("timer running...");
		_search_keys_pressed++;
		setTimeout( "es.check_search( " + _search_keys_pressed + " )", _search_delay );
	};//rt

	/**
	 * Method checks if search can be done. When keypress_count is equal
	 * to _search_keys_pressed this indicates the user is finished typing.
	 * If search_text is empty search gets reset.
	 */
	edit_search_class.prototype.check_search = function( keypress_count ){
		var search_text = $('#editCallout_searchPanel_searchTextInputField1').val();
		//alert('check search: ' + search_text );		
		if( !is_set( search_text )){
			embed_search_in_callout();
			$('#editCallout_searchPanel_saveButton').hide();
			return true;			
		}else if( keypress_count == _search_keys_pressed ){		
			run_search();
			_search_keys_pressed = 0;
		}		
	};//cs
	
	/** ****************** public method PROCESS SEARCH RESULT 
	 * used to direct the result of ajax call in run_search() to the
	 * right result processing method
	 * @return boolean 
	 */
	edit_search_class.prototype.process_search_result = function ( search_result ){
				
		//ec.show_ajax_debug( _ajax_test_url, search_result );
		
		_found_search_objects = search_result;		
		_search_result_length = search_result.length;
		
		if( _tool_purpose == 'pinpoint' ){ epp.set_found_objects(_search_result_length); }
		
		//alert('edit search: pro sr: so: ' + _search_object + ' _search_result_length: ' + _search_result_length );
		
		// ********************* 0 results ************************
		if( _search_result_length == 0 ){
			if( _tool_purpose == 'pinpoint' ){			
				epp.configure_pinpoint_callout();			
			}else{
				process_no_found_objects();
			}
			return true;						
			
		//************ more than 20 results (city)
		}else if( _search_result_length > 20 ){
			if( _tool_purpose == 'pinpoint' ){			
				epp.configure_pinpoint_callout();			
			}else{
				process_excessive_found_objects();
			}			
			return true;
						
		// ***************** exactly 1 result  ********************
		}else if( _search_result_length == 1 ){	
				process_single_found_object();		
			return true;								
			
		// *********** between 1 and 20 results (city) ************
		}else if( _search_result_length > 1 && _search_result_length <= 20 ){
			process_multiple_found_objects();
			return true;
			
		}else{
			
			return true;
		}
		
	};//psr
		
	
	edit_search_class.prototype.set_search_object = function( search_obj ){
		_search_object = search_obj;
		return true;
	};
	
	edit_search_class.prototype.set_object_menu_div = function( attach_to_id ){
		_object_menu_div = attach_to_id;
		return true;
	};	
	
	//TODO depricated, use get_search_object_label
	edit_search_class.prototype.get_picked_object_label = function(){		
		return _search_object_label;
	};
	
	//TODO depricated, use get_search_object_id
	edit_search_class.prototype.get_picked_object_id = function(){
		return _search_object_id;
	};
	
	edit_search_class.prototype.get_tool_task = function(){
		return _tool_task;
	};
	
	edit_search_class.prototype.get_search_object = function(){
		return _search_object;
	};
	
	edit_search_class.prototype.get_search_object_id = function(){
		return _search_object_id;
	};
	
	edit_search_class.prototype.get_search_object_label = function(){
		return _search_object_label;
	};
	
	edit_search_class.prototype.process_selected_object = function( id ){
		
		//alert('edit search: ' + _tool_purpose );
		
		if( _tool_purpose == "pinpoint" ){
			if( is_set(id)){
				var div_id = '#buildingId_' + id;
				_search_object_label = _buildings_list[id];
				_search_object_id    = id;
			}			
		}else{
			_search_object_label = $('select#search_object_menu option:selected').text();
			_search_object_id    = $('select#search_object_menu option:selected').val();
		}
		if( _tool_purpose == "register" ){
			$("#company_city_id").val( _search_object_id );
			$("#company_city_text").val( _search_object_label );
			$(ec.get_jq_original_element_id()).html(_search_object_label);
			$("#editTool_Message").html("");
			close_edit_callout();
		}
		if( _tool_purpose == "portfolio" ){
			portfolio.process_selected_object();
		}
		if( _property_name == "companyconnection" || _property_name == "tbuildinglinkedtprojectid" ){
			editvalue.process_selected_object();
		}		
		if( (_tool_purpose == "searchPanel" && _search_object == 'city') ||  _tool_purpose == "pinpoint" ){
			
			var res = new Array();
			res[0] = _search_object_label;
			res[1] = _search_object_id;

			if (_tool_purpose == "pinpoint") {
				epp.set_clicked_existing(true);
				epp.display_select_button(res);
				epp.configure_pinpoint_callout();
			} else {
				build_object_select_button();
			}
		}
		
		return true;
	};//pso
		
	
	edit_search_class.prototype.build_object_select_button = function( ){
		build_object_select_button();
		return true;
	};
	
	edit_search_class.prototype.build_object_select_menu = function( ){
		build_object_select_menu();
		return true;
	};
	
	edit_search_class.prototype.reset_search_result = function( ){
		//alert( "edit search: reset result " );
		_found_search_objects = '';
		return true;
	};
	
	/**
	 * Called from pinppoint run_pinpoint() takes address input
	 */
	edit_search_class.prototype.get_search_input = function( ){
		var input_content = $('#searchcontrol').val();
		//alert('edit search: ' + input_content );
		return true;
	};
	
	edit_search_class.prototype.set_test_url = function( url ){
		_ajax_test_url = url;
		return true;
	};
	
	edit_search_class.prototype.get_ajax_test_url = function(){
		return _ajax_test_url;
	};
	
	edit_search_class.prototype.get_found_search_objects = function(){
		return _found_search_objects;
	}
	
	/**
	 * Funtion called only by check_search method.
	 * 
	 */
	function run_search(){
		
		var search_text = $('#editCallout_searchPanel_searchTextInputField1').val();
		
		//alert("search being run..." + search_text );
		
		search_url  = ec.get_script_uri() + '?';
		search_url += 'pub_task=edit_callout';
		search_url += '&session_id=' + encodeURIComponent(_sess_id);
		
		search_url +=  '&search_text=' + encodeURIComponent(search_text);
		
		search_url += '&edit_object=' + encodeURIComponent(_search_object);
		search_url += '&tool_purpose=' + encodeURIComponent(_tool_purpose);
		search_url += '&property_name=' + encodeURIComponent(_property_name);				

        // if city id exists, we add it to search variables
        if ( is_set(_city_id)) {
        	if( _city_id > 100000 ){
        		search_url += '&cityid=' + encodeURIComponent(_city_id);
        	}
        }
			
		//limit search to a particular chosen country
		if( _tool_purpose == "portfolio" ) {
			var country_code = '';
			country_code = ec.get_object_id(); 				
			if( country_code ){
				search_url += '&country_code=' + encodeURIComponent(country_code);
			}
		}
		
		_ajax_test_url = search_url;

		//alert( "edit search: run:" + search_url );
				
		ajax_launcher( search_url, "", _tool_name, "process_search_result" );
	}
	
   /**
 	* used for various actions depending on tool purpose when single object is found
 	* When exactly one result is found, a new ajax call can be made to do some database actions
	* like add building or add city
 	* @return boolean
	*/
	function process_single_found_object( ){ 						
		
		if( _tool_purpose == 'pinpoint'){	
			build_object_select_divs();			
		}else{
			var html_text = '';
			html_text = '<font color="green"><b>Exactly one ' + _search_object + ' found.</b></font><br>';		
			$("#editTool_Message").html( html_text );
			build_object_select_menu();
		}
		
		if( _tool_purpose == "searchPanel" && _search_object == 'city'){
			_search_object_label = _found_search_objects[0].tcitytext;			
			if( _found_search_objects[0].tstatetext ){
				_search_object_label += " ," + _found_search_objects[0].tstatetext;
			}
			if( _found_search_objects[0].tcountrytext ){
				_search_object_label += " ," + _found_search_objects[0].tcountrytext;
			}			
		    _search_object_id    = _found_search_objects[0].tcityid;
		    html_text += '<br><center><font color="blue">' + _search_object_label + '</font>';
		    $("#editTool_Message").html( html_text );
		    build_object_select_button();
		}
		
		if( _tool_purpose == "register" ){				
			_search_object_label = _found_search_objects[0].tcitytext;			
			if( _found_search_objects[0].tstatetext ){
				_search_object_label += " ," + _found_search_objects[0].tstatetext;
			}
			if( _found_search_objects[0].tcountrytext ){
				_search_object_label += " ," + _found_search_objects[0].tcountrytext;
			}			
		    _search_object_id    = _found_search_objects[0].tcityid;
			if( ec.get_element_id() == "register_searchField_city" ){ 
				$("#company_city_id").val( _search_object_id );				
                $("#company_city_text").val( _search_object_label );
			}
			if( ec.get_element_id() == "register_searchField_billing" ){ 
				$("#billing_city_id").val( _search_object_id );
				$("#billing_city_text").val( _search_object_label );
			}
			
			$(ec.get_jq_original_element_id()).html(_search_object_label);			
			$("#editTool_Message").html("");
			close_edit_callout();			
		}
		
		if( _tool_purpose == "portfolio" ){
			//alert("edit search sfobj: " + typeof epp );
			if( typeof epp == 'object'){					
				ec.resize_edit_callout( _edit_search_height, _edit_callout_width, 'selectbutton');
				$("#editCallout_searchPinpoint_selectButton").show();
			}			
			portfolio.process_single_found_object( _found_search_objects ); 			
		}		

		if( _property_name == 'companyconnection' ){
			//alert( "edit search: pro single found obj: " + _found_search_objects[0].tcompanyid );
			editvalue.process_single_found_object( _found_search_objects );
		}				
		
		return true;
	}//psfo
	
	
	/**
	 * used to do some actions depending on tool purpose
	 * @return boolean
	 */
	function process_excessive_found_objects( ){		
		var html_text = '<font color="red"><b>Too many results, please be more specific.</b></font><br>';
		$("#editTool_Message").html( html_text );
		$("#search_object_menu").hide();
		$('#editCallout_searchPanel_saveButton').hide();
		return true;
	}//pefo	
	
	/**
	 * used to do some actions depending on tool purpose
	 * @return string perhaps some html
	 */
	function process_multiple_found_objects( ){
				
		if( _tool_purpose == 'pinpoint'){
			build_object_select_divs();
		}else{
			build_object_select_menu();
		}			
		return true;
	}//pmfo
	
	
	/**
	 * What to do if no objects are found. Standard message output
	 * to inform user of situation as well as other actions depending
	 * on tool purpose.
	 * @return boolean
	 */
	function process_no_found_objects( ){				

		if( _tool_purpose == 'portfolio' && _search_object == 'building' ){ 
			portfolio.process_no_found_objects( );
			return true;
		}else if( _property_name == 'companyconnection' ){
			editvalue.process_no_found_objects();
			return true;
		}else if( _tool_purpose == 'pinpoint' ){
			epp.process_no_found_objects();		
			return true;			
		}else{
			var alert_text  = '<font color="green"><b>No ' + _search_object + ' found.</b></font><br>';
			$("#editCallout_translation").html('');
			$("#editTool_Message").html( alert_text );			
			$("#search_object_menu").hide();
			$("#editCallout_searchPanel_saveButton").hide();			
			return true;
		}
	}//pmfo	
		
	
	/**
	 * used to build a select pulldown which is different depending on object 
	 * @return boolean true if select menu created
	 */
	function build_object_select_menu( ){			

		var menu_text = '';
		$("#search_pick_object").html('');
		
		if( _search_object == "company" || _search_object == "city" ){
			
			menu_text = '<select name="search_object_menu" id="search_object_menu" size="10" style="width:285px" onclick="es.process_selected_object()">';
		}else{
			menu_text = '<select name="search_object_menu" id="search_object_menu" size="5" style="width:320px" onclick="es.process_selected_object()">';
		}
		var alert_text = '';
		var label = '';
		var id = '';		

				
		for( each in _found_search_objects ){

			// ************************************************************************************************
			// ************************************** COUNTRY MENU ********************************************			
		    if( _search_object == "country" ){		  
		    	label = _found_search_objects[each].tcountrytext;
		    	menu_text += '<option ';
				if( each == 0 ) menu_text += 'selected';
				menu_text += ' value="' + _found_search_objects[each].tcountryid + '">' + label + '</option>';
		    }
			
		    // ************************************************************************************************
			// **************************************  CITY MENU  *********************************************
			if( _search_object == "city" ){								
				label = _found_search_objects[each].tcitytext;												
				if( _found_search_objects[each].tstatetext != undefined && _found_search_objects[each].tstatetext != null){
					label+= ", " + _found_search_objects[each].tstatetext;
				}												
				label += ", " + _found_search_objects[each].tcountrytext;				
				menu_text += '<option ';
				if( each == 0 ) menu_text += 'selected';
				menu_text += ' value="' + _found_search_objects[each].tcityid + '">' + label + '</option>';				
			}
			
			// **************************************************************************************************
			// **************************************  BUILDING MENU  *******************************************
			if( _search_object == "building" ){					
				//alert( "edit search display a building!" );
				$("#editCallout_searchPinpoint_objectList").css({"display":"block"});				
				label = _found_search_objects[each].tbuildingtext;
				id    = _found_search_objects[each].tbuildingid;
				menu_text += '<option value="' + id + '">' + label + '</option>';
				
				//blank for now uncomment to not show pulldown with pinpoint finds
				//also comment out line 251, uncomment 252
		        //menu_text = "";			
			}
			
			// **************************************************************************************************
			// **************************************  COMPANY MENU  ********************************************
			if( _search_object == "company" ){
				
				$("#editCallout_searchPanel_searchTextInputField2").hide();
				$("#editCallout_searchPanel_searchTextInputLabel2").hide();		
				$("#editCallout_searchPinpoint_objectList").css({"display":"block"});
				label = _found_search_objects[each].tcompanytext + ',    ' + _found_search_objects[each].tcitytext + ', ' + _found_search_objects[each].tcountrytext;
				id    = _found_search_objects[each].tcompanyid;
				menu_text += '<option value="' + id + '">' + label + '</option>';
			}

            // **************************************************************************************************
            // **************************************  COMPLEX MENU  ********************************************
            if( _search_object == "complex" ){
                
                $("#editCallout_searchPanel_searchTextInputField2").hide();
                $("#editCallout_searchPanel_searchTextInputLabel2").hide();     
                $("#editCallout_searchPinpoint_objectList").css({"display":"block"});
                label = _found_search_objects[each].tprojecttext;
                id    = _found_search_objects[each].tprojectid;
                menu_text += '<option value="' + id + '">' + label + '</option>';
            }
		
		}//fe search obj
		
		menu_text += '</select>';
		
		$('#editCallout_searchPanel_saveButton').hide();
		if( _object_menu_div != undefined && _object_menu_div != "" ){			
			$(_object_menu_div).css({ "display":"inline" });
			$(_object_menu_div).html( menu_text );			
		}else{
			$("#search_pick_object").show();
			$("#search_pick_object").html( menu_text );			
		}        
		$('#editCallout_translation').html('');
		alert_text  = '<font color="blue"><b>Please, choose a ' + _search_object + '.</b></font><br>';
		$("#editTool_Message").html( alert_text );
		
		return true;
	}//fun bosm
	
	/**
	 * Build object select menu will be eventually replaced with this method
	 * when time permits
	 * @return pulldown menu text for displaying list of objects
	 */
	function build_object_select_divs(){
	
		//alert( "edit search: build obj divs: " + _tool_list );
		var menu_text = '';
		var alert_text = '';
		var label = '';
		var id = '';
		var title = '';
		var bgcol = '';		

		$("#search_pick_object").html('');
		
		//alert('edit search: ' + _found_search_objects[each].tbuildingtext );
		
		menu_text = '<div id="search_object_menu" style="overflow:auto;width:331px;height:80px;">';
		for( each in _found_search_objects ){						
			label = _found_search_objects[each].tbuildingtext;
			id    = _found_search_objects[each].tbuildingid;
			title = _found_search_objects[each].building_html;

			_buildings_list[id] = label;
			if( each % 2 == 0 ){
				bgcol = 'background:#BFD2FF';
			}else{
				bgcol = 'background:#E5EDFF';
			}
			menu_text += '<div style="' + bgcol + '" id="buildingId_' + id;
			menu_text += '" onclick="es.process_selected_object(' + id + ')">' + title + '</div>';
		}//fe
		menu_text += '</div>';
		$("#editCallout_searchPinpoint_objectList").html( menu_text );
		$("#editCallout_searchPinpoint_objectList").show();
		//$('#editCallout_searchPinpoint_selectButton').hide();
		$("#editCallout_translation").html('');
		epp.configure_pinpoint_callout();
	}
	
	/**
	 * Method used to produce an add button when a particular searched for item is found.
	 */
	function build_object_select_button( ){
		
		$('#editCallout_searchPanel_searchTextInputField2').val('');
		
		//alert( 'edit search: build sel butt: ' + _property_name);		
		var value_text = "add " + _search_object;
		if( ec.get_property_name() == 'companyconnection' ){
			value_text = "add involvement";
		}
		
		$('#editCallout_searchPanel_saveButton').val(value_text);
		
		if( _tool_purpose == "searchPanel" && _search_object == 'city'){
			$("#last_js_library").append('<script type="text/javascript" src="/htmldata/js/emporis/templates/edit_value.js"></script>');
			this.editvalue = new edit_value_class();
			editvalue.fill_edit_value_source();
		}
		$('#editCallout_searchPanel_saveButton').click( function(){ editvalue.process_selected_object(); });
		$('#editCallout_searchPanel_saveButton').show();
		return true;
	}//bosm

	
	function close_edit_callout(){
		$("#editCallout_innerContainer").dialog( "close" );
		return true;
	}//fun cec
	
	function embed_search_in_callout(){
		
		//alert( "edit search: embedd: " + _tool_purpose );
				
		$("#editTool_Message").html("");
		$("#search_pick_object").html("");
		$('#editCallout_searchPanel').show();
		$("#editCallout_searchPanel_searchTextInputField1").val("");
		//$("#editCallout_searchPanel_searchTextInputField1").focus();
		$("#editCallout_searchPanel_searchTextInputField1").show();
		
		$("#editCallout_searchPanel_searchTextInputLabel1").show();		
		$("#editCallout_searchPanel_searchTextInputField2").hide();
		$("#editCallout_searchPanel_searchTextInputLabel2").hide();

		if( _tool_purpose == "portfolio"){		
			$("#editCallout_searchPanel_action1").hide();
			$("#editCallout_searchPanel_pinpoint").hide();
			$("#editCallout_searchPanel_value").hide();
			$("#editCallout_searchPinpoint_objectList").hide();
			$("#editCallout_searchPinpoint").hide();
			return true;
		}
		if( _tool_purpose == "register" ){
			$("#editCallout_searchPanel_searchTextInputField1").show();
			return true;
		}
		if( ec.get_property_name() == 'companyconnection' ){
			_search_object = 'company';
			editvalue.embed_search_in_callout();
			//alert('edit search: embed: eval ' + _edit_search_height );						
			return true;
		}
        if( _tool_purpose == 'searchPanel' && _property_name == "tbuildinglinkedtprojectid" ){
            _search_object = 'complex';
            editvalue.embed_search_in_callout();
            //alert('edit search: embed: eval ' + _edit_search_height );                        
            return true;
        }
		if( _tool_purpose == 'searchPanel' && _property_name == "tcompanylinkedtcityid" ){
			_search_object = 'city';			
			_default_tool_message = ec.set_callout_message('Type in a city name', 'blue');
			ec.resize_edit_callout( _edit_search_height, _edit_callout_width );
			return true;
		}
		
	}//esc
	

} //esc
