
    // Our global state
    var gLocalSearch;
//    var gMap;
    var gSelectedResults = [];
    var gCurrentResults = [];

    // Create our "tiny" marker icon
    var gSmallIcon = new GIcon();
    gSmallIcon.image = "http://labs.google.com/ridefinder/images/mm_20_yellow.png";
    gSmallIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    gSmallIcon.iconSize = new GSize(12, 20);
    gSmallIcon.shadowSize = new GSize(22, 20);
    gSmallIcon.iconAnchor = new GPoint(6, 20);
    gSmallIcon.infoWindowAnchor = new GPoint(5, 1);

    // Set up the map and the local searcher.
    function initSearch() {
//      var input = document.getElementById("q");
//      input.focus();

      // Initialize the map
//      gMap = new GMap(document.getElementById("map"));
//      gMap.addControl(new GSmallMapControl());
//      gMap.addControl(new GMapTypeControl());
//      gMap.setCenter(new GLatLng(37.4419, -122.1419), 13);

      // Initialize the local searcher
      gLocalSearch = new GlocalSearch();
      gLocalSearch.setCenterPoint(gmap.map);
      gLocalSearch.setSearchCompleteCallback(null, OnLocalSearch);

      // Execute the initial search
//      gLocalSearch.execute(input.value);
    }

    // Called when Local Search results are returned, we clear the old
    // results and load the new ones.
    function OnLocalSearch() {
      hideLoadingMessage();
      if (!gLocalSearch.results) return;
      if (gLocalSearch.results.length==0) return;
      var searchWell = document.getElementById("searchwell");
      searchWell.style.display = 'block';

      // Clear the map and the old search well
      searchWell.innerHTML = '<div style="width:100%;text-align:right"><a id="search_close" href="javascript:;" onclick="this.parentNode.parentNode.style.display=\'none\';">Close</a></a></div>';
      var fun = function() {
	_gi('searchwell').style.display = 'none';
      }
//      _gi('search_close').addEventListener('click', fun, true);
      MyAddEventListener( _gi('search_close'), 'click', fun, true);

      for (var i = 0; i < gCurrentResults.length; i++) {
        if (!gCurrentResults[i].selected()) {
          gmap.map.removeOverlay(gCurrentResults[i].marker());
        }
      }

      gCurrentResults = [];
      for (var i = 0; i < gLocalSearch.results.length; i++) {
        gCurrentResults.push(new LocalResult(gLocalSearch.results[i]));
      }

      // move the map to the first result
      var first = gLocalSearch.results[0];
//      gmap.map.recenterOrPanToLatLng(new GPoint(parseFloat(first.lng), parseFloat(first.lat)));
      gmap.map.panTo(new GLatLng(first.lat, first.lng));

    }

    // Cancel the form submission, executing an AJAX Search API search.
    function CaptureForm() {
      showLoadingMessage('Searching locations ...');
      gLocalSearch.execute(_gi('q').value);
      return false;
    }


    // A class representing a single Local Search result returned by the
    // Google AJAX Search API.
    function LocalResult(result) {
      this.result_ = result;
//      this.result_.html = _ce('div');
//      this.result_.html.innerHTML = this.result_.title;
// alert(this.result_.html.innerHTML);
      var title = this.result_.html.getElementsByTagName('a')[0];
      title.href = 'javascript:;';
      title.target = '';

      this.resultNode_ = this.unselectedHtml();
      _gi("searchwell").appendChild(_ce('hr'));
      _gi("searchwell").appendChild(this.resultNode_);
      this.marker_ = this.marker(gSmallIcon);
      var x_marker = this.marker_;
      gmap.map.addOverlay(this.marker_);

      this.resultNode_.marker = this.marker_;
      this.resultNode_.result = this;
      MyAddEventListener (this.resultNode_, 'click', function() {
	  var result = this.result;
//	  this.marker.openInfoWindow(result.selected() ? result.selectedHtml() : result.unselectedHtml());
	  x_marker.openInfoWindow(result.selected() ? result.selectedHtml() : result.unselectedHtml());
      }, true);
    }

    // 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.selected() ? this.selectedHtml() :
                                                this.unselectedHtml());
      });
      this.marker_ = 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
        gmap.map.removeOverlay(this.marker());
        this.marker_ = null;
        gmap.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.unselectedHtml = function() {
      var container = document.createElement("div");
      container.className = "unselected";
      container.appendChild(this.result_.html.cloneNode(true));
//      container.innerHTML = _gi('q').value;
      var saveDiv = document.createElement("div");
      saveDiv.className = "select";
      saveDiv.innerHTML = "Save this location";
      GEvent.bindDom(saveDiv, "click", this, function() {
        gmap.map.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_;
    }

// initSearch();
