GeocoderTool = function(id, mapId, tbAddressId, btnSearchId, loadingDivId, method, pushpinId, host, service, searchField){

	this._id = id;
	this._floatingPanel = $find(id);
	this._map = $find(mapId);
	this._tbAddress = $get(tbAddressId);
	this._btnSearch = $get(btnSearchId);
	this._btnSearch.onclick = Function.createDelegate(this, this._search);
	this._loadingDiv = $get(loadingDivId);
	this._method = method;
	this._pushpin = $find(pushpinId);
	this._host = host;
	this._service = service;
	this._searchField = searchField;
};

GeocoderTool.prototype = {
	show : function()
	{
		floatingPanelManager.closeAll();
		floatingPanelManager.open(this._id);
		this._tbAddress.focus();
	},
	
	search: function(address)
	{
		this._execute(address);
	},
	
	_search: function(){
		var address = this._tbAddress.value;
	
		this._btnSearch.style.display = "none";
		this._loadingDiv.style.display = "block";
	
		this._execute(address);
	},
	
	_execute: function(address)
	{
		if(this._method == "Yahoo"){
			MapsDirect.Web.UI.OpenControls.GeocodeService.SearchYahoo(address, this._map.get_spatialReference(), Function.createDelegate(this, this._searchSucceeded), Function.createDelegate(this, this._searchFailed));
		}else if(this._method == "GeocodeServer"){
			MapsDirect.Web.UI.OpenControls.GeocodeService.SearchGeocodeServer(this._host, this._service, this._searchField, address, Function.createDelegate(this, this._searchSucceeded), Function.createDelegate(this, this._searchFailed));
		}
	},
	
	// This is the callback function invoked if the Web service succeeded.
	// It accepts the result object as a parameter.
	_searchSucceeded: function(response, eventArgs)
	{
		this._btnSearch.style.display = "block";
		this._loadingDiv.style.display = "none";
																		  		
		this._map.zoomToScale(2000, {x:response.X, y:response.Y});
		
		var point = new MapsDirect.Geometry.Point(response.X, response.Y);
		
		var html = "<div style=\"width:200px;\">";
		if(response.Address != "") html += response.Address + "<br />";
		if(response.City != "" && response.State != ""){
			html += response.City + ", ";
			html += response.State + " ";
			if(response.Zip != "") html += response.Zip; 
		}else{
			if(response.City != "") html += response.City + "<br />"; 
			if(response.State != "") html += response.State + "<br />";
			if(response.Zip != "") html += response.Zip; 
		} 		
		html += "</div>";
		this._pushpin.show(point, "Address", html, null, null);
	},


	// This is the callback function invoked if the Web service failed.
	// It accepts the error object as a parameter.
	_searchFailed: function(error)
	{
		this._btnSearch.style.display = "block";
		this._loadingDiv.style.display = "none";
		// Display the error.    
		alert(error.get_message());
	}

};
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();