//API keys for maps and search are located in themes/templates/pages.ss

//urls to sign up for a new key (required for each new domain name it is registered to)
//http://code.google.com/apis/maps/signup.html
//http://code.google.com/apis/ajaxsearch/signup.html

var app;
var searchptr;
function OnLoad() 
{
	app = new App();
}

function App() 
{
	this.myMap = null;
	this.markerList = new Array();

	// create a map
	this.myMap = new GMap2(document.getElementById("mapDiv"));
	this.myMap.addControl(new GSmallMapControl());
	this.myMap.setCenter(new GLatLng(37.3861, -122.083), 14);

	// Create a search control
	var searchControl = new GSearchControl();

	// Add in a full set of searchers
	var localSearch = new GlocalSearch();
	var options = new GsearcherOptions();
	options.setExpandMode(GSearchControl.EXPAND_MODE_OPEN);
	searchControl.addSearcher(localSearch, options);

	// Set the Local Search center point
	localSearch.setCenterPoint(this.myMap);

	// tell the searcher to draw itself and tell it where to attach
	searchControl.draw(document.getElementById("searchcontrol"));

	// tell the search control to call be on start/stop
	searchControl.setSearchCompleteCallback(this, App.prototype.OnSearchComplete);

	// execute an inital search
	var elm = document.getElementById("myMapPostCode")
	if (elm)
	{
   	searchControl.execute(elm.innerHTML);
   }
}

App.prototype.OnSearchComplete = function(sc, searcher) 
{
	// if we have local search results, put them on the map
	if ( searcher.results && searcher.results.length > 0) 
	{
		for (var i = 0; i < searcher.results.length; i++) 
		{
			var result = searcher.results[i];

			// if this is a local search result, then proceed...
			if (result.GsearchResultClass == GlocalSearch.RESULT_CLASS ) 
			{
				var markerObject = new Object();
				markerObject.result = result;
				markerObject.latLng = new GLatLng(parseFloat(result.lat), parseFloat(result.lng));
				markerObject.gmarker = new GMarker(markerObject.latLng);

				this.markerList.push(markerObject);
				this.myMap.addOverlay(markerObject.gmarker);
				result.__markerObject__ = markerObject;
			}
		}
		this.OnMarkerClick(this.markerList[0]);
	}
}

App.prototype.OnKeep = function(result) {}

App.prototype.OnMarkerClick = function(markerObject) 
{
	var htmlNode = markerObject.result.html.cloneNode(true);
	markerObject.gmarker.openInfoWindow(htmlNode);
}


function method_closure(object, method, opt_argArray) 
{
	return function() 
	{
		return method.apply(object, opt_argArray);
	}
}

GSearch.setOnLoadCallback(OnLoad);