var map;
var gdir;

function initialize() {
  if (GBrowserIsCompatible()) {      
    map = new GMap2(document.getElementById("map"));
    gdir = new GDirections(map, document.getElementById("directions_d"));
    map.setCenter(new GLatLng(-120.639983,35.243178), 11);
	map.addControl(new GLargeMapControl());
 
 // begin map with both offices

	
	

    // Create the marker and corresponding information window
    function createInfoMarker(point, address) {
      var marker = new GMarker(point);
      GEvent.addListener(marker, "click",
      function() {
         marker.openInfoWindowHtml(address);
      }
    );
    return marker;
    }
 
    // Arroyo Grande Office
    var point = new GPoint(-120.60482,35.1367366);
    address = "<strong>Pismo Beach Office</strong><br />901 Oak Park Blvd #202<br />Pismo Beach, CA 93449<br />Phone: (805) ???-????";
    var marker = createInfoMarker(point, address);
    map.addOverlay(marker); 
   // SLO office Address
    var point = new GPoint(-120.640049,35.243093);
    address = "<strong>San Luis Obispo Office</strong><br />895 Aerovista Place, Suite 103 <br />San Luis Obispo, CA 93401<br />Phone: (805) 541-2368";
    var marker = createInfoMarker(point, address);
    map.addOverlay(marker);
	marker.openInfoWindowHtml(address);	
	// end map with both offices
	
    GEvent.addListener(gdir, "error", handleErrors);
  //  GEvent.addListener(gdir, "addoverlay", onGDirectionsAddOverlay); // Triggers marker swap, Esa
    //map.setCenter(new GLatLng(0,0),0);	// inital setCenter()  added by Esa.
    //setDirections("", "", "en_US");
    // api version display added by Esa
   // document.getElementById("api-v").innerHTML = '2.'+G_API_VERSION;
  }
}

function setDirections(fromAddress, toAddress, locale) {
  gdir.load("from: " + fromAddress + " to: " + toAddress,
  { "locale": locale , "getSteps":true});
}

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);
  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);
  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);
  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);
  else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
    alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
  else alert("An unknown error occurred.");
}
