// A SearchControl is a GControl that displays and edit box and search 
// in the maps for a place in  Google Maps).

// We define the function first
function SearchControl() {
}

// To "subclass" the GControl, we set the prototype object to
// an instance of the GControl object
SearchControl.prototype = new GControl();

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
SearchControl.prototype.initialize = function(map) {
  
  var container = document.createElement("div");
  this.setButtonStyle_(container);
    
  var searchBox = document.createElement("input");
  searchBox.setAttribute("type","text");
  searchBox.setAttribute("value","Search for a place");
  searchBox.setAttribute("id","GControl_Search");
  container.appendChild(searchBox);
  
  var searchButton = document.createElement("input");
  searchButton.setAttribute("type","button");
  searchButton.setAttribute("value","Search");
  container.appendChild(searchButton);
  
          
  GEvent.addDomListener(searchBox, "change", this.showAddress);
  GEvent.addDomListener(searchButton, "click", this.showAddress);
    
  map.getContainer().appendChild(container);
    
  return container;
}

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
SearchControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(75, 7));
}

// Sets the proper CSS for the given button element.
SearchControl.prototype.setButtonStyle_ = function(button) {  
  button.style.color = "#000000";
  button.style.backgroundColor = "white";
  button.style.font = "small Arial";
  button.style.border = "1px solid black";
  button.style.padding = "2px";
  button.style.marginBottom = "3px";
  //button.style.textAlign = "center";
  //button.style.width = "6em";
  //button.style.cursor = "pointer";
}



SearchControl.prototype.showAddress = function() {  
  var geocoder = new GClientGeocoder();
  var address = document.getElementById("GControl_Search").value; 
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
        map.setCenter(point, 13);
        var marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(address);
      }
    }
  );
}