/****************************************************************************************************************
 * 
 * 												CLASS EDIT PINPOINT (uses edit_callout_class)
 * 
 ****************************************************************************************************************
 * STARTED as of 8.April 2009, 11:20
 * Instantiated by edit_callout_class in embed_pinpoint_in_callout() method.
 * 
 * 
 * 
 * @author Rhonda Kammer
 ****************************************************************************************************************
 * NOTES:
 *
 **/

function edit_pinpoint_class( ){
	
	/** ****************************************************************************************************** */
	/** ****************************************  private class variables ************************************ */
	
	var _edit_object  	= ec.get_edit_object();
	var _tool_purpose 	= ec.get_tool_purpose();
	var _property_name	= ec.get_property_name();
	var _nav_id 		= ec.get_nav_id();	
	var _tool_name 	  	= "edit_pinpoint";
	var _pp_height		= "";
	var _pp_width		= "";
	var _pp_name 		= "ppbic";
	var _pp_gkey 		= "ABQIAAAAuSdiuC0IXzXGnBD7lySfjBTXczin-9bG-moAn-XAmHaF5KHz3xQCHg9wq5wZDQkx4zgotE8yyYfQOg";	
	var _pp_provider_google 	= "google";
	var _pp_provider_ms			= "msve";
	var _pp_user_language		= "en";
	var _pp_container			= ""
	var _pp_city_id  			= ec.get_object_id();
	//FOR TESTING var _pp_url					= "http://dev28.emporis.com/?";		
	//FOR LIVE: 
	var _pp_url					= "http://widgets.emporis.com/?";
	var _pp_session_id 			= ec.get_session_id();
	var _pp_latitude   			= "";
	var _pp_longitude			= "";
	var _pp_nav 				= ec.get_nav_template();
	var _pp_building_id 		= '';
	var _ajax_test_url 					= '';
	
	
	/** *************************************************
	 * 
	 *                  CONSTRUCTOR
	 * 
	 ************************************************** */
	build_opening_url();

		
	/** ***************************************************************************************************** */
	/** ****************************************** public class methods ************************************* */
	
	/**
	 * Method takes a city id, forms an ajax url and makes remote call
	 * to pinpoint which presents the city contained in the city id.
	 * 
	 * @return pinpoint map displayed 
	 */
	edit_pinpoint_class.prototype.run_pinpoint = function(){		
		run_pinpoint();
	}//rpp
	
	
	edit_pinpoint_class.prototype.enlarge_map = function( height, width ){	
		if( height == "" || height == undefined ){ 
			_pp_height = 300; 
		}else{
			_pp_height = height;
		}
		if( width == "" || width == undefined ){ 
			_pp_width = 332; 
		}else{
			_pp_width = width;
		}		
		run_pinpoint();
	}//em
	
	
	/**
	 * Method retrieves objects (as of 14 April 2009 only buildings) located in the
	 * city id output by edit_search_class in methods process_selected_object() or
	 * process_single_found_object() called only by ajax not by external process.
	 * @param response data from ajax call in method run_pinpoint()
	 * @return 
	 */
    edit_pinpoint_class.prototype.get_pinpoint_objects = function ( response ){		 		 
		 
		 //alert( "edit pinpoint get pp objs: " + _tool_purpose );
		 
		 //alert( "edit pinpoint: r: " + response[0] );
		 
		 _pp_building_id = response[1];
		
		 //go get the name of the found building
		 if( response[0] == 'building' ){				
			
			search_url  = ec.get_script_uri() + '?pub_task=edit_callout';
			search_url += '&session_id=' + encodeURIComponent(_pp_session_id);
			search_url += '&edit_object=building';
			
			//add pinpoint first looks up a building name
			if( _property_name == 'addPinpoint' ){
				search_url += '&property_name=searchBuildingPinpoint';
			}else{
				search_url += '&property_name=' + encodeURIComponent(_property_name);
			}
			search_url += '&tool_purpose=' + encodeURIComponent(_tool_purpose);
			var bid = response[1];
			search_url += '&building_id=' + encodeURIComponent(bid);					
			
			if(typeof es == "object" ){
									
				//tell edit search where to display objects
				//es.set_object_menu_div("#editCallout_searchPinpoint_objectList");
										
				//tell edit_search which objects to display
			 	es.set_search_object( "building" );			 			 										
			 	//go get objects
				if( _tool_purpose == 'portfolio'){
					ajax_launcher( search_url, '', 'edit_pinpoint', "portfolio.process_single_found_object" );
					return true;
				}
				if( _tool_purpose == 'cityPage' || _property_name == 'addPinpoint' ){
					//alert( "edit pinpoint get pp objs: " + search_url );
					ajax_launcher( search_url, '', 'city_page', "citypage.process_single_found_object" );
					return true;
				}
								
			}else{
				return false;	
			}
			
		}else{
			
			_pp_latitude = response[0];
			_pp_longitude = response[1];
			
			if( _tool_purpose == 'cityPage' || _property_name == 'addPinpoint' ){				
				citypage.process_no_found_objects();
				return true;
			}
			if( _tool_purpose == 'portfolio' ){
				portfolio.process_no_found_objects();
				return true;
			}
			if( _tool_purpose == 'building' && _property_name == 'editPinpoint'){
				display_save_button( response );
				return true;
			}
		}
	}//gppo
	
	
	edit_pinpoint_class.prototype.get_building_data = function(){
		var coords = new Array();
		coords[0] = _pp_latitude;
		coords[1] = _pp_longitude;
		coords[2] = _pp_city_id;
		return coords;
	}//gbd
	
	edit_pinpoint_class.prototype.update_pinpoint = function( latitude, longitude ){
		
		// alert( "edit pinpoint: update pinpoint please! lat: " + latitude + " long: " + longitude );
		//alert( "edit pinpoint: nav id: " + _nav_id + " tool pur: " + _tool_purpose + " pr name: " + _property_name );
		
		var url  = ec.get_script_uri() + '?pub_task=edit_callout';
		url += '&session_id=' + encodeURIComponent(_pp_session_id);		
		url += '&property_name=' + encodeURIComponent(_property_name);
		url += '&tool_purpose=' + encodeURIComponent(_tool_purpose);
		url += '&latitude=' + encodeURIComponent(latitude);
		url += '&longitude=' + encodeURIComponent(longitude);
		var booid = $('#editCallout_buildingooid').text();
		url += '&object_id=' + encodeURIComponent(booid);
		
		//alert( "update pinpoint: " + url );
		
		_ajax_test_url = url;
		
		ajax_launcher( url, '', "edit_pinpoint", "epp.display_page" );	
		
	};//upp
	
	edit_pinpoint_class.prototype.display_page = function( response ){
		
		//ec.show_ajax_debug( _ajax_test_url, response );			
		
		if( response != undefined && _tool_purpose == 'building' && _property_name == 'editPinpoint' ){
			//alert( "edit pinpoint: displ page: " + response );
			ec.refresh_page();
		}		
	}//dp
	

	function run_pinpoint(){
		
		//alert('edit pinpoint: widget call ');
		$("#editCallout_searchPinpoint").html("");
		$("#editTool_Message").html("");		
		$("#editCallout_searchPinpoint").show();        
        //do not know what this is. rlk $js = ppic();
		$.getScript( _pp_url, function(){ emporisInitLoader(); });		
		
	}//rpp
	
	function build_opening_url(){
		
		_pp_url += 'widget=' +    _pp_name;
		_pp_url += '&gkey=' +     _pp_gkey;	
	 	//_pp_url += '&provider=' +  _pp_provider_ms;
	 	_pp_url += '&provider=' +  _pp_provider_google;
	 	_pp_url += '&language=' + _pp_user_language;
	 	
	 	//set default size of map
	 	if( _pp_height == "" || _pp_height == undefined ){ _pp_height = 300; }
		if(  _pp_width == "" || _pp_width == undefined ){ _pp_width = 332; }		
		_pp_url += '&width=' +    _pp_width + "px";
	 	_pp_url += '&height=' +   _pp_height + "px";	 						
			 
		//for city page we will need to add another parameter
		if( _pp_nav == 'city' || _property_name == 'addPinpoint' ){
			_pp_url += '&click_existing=yes';
		}else{
			_pp_url += '&click_existing=no';
		}				
		 
		//when we are using the pinpoint as pinpoint editor
		if( _property_name == 'editPinpoint' || _property_name == 'addPinpoint' ){
			var lat  = $('#editCallout_tbuildinglatitude').text();
		 	var long = $('#editCallout_tbuildinglongitude').text();		 		 
		 	_pp_url += '&building_lat=' + encodeURIComponent(lat);
		 	_pp_url += '&building_long=' + encodeURIComponent(long);		 
		}
		if( _property_name == 'editPinpoint' ){
			_pp_url += '&edit_existing=yes';
		}
		if( _property_name == 'addPinpoint' ){
			_pp_url += '&add_nearby=yes';
		}
		
		_pp_url += '&callback=epp.get_pinpoint_objects';
		_pp_url += '&container=editCallout_searchPinpoint';
		_pp_url += '&id=' + encodeURIComponent(_pp_city_id);
		 
		//alert( "edit pinpoint build url: " + _pp_url );
		
	}//bou
	
	function display_save_button( response ){
		if( response != undefined ){
			var button_text = '<input type="button" onclick="epp.update_pinpoint(' + response[0] + ', ' + response[1] + ')" value="update pinpoint">&nbsp;&nbsp;<br>';
			button_text += '<font color="blue"><b> to longitude: ' + response[0] + '<br> latitude: ' + response[1] + '</b></font>&nbsp;&nbsp;&nbsp;&nbsp;';
		}else{
			var button_text = 'Pinpoint not registered. Please refresh and click again.';
		}		
		$('#editCallout_searchPinpoint_selectButton').html( button_text ).show();
	}
	

					
}//cl epp

















