google.load('search' , '1');

var gLocalSearch;
var gMap;
var gInfoWindow;
var gSelectedResults = [];
var gCurrentResults = [];
var gSearchForm;

// Set up the map and the local searcher.
function OnLoad() {
	// Initialize the local searcher
	gLocalSearch = new GlocalSearch();
	gLocalSearch.setCenterPoint(map.getCenter());
	gLocalSearch.setSearchCompleteCallback(null, OnLocalSearch);
}

function unselectMarkers() {
	for (var i = 0; i < gCurrentResults.length; i++) {
		gCurrentResults[i].unselect();
	}
}

// Called when Local Search results are returned, we clear the old
// results and load the new ones.
function OnLocalSearch() {
	if (!gLocalSearch.results) return;

	gCurrentResults = [];
	for (var i = 0; i < gLocalSearch.results.length; i++) {
		gCurrentResults.push(new LocalResult(gLocalSearch.results[i]));
	}

	var attribution = gLocalSearch.getAttribution();
	if (attribution) {
	//document.getElementById("searchwell").appendChild(attribution);
	}

	/*
	// Move the map to the first result
	var first = gLocalSearch.results[0];
	
	map.setCenter(new google.maps.LatLng(parseFloat(first.lat),
		parseFloat(first.lng)));

	//map.setZoom(12);
	map.setZoom(9);
	*/
}


// A class representing a single Local Search result returned by the
// Google AJAX Search API.
function LocalResult(result) {
	var me = this;
	me.result_ = result;
	me.resultNode_ = me.node();
}

LocalResult.prototype.node = function() {
	if (this.resultNode_) return this.resultNode_;
	return this.html();
};

// Returns the GMap marker for this result, creating it with the given
// icon if it has not already been created.
LocalResult.prototype.marker = function() {

	};

// Unselect any selected markers and then highlight this result and
// display the info window on it.
LocalResult.prototype.select = function() {

	this.selected_ = true;

	gInfoWindow.setContent(this.html(true));
	gInfoWindow.open(map, this.marker());
};

LocalResult.prototype.isSelected = function() {
	return this.selected_;
};

// Remove any highlighting on this result.
LocalResult.prototype.unselect = function() {
	this.selected_ = false;
	this.highlight(false);
};

// Returns the HTML we display for a result before it has been "saved"
LocalResult.prototype.html = function() {
	var me = this;
	var container = document.createElement("div");
	container.className = "unselected";
	container.appendChild(me.result_.html.cloneNode(true));
	return container;
}

LocalResult.prototype.highlight = function(highlight) {
	this.marker().setOptions({
		icon: highlight ? gRedIcon : gYellowIcon
		});
	this.node().className = "unselected" + (highlight ? " red" : "");
}

google.setOnLoadCallback(OnLoad);
