tx_decgmaps_addressLookup = {
	ref: null,
	config: null,
	
	geocoder: null,
	search: "",
	marker: null,
	
	init: function(){
		this.geocoder = new GClientGeocoder();
		
		var mod = this;
		jQuery("#"+this.ref.mapId+"-makeAddressLookup").click(function(){
			mod.findLocation();
			return false;
		});
	},
	
	findLocation: function(){
		var adrParts = [];
		var val;
		var hasZip = false;
		
		if(this.config.showAdr){
			val = jQuery("#"+this.ref.mapId+"-addressField").val();
			if(val != "") adrParts.push(val);
		}

		if(this.config.showZip){
			val = jQuery("#"+this.ref.mapId+"-zipField").val();
			if(val != "") {
				adrParts.push(val);
				hasZip = true;
			}
		}

		if(this.config.showCity){
			val = jQuery("#"+this.ref.mapId+"-cityField").val();
			if(val != ""){
				if(hasZip){
					adrParts[adrParts.length-1] += " " + val;
				} else adrParts.push(val);
			}
		}

		if(this.config.showCountry){
			val = jQuery("#"+this.ref.mapId+"-countryField").val();
			if(val != "") adrParts.push(val);
		}
		
		this.search = adrParts.join(", ");
		
		if(this.search == ""){
			this.ref.gmap.removeOverlay(this.marker);
			this.ref.gmap.setCenter(new GLatLng(parseFloat(this.ref.config.defaut.lat), parseFloat(this.ref.config.defaut.lng)), parseInt(this.ref.config.defaut.zoom));
			this.ref.mng.refresh();
		} else {	
			var mod = this;
			this.geocoder.getLatLng(this.search, function(point){
				mod.setPoint(point);
			});
		}
	},
	
	
	setPoint: function(point){
		if(point){
			this.marker = new GMarker(new GLatLng(point.lat(), point.lng()), {clickable: false, title: this.search});
			this.ref.gmap.addOverlay(this.marker);
			this.ref.gmap.setCenter(new GLatLng(point.lat(), point.lng()), parseInt(this.config.zoomLevel));
			this.ref.mng.refresh();
		} else this.showError(eval(this.ref.LL.address_notfound));
	},
	
	showError: function(msg){
		window.alert(msg);
	}
}
