/**
 *  * Class: OpenLayers.Control.Geocoder
 *   * A wrapper around GClientGeocoder.
 *    */
OpenLayers.Control.Geocoder = OpenLayers.Class(OpenLayers.Control, {

		    /**
		     *      * Property: geocoder
		     *           * {GClientGeocoder}
		     *                */
		    geocoder: null,
		        
		        /**
			 *      * Property: clientProj
			 *           * {<OpenLayers.Projection>} The spatial reference for the client (the map).
			 *                */
		        clientProj: null,

			    /**
			     *      * Property: serverProj
			     *           * {<OpenLayers.Projection>} The spatial reference for the geocoding service.
			     *                */
			    serverProj: null,

			        /**
				 *      * Constructor: OpenLayers.Control.Geocoder
				 *           *
				 *                * Parameters:
				 *                     * options - {Object} An optional object with properties to be set on the
				 *                          *     control.
				 *                               */
			        initialize: function(options) {
					        this.serverProj = new OpenLayers.Projection("EPSG:4326");
						        OpenLayers.Control.prototype.initialize.apply(this, [options]);
							        try {
									            this.geocoder = new GClientGeocoder();
										            } catch(err) {
												                OpenLayers.Console.error("GClientGeocoder not available", err);
														        }
								    },
				    
				    /**
				     *      * APIMethod: destroy
				     *           * Clean up the control
				     *                */
				    destroy: function() {
						             this.geocoder = null;
							             OpenLayers.Control.prototype.destroy.apply(this, arguments);
								         },
					         
					         /**
						  *      * Method: setMap
						  *           * Handle whatever needs to be done when control is added to map.
						  *                * 
						  *                     * Parameters:
						  *                          * map - {<OpenLayers.Map>} 
						  *                               */
					         setMap: function(map) {
								         OpenLayers.Control.prototype.setMap.apply(this, [map]);
									         this.clientProj = this.map.projection;
										     },
							     
							     /**
							      *      * Method: setBounds
							      *           * Sets the geocoder to magnify geocoding results within or near the given
							      *                *     bounds.  Note that setting bounds does not restrict results to that
							      *                     *     extent, though it will elevate them in priority.
							      *                          *
							      *                               * Parameters:
							      *                                    * bounds - {<OpenLayers.Bounds>} A bounds for prioritizing results.
							      *                                         */
							     setBounds: function(bounds) {
										        bounds = bounds.clone().transform(this.clientProj, this.serverProj);
											        this.geocoder.setViewport(new GLatLngBounds(
															            new GLatLng(bounds.bottom, bounds.left),
																                new GLatLng(bounds.top, bounds.right)
																		        ));
												    },
									    
									    /**
									     *      * APIMethod: getLocation
									     *           * Geocode an address and call a callback with the resulting location.
									     *                *     If no match is found, callback will receive null.
									     *                     *
									     *                          * Parameters:
									     *                               * address - {String} An address string.
									     *                                    * callback - {Function} A function to be called with the resulting
									     *                                         *     <OpenLayers.LonLat>.  If no match is found, callback will receive
									     *                                              *     null.
									     *                                                   */
									    getLocation: function(address, callback) {
												         if(!callback) {
														             callback = function() {};
															             }
													         var bound = OpenLayers.Function.bind(function(point) {
																             var lonlat = null;
																	                 if(point) {
																			                 var lonlat = new OpenLayers.LonLat(point.lng(), point.lat());
//                lonlat.transform(this.serverProj, this.clientProj);
																						             }
																								                 callback(lonlat);
																										         }, this);

														         this.geocoder.getLatLng(address, bound);
															     },

											     CLASS_NAME: "OpenLayers.Control.Geocoder"
});


