// JavaScript Document
var map = null;
var geocoder = null;
var gdir;
var addressMarker;
var mapCenterLat=37.4419;
var mapCenterLong=-122.1419;
var mapDefaultView='map';
var geoXml;
var kmzFile="";

var zoomlevel = 1;

function initialize() {
  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map_canvas"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(mapCenterLat, mapCenterLong), zoomlevel);
	if(mapDefaultView=='hybrid') map.setMapType(G_HYBRID_MAP);
        else if(mapDefaultView=='satellite') map.setMapType(G_SATELLITE_MAP);
        
	if(kmzFile=="")
	{
		geocoder = new GClientGeocoder();
		gdir = new GDirections(map, document.getElementById("directions"));
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors); 
	}
	else
	{
		geoXml = new GGeoXml(kmzFile);
		map.addOverlay(geoXml);
	}
		// Add 10 markers to the map at random locations
        /*var bounds = map.getBounds();
        var southWest = bounds.getSouthWest();
        var northEast = bounds.getNorthEast();
        var lngSpan = northEast.lng() - southWest.lng();
        var latSpan = northEast.lat() - southWest.lat();
        for (var i = 0; i < 10; i++) {
          var point = new GLatLng(southWest.lat() + latSpan * Math.random(),
                                  southWest.lng() + lngSpan * Math.random());

alert(point);
          map.addOverlay(new GMarker(point));
        }*/
  }
}

function addMarker(lat, long, popup_info, alertAddressNotFound, openPopup, customIcon, centerMap) {
	if(!customIcon) customIcon="";
	if(!centerMap) centerMap=false;
	var point = new GLatLng(lat, long);
	if(centerMap==true) map.setCenter(point, zoomlevel);
	
	if(customIcon!="")
	{
		var blueIcon = new GIcon(G_DEFAULT_ICON);
		blueIcon.image = customIcon;
		blueIcon.iconSize = new GSize(30, 25);
		blueIcon.shadow = "";
		markerOptions = { icon:blueIcon };
		var marker = new GMarker(point, markerOptions);
	}
	else 
	{
		var marker = new GMarker(point);
	}

	map.addOverlay(marker);
	GEvent.addListener(marker,"click", function() {
		//var myHtml = "<b>#" + number + "</b><br/>" + message[number -1];
		map.openInfoWindowHtml(point, popup_info);
  	});
  	if(openPopup==true) marker.openInfoWindowHtml(popup_info);
}

function showAddress(address, popup_info, alertAddressNotFound, openPopup, centerMap) {
  if(!centerMap) centerMap=false;
  if (geocoder) {
	geocoder.getLatLng(
	  address,
	  function(point) {
		if (!point && alertAddressNotFound==true) {
		  alert(address + " not found");
		} else {
		  if(centerMap==true) map.setCenter(point, zoomlevel);
		  var marker = new GMarker(point);
		  map.addOverlay(marker);
		  GEvent.addListener(marker,"click", function() {
			//var myHtml = "<b>#" + number + "</b><br/>" + message[number -1];
			map.openInfoWindowHtml(point, popup_info);
		  });
		  if(openPopup==true) marker.openInfoWindowHtml(popup_info);
		}
	  }
	);
  }
}

function setDirections(fromAddress, toAddress, locale) {
	gdir.load("from: " + fromAddress + " to: " + toAddress,{ "locale": locale });
}
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.");
	   
}

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 printDirections(from, to) {
	window.open ("print-directions.php?from=" + from + "&to=" + to, "printmap","location=0,status=0,scrollbars=1, width=650,height=400"); 
}