var map;
var gdir;
var geocoder = null;
var addressMarker;

function initialize(to) {
	if (GBrowserIsCompatible()) {      
		map = new GMap2(document.getElementById("map_canvas"));
		gdir = new GDirections(map, document.getElementById("directions"));
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);
		var lat;
		var lng;
		var bubbleText;
		if(to == "event"){
			lat = 39.871935;
			lng = -74.523225;
			bubbleText = "<b>OCCR - Event Sign up.</b>";
		}else{
			lat =40.0118741
			lng = -74.3142657;
			bubbleText = "<b>OCCR meeting spot.</b><br>The Fleet Reserve<br>204 Union Ave<br>Lakehurst, NJ 08733-2934";
		}

		//center map ;
		var center = new GLatLng(lat,lng);
		map.setCenter(center, 14); 
		//center marker
		var marker = createMarker(center,bubbleText);
		//var marker = new GMarker(center,'Some stuff to display in the<br>Second Info Window');
		map.addOverlay(marker); 
		//adding zoom & different view controls
		var mapControl = new GMapTypeControl();
		map.addControl(mapControl);
		map.addControl(new GSmallMapControl());
	}
}
    
function createMarker(point,html) {
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
	  marker.openInfoWindowHtml(html);
	});
	return marker;
}

function setDirections(fromAddress,toAddress) {
	var latlngString = "";
	if(toAddress == "event"){
		latlngString = " to: 39.871935, -74.523225";
	}else{
		latlngString = " to: 40.0118741,-74.3142657";
	}

	// bb forest gdir.load("from: " + fromAddress + " to: 39.871935, -74.523225");
	gdir.load("from: " + fromAddress + latlngString);
}

function handleErrors(){
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS){
		alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
		document.directionsForm.from.select();	   
	}else if (gdir.getStatus().code == G_GEO_SERVER_ERROR){
		alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);	   
		document.directionsForm.from.select();
	}else if (gdir.getStatus().code == G_GEO_MISSING_QUERY){
		alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);   
		document.directionsForm.from.select();
	}else if (gdir.getStatus().code == G_GEO_BAD_KEY){
		alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
		document.directionsForm.from.select();
	}else if (gdir.getStatus().code == G_GEO_BAD_REQUEST){
		alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
		document.directionsForm.from.select();
	}else {
	   alert("Please enter a starting point.");
	   document.directionsForm.from.select();
	}
}

function onGDirectionsLoad(){ 
  // Use this function to access information about the latest load()
  // results.

  // e.g.
  // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
  // and yada yada yada...
}

function clearInput(){
	//document.directionsForm.from.value = "";
	document.directionsForm.from.select();
}
