﻿
// ==== Remove the old Elabels when the user asks for the results to be cleared ====
function doIdleCallback() {
	//map.clearOverlays();
}

// ==== Remove the old ELabels when a new search is performed ====
function doSearchCompleteCallback(search) {
	//map.clearOverlays();
}

// ==== Scan through the markers and GlocanLresults, creating ELabels ====
function doMarkersSetCallback(info) {
	old_labels_arr=search_labels_arr;
	old_markers_arr=search_markers_arr;
	search_markers_arr = new Array();
	search_labels_arr = new Array();
	for (var i=0; i<info.length; i++) {
	  var title = info[i].result.titleNoFormatting;
	  var marker = info[i].marker;
	  var label = new ELabel(marker.getLatLng(), '<div style="background-color:#ccccff;border:2px solid black;white-space:nowrap">'+title+'<\/div>', null, new GSize(3,-23), 75);
	  map.addOverlay(label);
	  marker.Elabel = label; // === remember which Elabel beongs to this marker
	  search_markers_arr.push(marker);
	  search_labels_arr.push(label);
	}
	if (old_labels_arr != null) {// it is not the first time we draw markers
	 for (i in old_labels_arr)
	 {
	   map.removeOverlay(old_labels_arr[i]);
	 }
	}
	if (old_markers_arr != null) {// it is not the first time we draw markers
	 for (i in old_markers_arr)
	 {
	   map.removeOverlay(old_markers_arr[i]);
	 }
	}
	addClearResultsControl();
	darom();
}

function addClearResultsControl()
{
	if (ClearSearchResultsControlExists==0)
	{
	   ClearSearchResultsTypeControl=new ClearSearchResultsControl();
	   map.addControl(ClearSearchResultsTypeControl);
	   ClearSearchResultsControlExists=1;
	}
}

// ==== hide the Elabel when the info Window opens ====
function doGenerateMarkerHtmlCallback(marker,html,result)
{
	if (hiddenElabel)
	{
	  hiddenElabel.show();
	  hiddenElabel = 0;
	}
	marker.Elabel.hide();
	hiddenElabel = marker.Elabel; // remember which ELabel is hidden
	return html; // return the unmodified html object
}

function handleClearSearchResults()
{
	if (search_labels_arr != null)
	{// it is not the first time we draw markers
	 for (i in search_labels_arr)
	 {
	   map.removeOverlay(search_labels_arr[i]);
	 }
	}
	if (search_markers_arr != null) {// it is not the first time we draw markers
	 for (i in search_markers_arr)
	 {
	   map.removeOverlay(search_markers_arr[i]);
	 }
	}
	map.removeControl(ClearSearchResultsTypeControl);
	ClearSearchResultsControlExists=0;
}


function sendSearchString() {

	// looking for the input box of the search bar as implemented by google's API.
	// the ID of the input box is changing, so cannot be referenced; therefore using the input box's name:
	var elements=document.getElementsByName("search");
	var searchString;
	if (elements.length == 1)
	{
		searchString = elements[0].value;
	}
	else
	{
		searchString = "Search string error";
	}

	var lat = map.getCenter().lat() * 1000000;
	var lng = map.getCenter().lng() * 1000000;

	downloadUrl("http://www.anagog.com/php/findMyCar/fmc_process.php?task=map_search&user=WA"+username+"&version="+Version+"&s="+searchString+"&lat=" + lat + "&long=" + lng, function(data, status){
		}
	);
}
