var map = null;
var geocoder = null;

function initialize () {
	if ( GBrowserIsCompatible() ) {        
		map = new GMap2(document.getElementById("map_canvas"));        
		//map.setCenter(new GLatLng(37.4419, -122.1419), 13);
		map.setCenter(new GLatLng(userLatitude, userLongitude), 10);
		map.addControl(new GLargeMapControl());
		geocoder = new GClientGeocoder();
	}    
}

/**
 * show an address on the map
 */
function showAddress ( address, name, phone, latitude, longitude ) {
	if (geocoder) {
		geocoder.getLatLng ( address,
			function ( point ) {
				if ( !point ) {
					//alert("Sorry, that address cannot be found.");  //address
				} else {
					//document.getElementById("map_canvas").focus();  // bring the map into view
					map.setCenter(point, 8);
					var marker = new GMarker(point);
					map.addOverlay(marker);
					marker.openInfoWindowHtml("<strong>"+name+"</strong><br/>"+address+"<br/>"+phone);
				}
			}
		);					
	} 
}

/**
 * record an address' lat and long in the datafile
 */
function recordAddress ( address, name, phone, instructor ) {
	if (geocoder) {
		geocoder.getLatLng ( address,
			function ( point ) {
				if ( !point ) {
					//alert("Sorry, that address cannot be found.");  //address
				} else {
					map.setCenter(point, 8);
					var marker = new GMarker(point);
					map.addOverlay(marker);
					marker.openInfoWindowHtml("<strong>"+name+"</strong><br/>"+address+"<br/>"+phone);
					
					// call a script to record the latitude and longitude
					var latLong = marker.getLatLng();
						
					// save the score	
					xmlHttp=GetXmlHttpObject();
					if (xmlHttp != null) {
						var url = "directoryUpdate.php?instructor="+instructor+"&latLong="+latLong;
						//xmlHttp.onreadystatechange = stateChanged;
						xmlHttp.open("GET", url, true);
						xmlHttp.send(null);
						//return true;
					} //return false;
				}
			}
		);					
	} 
}






var xmlHttp;
/**
 * AJAX function
 */
function GetXmlHttpObject() {
	var xmlHttp=null;
	try { // Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) {  // Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	} 
	return xmlHttp;
}
			