﻿    // Our global state
    var gLocalSearch;
    var gSelectedResults = [];
    var gCurrentResults = [];
    var gSearchForm;

    // Create our "tiny" marker icon
    var gSmallIcon = new GIcon();
    gSmallIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
    gSmallIcon.shadow = null;
    gSmallIcon.iconSize = new GSize(12, 20);
    gSmallIcon.shadowSize = new GSize(22, 20);
    gSmallIcon.iconAnchor = new GPoint(6, 20);
    gSmallIcon.infoWindowAnchor = new GPoint(5, 1);
    
    
    function doSearch() {
      //var zip = document.getElementById("zipInput").value;
      var cat = document.getElementById("catInput").value;
      gLocalSearch.setCenterPoint(gmap);
      gLocalSearch.execute(cat);
    }
    
    function myclick_result(id) {
      for (var i = 0; i < gCurrentResults.length; i++) {
        if(id === i) {
          gCurrentResults[i].marker_.openInfoWindow(gCurrentResults[i].result_.html.innerHTML);
        }
      }
    }
    
    function clearMapSearch() {
     var searchWell = document.getElementById("searchwell");

      // Clear the map and the old search well
      searchWell.innerHTML = "";
      for (var i = 0; i < gCurrentResults.length; i++) {
          gmap.removeOverlay(gCurrentResults[i].marker());
      }
    }

    // Called when Local Search results are returned, we clear the old
    // results and load the new ones.
    function OnLocalSearch() {
      if (!gLocalSearch.results) return;
      var searchWell = document.getElementById("searchwell");

      // Clear the map and the old search well
      searchWell.innerHTML = "";
      for (var i = 0; i < gCurrentResults.length; i++) {
          gmap.removeOverlay(gCurrentResults[i].marker());
      }

      gCurrentResults = [];
      for (var i = 0; i < gLocalSearch.results.length; i++) {
        gCurrentResults.push(new LocalResult(gLocalSearch.results[i]));
      }

//      var attribution = gLocalSearch.getAttribution();
//      if (attribution) {
//        document.getElementById("searchwell").appendChild(attribution);
//      }
      
      var sHTML = "";
      sHTML = "<table style='width:350px;height:100%;white-space:nowrap;'>";
      
      for (var i = 0; i < gLocalSearch.results.length; i++) {
          sHTML = sHTML + "  <tr style='height:20px;'>";
          sHTML = sHTML + "    <td>";
          sHTML = sHTML + "      <a href='#' style='padding-left:5px;color:#395036;text-decoration:none;font-size:12px;font-family:Verdana;' onclick='javascript:myclick_result(" + i + ");return false;' >" + gLocalSearch.results[i].titleNoFormatting.replace("'", "&#39;") + "</a>";
          sHTML = sHTML + "    </td>";
          sHTML = sHTML + "  </tr>"; 
      }
      
      sHTML = sHTML + "  <tr style=''>";
      sHTML = sHTML + "    <td>";
      sHTML = sHTML + "      &#160;";
      sHTML = sHTML + "    </td>";
      sHTML = sHTML + "  </tr>"; 
      sHTML = sHTML + "</Table>";
      
      document.getElementById("searchwell").innerHTML = sHTML;

      // move the map to the first result
      var first = gLocalSearch.results[0];
      //gmap.panTo(new GPoint(parseFloat(first.lng), parseFloat(first.lat)));

    }

    // Cancel the form submission, executing an AJAX Search API search.
    function CaptureForm(searchForm) {
      gLocalSearch.execute(searchForm.input.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.resultNode_ = this.unselectedHtml();
      //document.getElementById("searchwell").appendChild(this.resultNode_);
      gmap.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.result_.html.innerHTML);
      });
      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.removeOverlay(this.marker());
//        this.marker_ = null;
//        gmap.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));
//      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_;
//    }

    //GSearch.setOnLoadCallback(OnLoad);

