function SearchControl(callback_) {
	this.callback = callback_;
}

SearchControl.prototype = new GControl();

SearchControl.prototype.initialize = function(map_) {
	this.geocoder;
	this.map;
	this.searchInput;
	
	this.map = map_;
	this.geocoder = new GClientGeocoder();
	
  container = document.createElement("div");
	box = document.createElement("div");
  box.style.color = "#979797";
  box.style.backgroundColor = "white";
  box.style.border = "1px solid #bccdf0";
  box.style.padding = "1px";
	
  table = document.createElement("table");
	row = table.insertRow(0);
	cell0 = row.insertCell(0);
	cell1 = row.insertCell(1);

  this.searchInput = document.createElement("input");
  this.searchInput.type = "text";

	this.searchInput.onkeypress = this.search;
	this.searchInput.searchControlInstance = this; // ------------------(1)
	
  this.setInputStyle(this.searchInput);
  cell0.appendChild(this.searchInput);

  searchDiv = document.createElement("div");
  this.setButtonStyle(searchDiv);
  cell1.appendChild(searchDiv);
  searchDiv.appendChild(document.createTextNode("Procurar"));
	
	var searchControlInstance = this;	// Just to function (closure) in GEvent get access 
  GEvent.addDomListener(searchDiv, "click", function() {
		searchControlInstance.geocodeSearch();
  });

  box.appendChild(table);
  container.appendChild(box);

  this.map.getContainer().appendChild(container);
  return container;
}

SearchControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(2, 15));
}

SearchControl.prototype.geocodeSearch = function() {
	callback = this.callback;				// Just to function (closure) in this.geocoder.getLatLng get access 
	address = this.searchInput.value;
	map = this.map;

	if (this.geocoder) {
		this.geocoder.getLatLng(
			address,
		  function(point) {
		    if (!point) {
		      alert(address + "n\u00e3o foi encontrado.\n\nDescreva com mais detalhes\nEx: Rua Alice, 278, Rio de Janeiro \n\nou procure por algum lugar próximo e MOVA a marca até o local desejado.");
		    } else {
					if(this.callback == undefined){
		      	map.setCenter(point, 13);
					}	else {
						callback(address, point);
					}
		    }
		  }
		);
	}
}

SearchControl.prototype.search = function(e) {
	var characterCode;
	if(e && e.which){
		e = e;
		characterCode = e.which;
	} else {
		e = event;
		characterCode = e.keyCode;
	}
	if(characterCode == 13){ // if generated character code is equal to ascii 13 (if enter key)
		this.searchControlInstance.geocodeSearch(); // this here means seachInput. Now is possible call geocodeSearch() because (1).
		return false;
	}	else {
		return true;
	}
}

SearchControl.prototype.setInputStyle = function(input) {
	input.align = "left";
  input.style.border = "1px solid #979797";
	input.style.width = "230px";
	input.style.height = "13px";
	input.style.fontSize = "11px";
}

SearchControl.prototype.setButtonStyle = function(button) {
  button.style.color = "#fff";
  button.style.backgroundColor = "#f58220";
  button.style.font = "11px Arial";
  button.style.fontWeight = "bold";
  button.style.border = "1px solid #f6be8e";
  button.style.padding = "2px";
  button.style.marginBottom = "3px";
  button.style.textAlign = "center";
  button.style.width = "5em";
  button.style.cursor = "pointer";
}
