// adapted from http://www.google.com/uds/samples/places.html

function localsearch(map,searchformdiv,searchresultsdiv) {

	// Create our "tiny" marker icon
	this.smallicon=new google.maps.Icon();
	this.smallicon.image=
		"http://labs.google.com/ridefinder/images/mm_20_yellow.png";
	this.smallicon.shadow=
		"http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	this.smallicon.iconSize=new google.maps.Size(12,20);
	this.smallicon.shadowSize=new google.maps.Size(22,20);
	this.smallicon.iconAnchor=new google.maps.Point(6,20);
	this.smallicon.infoWindowAnchor=new google.maps.Point(5,1);

	this.map=map;
	this.searchformdiv=searchformdiv;
	this.searchresultsdiv=searchresultsdiv;
	this.resultmarkers=[];

	// hack
	var	that=this;

	// Initialize the search form
	this.searchform=new google.search.SearchForm(false,this.searchformdiv);
	this.searchform.setOnSubmitCallback(null,function(searchform) {
		// Cancel the form submission,
		// executing an AJAX Search API search.
		that.localsearcher.execute(searchform.input.value);
		return false;
	});

	// Initialize the local searcher
	this.localsearcher=new google.search.LocalSearch();
	this.localsearcher.setCenterPoint(this.map);
	this.localsearcher.setSearchCompleteCallback(null,function() {

		// Called when Local Search results are returned,
		// we clear the old results and load the new ones...
		if (!that.localsearcher.results ||
			!that.localsearcher.results.length) {
			return;
		}

		// clear the old results
		that.clear();

		// create the new set of results
		that.resultmarkers=[];
		for (var i=0; i<that.localsearcher.results.length; i++) {

			// create marker
			var	marker=new resultmarker(
						that.localsearcher.results[i],
						that.smallicon);
			map.addOverlay(marker.marker);
			that.resultmarkers.push(marker);

			// append to search results
			searchresultsdiv.appendChild(marker.html);
		}
	
		// handle the attribution
		var attribution=that.localsearcher.getAttribution();
		if (attribution) {
			that.searchresultsdiv.appendChild(attribution);
		}
	
		// move the map to the first result
		var first=that.localsearcher.results[0];
		that.map.panTo(new google.maps.LatLng(first.lat,first.lng));
	});
}

localsearch.prototype.clear=function() {

	// Clear the old search results
	this.searchresultsdiv.innerHTML="";

	// Clear the map and the old search well
	for (var i=0; i<this.resultmarkers.length; i++) {
		this.map.removeOverlay(this.resultmarkers[i].marker);
	}
}

localsearch.prototype.execute=function(query) {
	this.searchform.execute(query);
}

function resultmarker(result,icon) {
	this.html=document.createElement("div");
	this.html.appendChild(result.html.cloneNode(true));
	this.marker=new google.maps.Marker(new google.maps.LatLng(
						parseFloat(result.lat),
						parseFloat(result.lng)),
						icon);
	this.markerhtml=document.createElement("div");
	this.markerhtml.appendChild(result.html.cloneNode(true));
	var	that=this;
	GEvent.bind(this.marker,"click",this,function() {
		that.marker.openInfoWindow(that.markerhtml);
	});
}
