    var map = null;
    var geocoder = null;
    var gLocalSearch;
    //var gSelectedResults = [];
    var markers = [];
    var gCurrentResults = [];
    var gSearchForm;
    var countryCode;
    var response;

    
    // Create our "tiny" marker icon
    var gSmallIcon = new GIcon();
    gSmallIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
    gSmallIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    gSmallIcon.iconSize = new GSize(22, 20);
    gSmallIcon.shadowSize = new GSize(32, 20);
    gSmallIcon.iconAnchor = new GPoint(6, 20);
    gSmallIcon.infoWindowAnchor = new GPoint(5, 1);
    
    function initialize(defaultLat, defaultLng, defaultAddress, save) {
 
      	
      if (GBrowserIsCompatible()) {
          
      	

        map = new GMap2(document.getElementById("map_canvas"));
        geocoder = new GClientGeocoder();
        
        gSearchForm = new GSearchForm(false, document.getElementById("searchform"));
      	gSearchForm.setOnSubmitCallback(null, CaptureForm);
              	
      	gLocalSearch = new GlocalSearch();
	    gLocalSearch.setCenterPoint(map);
	   //gLocalSearch.setAddressLookupMode(ADDRESS_LOOKUP_DISABLED);
	    //gLocalSearch.setResultSetSize(8);
	    gLocalSearch.setSearchCompleteCallback(null, OnLocalSearch);
        
        
        if(defaultLat != 0 && defaultLng !=0 && defaultLat != '' && defaultLng !='')
       		map.setCenter(new GLatLng(defaultLat, defaultLng), 13);
       	else if(defaultAddress != '')
       		
       		showAddress(defaultAddress,save);
       	else
       		map.setCenter(new GLatLng(1, -1), 1); 	
       	
        map.enableDragging();
        map.addControl(new GSmallZoomControl());
        map.addControl(new GMapTypeControl());
               

	    gSearchForm.execute('bar');
	     
      }
    }
    
    function CaptureForm(searchForm) {
      gLocalSearch.execute(searchForm.input.value);
      return false;
    }

    function OnLocalSearch() {
      if (!gLocalSearch.results) return;
     
      // Clear the map and the old search well
    
      var searchWell = document.getElementById("searchwell");

      // Clear the map and the old search well
      searchWell.innerHTML = "";
      
      for (var i = 0; i < gCurrentResults.length; i++) {
        if (!gCurrentResults[i].selected()) {
          map.removeOverlay(gCurrentResults[i].marker());
        }
      }

      gCurrentResults = [];
      for (var i = 0; i < gLocalSearch.results.length; i++) {
        gCurrentResults.push(new LocalResult(gLocalSearch.results[i],i));
      }

      var attribution = gLocalSearch.getAttribution();
      if (attribution) {
        document.getElementById("searchwell").appendChild(attribution);
      }
      
    }
    
        // A class representing a single Local Search result returned by the
    // Google AJAX Search API.
    function LocalResult(result, j) {
      this.result_ = result;
      this.result_.contor = j;
      this.resultNode_ = this.defaultHtml(j);
      document.getElementById("searchwell").appendChild(this.resultNode_);
      map.addOverlay(this.marker(gSmallIcon));
    }

    // Returns the GMap marker for this result, creating it with the given
    // icon if it has not already been created.
    LocalResult.prototype.marker = function(opt_icon) {
      if (this.marker_) return this.marker_;
      var marker = new GMarker(new GLatLng(parseFloat(this.result_.lat),
                                         parseFloat(this.result_.lng)),
                               opt_icon);
      GEvent.bind(marker, "click", this, function() {
        marker.openInfoWindow(this.defaultHtml() );
      });
      
       GEvent.bind(marker, "mouseover", this, function() {
        marker.openInfoWindow(this.defaultHtml() );
      });
      /* GEvent.bind(marker, "mouseout", this, function() {
        marker.closeInfoWindow();
      });*/
      this.marker_ = marker;
      markers[this.result_.contor] = marker;
      return marker;
    }

    // "Saves" this result if it has not already been saved
    LocalResult.prototype.select = function() {
      if (!this.selected()) {
        this.selected_ = true;

        // Remove the old marker and add the new marker
        map.removeOverlay(this.marker());
        this.marker_ = null;
        map.addOverlay(this.marker(G_DEFAULT_ICON));

        // Add our result to the saved set
        //document.getElementById("selected").appendChild(this.selectedHtml());

        // Remove the old search result from the search well
      //  this.resultNode_.parentNode.removeChild(this.resultNode_);
      }
    }

    
     // Returns the HTML we display for a result before it has been "saved"
    LocalResult.prototype.defaultHtml = function() {
      var container = document.createElement("div");
     
     var output  = '<span class="boldedText"><a href="javascript:markers['+this.result_.contor+'].openInfoWindow(gCurrentResults['+this.result_.contor+'].defaultHtml());">' + this.result_.title + '</a></span><br/>' + this.result_.streetAddress + '<br/>' + this.result_.city;
     if(this.result_.region)
     	output = output +', ' + this.result_.region ;

     if(this.result_.phoneNumbers)
     	output = output + '<br/>' + this.result_.phoneNumbers[0].number; 
     	
     output = output + '<form name="localResult" method="POST" action="">' +
     						'<input type="hidden" name="location" value="' +  this.result_.title + '">' +
      						'<input type="hidden" name="address" value="' +  this.result_.streetAddress + '">' +
      						'<input type="hidden" name="country" value="' +  this.result_.country + '">' + 
      						'<input type="hidden" name="region" value="' +  this.result_.region + '">' + 
      						'<input type="hidden" name="city" value="' +  this.result_.city + '">' +      
      						'<input type="hidden" name="lat" value="' +  this.result_.lat  + '">' +  
      						'<input type="hidden" name="lng" value="' +  this.result_.lng  + '">' +  
      						'<input type="submit" name="saveLocalSearchLocation" value="Select location" class="buttonField"></form>'+ '<div class="line"><!-- --></div>';
     
	  container.innerHTML = output;
      return container;
    }
    
    // Returns the HTML we display for a result before it has been "saved"
    LocalResult.prototype.unselectedHtml = function() {
      var container = document.createElement("div");
      container.className = "unselected";
      container.appendChild(this.result_.html.cloneNode(true));
      /*var saveDiv = document.createElement("div");
      saveDiv.className = "select";
      saveDiv.innerHTML = "Save this location";
      GEvent.bindDom(saveDiv, "click", this, function() {
        gMap.closeInfoWindow();
        this.select();
        gSelectedResults.push(this);
      });
      container.appendChild(saveDiv);*/
      return container;
    }

    // Returns the HTML we display for a result after it has been "saved"
    LocalResult.prototype.selectedHtml = function() {
      return this.result_.html.cloneNode(true);
    }

    // Returns true if this result is currently "saved"
    LocalResult.prototype.selected = function() {
      return this.selected_;
    }
    
    function showAddress(address,save) {
    	
      if (geocoder) {
      	
        geocoder.getLocations(
          address,
          function(response) {
          
           if (!response || response.Status.code != 200) {
	        alert("Searched address not found");
	        map.setCenter(new GLatLng(1, -1), 1); 
	      } 
	      else if(!response.Placemark[0].AddressDetails.Country) {
	      	alert("Searched address not found");
	      	map.setCenter(new GLatLng(1, -1), 1); 
	      }
	      else if(!response.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality) {
	     	 alert("Searched address not found");
	     	 map.setCenter(new GLatLng(1, -1), 1); 
	      }
	      else {
	        place = response.Placemark[0];
	       	
	       var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
	      	
              map.setCenter(point, 13);
            
              map.clearOverlays() ;
            
              gSearchForm.execute('bar');
             
              var marker = new GMarker(point,{draggable: false});
           	   		
              var addressWindow ;
              if(save>0) {
              	
              	if(place.AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare)
              		address = place.AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
              	addressWindow = '<span class="boldedText">Location address:</span>' + address + ', ' + place.AddressDetails.Country.AdministrativeArea.Locality.LocalityName + ', ' + place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName +
      						'<br/><form name="searchedAddress" method="POST" action="" onsubmit="if(this.location.value==\'\') {alert(\'Fill in the location name\');  this.location.focus(); return false;}">' +
      						'<span class="boldedText">Location name:</span> <input type="text" name="location" value="" class="inputField"/>' ;
      				if(save != 2)
      					addressWindow = addressWindow +	'<br/><span class="boldedText">Keep this location private: </span> <input type="checkbox" name="private" value="1"/>';
      			addressWindow = addressWindow +	'<input type="hidden" name="address" value="' +  address + '">' +
      						'<input type="hidden" name="country" value="' +  place.AddressDetails.Country.CountryNameCode + '">' + 
      						'<input type="hidden" name="region" value="' +  place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName + '">' + 
      						'<input type="hidden" name="city" value="' +  place.AddressDetails.Country.AdministrativeArea.Locality.LocalityName + '">' +      
      						//'<input type="hidden" name="zip" value="' +  place.AddressDetails.Country.AdministrativeArea.PostalCode.PostalCodeNumber + '">' +      
      						'<input type="hidden" name="lat" value="' +  marker.getLatLng().lat()  + '">' +  
      						'<input type="hidden" name="lng" value="' +  marker.getLatLng().lng()  + '">' +  
      						'<br/><input type="submit" name="saveMapLocation" value="Save location" class="buttonField"></form>';
             	
              } 
      		 else
              	addressWindow = '<b>Location address:</b> ' + address ;
              GEvent.addListener(marker, "mouseover", function() {
			 	 marker.openInfoWindowHtml(addressWindow);
			  });
			  GEvent.addListener(marker, "click", function() {
			 	 marker.openInfoWindowHtml(addressWindow);
			  });
			 
              map.addOverlay(marker);
              
              if(save) {
              	 marker.openInfoWindowHtml(addressWindow);
              }
             
            }
          }
        );
      }
    }
    
    function initializeOnUpdate(defaultLat, defaultLng, lid, address,  name, is_sponsor) {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        var latlng;
        if(defaultLat != 0 && defaultLng !=0) {
        	latlng = new GLatLng(defaultLat, defaultLng);
       		map.setCenter(latlng, 14);
        }
       	else {
       		latlng = new GLatLng(1, -1);
       		map.setCenter(latlng, 1); 	
       	}	
        map.enableDragging();
        map.addControl(new GSmallZoomControl());
   		
        geocoder = new GClientGeocoder();
        
		var marker = new GMarker( latlng,{draggable: true});
		map.addOverlay(marker); 
		var addressWindow = '<span class="boldedText">Location address:</span> ' + address + 
					'<br/><form name="searchedAddress" method="POST" action="" onsubmit="if(this.locationName.value==\'\') {alert(\'Fill in the location name\');  this.locationName.focus(); return false;}">' +
					'<span class="boldedText">Location name:</span> <input type="text" name="locationName" value="'+ name +'" class="inputField"/>' +
					'<br/><span class="boldedText">Featured location: </span> <input type="checkbox" name="featured" value="'+ is_sponsor +'"/>' +
					'<input type="hidden" name="address" value="' +  address + '">' +  
					'<input type="hidden" name="lat" value="' +  defaultLat  + '">' +  
					'<input type="hidden" name="lng" value="' +  defaultLng  + '">' +  
					'<input type="hidden" name="lid" value="' +  lid  + '">' + 
					'<br/><input type="submit" name="updateMapLocation" value="Update location" class="buttonField"></form>' ;
		
		
		GEvent.addListener(marker, "mouseover", function() {
		 marker.openInfoWindowHtml(addressWindow);
		});
		GEvent.addListener(marker, "click", function() {
		 marker.openInfoWindowHtml(addressWindow);
		});
		 
	  	marker.openInfoWindowHtml(addressWindow);
      
       	gSearchForm = new GSearchForm(false, document.getElementById("searchform"));
      	gSearchForm.setOnSubmitCallback(null, CaptureForm);
	     
      }
    }
    
    function showAddressOnUpdate(address, name, lid, is_sponsor) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 14);
              var marker = new GMarker(point,{draggable: false});
             
              var addressWindow = '<span class="boldedText">Location address:</span> ' + address + 
      						'<br/><form name="searchedAddress" method="POST" action="" onsubmit="if(this.locationName.value==\'\') {alert(\'Fill in the location name\');  this.locationName.focus(); return false;}">' +
      						'<span class="boldedText">Location name:</span> <input type="text" name="locationName" value="' + name + '" class="inputField"/>' +
      						'<br/><span class="boldedText">Featured location: </span> <input type="checkbox" name="featured" value="' + is_sponsor + '"/>' +
      						'<input type="hidden" name="address" value="' +  address + '">' +  
      						'<input type="hidden" name="lat" value="' +  marker.getLatLng().lat()  + '">' +  
      						'<input type="hidden" name="lng" value="' +  marker.getLatLng().lng()  + '">' +  
      						'<input type="hidden" name="lid" value="' +  lid  + '">' +
      						'<br/><input type="submit" name="updateMapLocation" value="Update location" class="buttonField"></form>';
             	
             
              GEvent.addListener(marker, "mouseover", function() {
			 	 marker.openInfoWindowHtml(addressWindow);
			  });
			  GEvent.addListener(marker, "click", function() {
			 	 marker.openInfoWindowHtml(addressWindow);
			  });
			 
              map.addOverlay(marker);
              marker.openInfoWindowHtml(addressWindow);
                          
            }
          }
        );
      }
    }
    
     function showSimpleAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 13);
              var marker = new GMarker(point,{draggable: false});
             
              var addressWindow = '<b>Location address:</b> ' + address + 
      						'<br/><form name="searchedAddress" method="POST" action="" onsubmit="if(this.locationName.value==\'\') {alert(\'Fill in the location name\');  this.locationName.focus(); return false;}">' +
      						'<b>Location name:</b> <input type="text" name="locationName" value=""/>' +
      						'<br/><b>Featured location: </b> <input type="checkbox" name="featured" value="0"/>' +
      						'<input type="hidden" name="address" value="' +  address + '">' +  
      						'<input type="hidden" name="lat" value="' +  marker.getLatLng().lat()  + '">' +  
      						'<input type="hidden" name="lng" value="' +  marker.getLatLng().lng()  + '">' +  
      						'<br/><input type="submit" name="saveMapLocation" value="Save location" class="buttonField"></form>' + '<div class="line"><!-- --></div>';
             	
             
              GEvent.addListener(marker, "mouseover", function() {
			 	 marker.openInfoWindowHtml(addressWindow);
			  });
			  GEvent.addListener(marker, "click", function() {
			 	 marker.openInfoWindowHtml(addressWindow);
			  });
			 
              map.addOverlay(marker);
              marker.openInfoWindowHtml(addressWindow);
                          
            }
          }
        );
      }
    }
    
 function myEscape(str) {
 
 	return str.toString().replace("'","&singleQuot;");
 }
