/* 
Routines in support of using Ajax to get static map for a given Position.

Used by Trackem Location, Detailed Location, and Stop reports

*/

	// given LatLon and Label, invoke UTILS function to request Static Map for the position.  UTILS response will contain NULL or full URL to cached static map
	// UtilsURL must be passed into routine via parameter since unable to run PHP code from within the script...  Actual data fed into this parm 
	// is what is produced by php call : <?php echo url_for('utils/getStaticMapURL');?>
	function getStaticMap(Lat, Lon, Label, UtilsURL)
	{
		if (Lat >= -90.0 && Lat <= 90.0 && Lon >= -180.0 && Lon <= 180.0)
		{
			var options = { 
			url: UtilsURL,
			data: "lat=" + Lat + "&lon=" + Lon + "&label=" + Label, 
	        success:       function (json, statusText) { popStaticMap(json,statusText);}, 
	        dataType:	'json'  				
	        };
	  		$.ajax(options);
 		}
 		else
 		{
 			alert("Invalid coordinates - Lat (" + Lat + ") or Lon (" + Lon + ") are out of range [-90:90 / -180:180]."); 
 		}
	}

	// Processes UTILS response of NULL or full URL to cached static map.  If URL, pop up in child window.  If NULL, alert user that no graphic available.  
	function popStaticMap(json, statusText)
	{
		if (json.fullURL)
		{
			staticMap = window.open(json.fullURL, "SelectedPosition", "status=0,width=420,height=420");
			staticMap.focus();
			window.onunload = function(){staticMap.close()}; 
		}
		else
		{
 			alert("Mapping Provider unable to generate static map for coordinates Lat:" + Lat + " / Lon:" + Lon + "."); 
		}
	}

	// given DevicePositionHistory objids for start and finishing points in a Route, and possibly labels to assign to the start and end points, 
	// invoke UTILS function to request Static Routed Map for the position.  UTILS response will contain NULL or full URL to cached static map.
	// UtilsURL must be passed into routine via parameter since unable to run PHP code from within the script...  Actual data fed into this parm 
	// is what is produced by php call : <?php echo url_for('utils/getStaticRoutedMapURL');?>
	function getStaticRoutedMap(Start, Finish, StartLabel, FinishLabel, UtilsURL)
	{
		if (Start && Finish)
		{
			var options = { 
			url: UtilsURL,
			data: "start_point=" + Start + "&finish_point=" + Finish + "&start_label=" + StartLabel + "&finish_label=" + FinishLabel, 
	        success:       function (json, statusText) { popStaticMap(json,statusText);}, 
	        dataType:	'json'  				
	        };
	  		$.ajax(options);
 		}
 		else
 		{
 			alert("Invalid start and finish points - Start (" + Start + ") or Finish (" + Finish + ") for route."); 
 		}
	}
