var geocoder = null;
var map = null;

/**
 * Displays all the predefined locations on the map with a red marker.  Displays the address
 * of each point when clicked on.
 */
function load() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(21.292703214288892, -157.84148812294006), 15);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    geocoder = new GClientGeocoder();
    
    // Creates a marker at the given point and displays info when clicked on
    function createMarker(latlng, infoToDisplay) {
      var marker = new GMarker(latlng);
      GEvent.addListener(marker,"click", function() {
        map.openInfoWindowHtml(latlng, infoToDisplay);
      });
      return marker;
	}
	
	var infoToDisplay = "<b>DataHouse</b><br/>1534 Kapiolani Blvd<br/>Honolulu, HI 96814";
	map.addOverlay(createMarker(new GLatLng(21.292703214288892, -157.84148812294006), infoToDisplay));
	
    var infoToDisplay = "<b>Don Quijote</b><br/>801 Kaheka St<br/>Honolulu, HI 96814";
	map.addOverlay(createMarker(new GLatLng(21.29447257682827, -157.83896684646606), infoToDisplay));
	
  }
}

/**
 * Displays the map with a marker to represent the polling place the user selected.
 */
function load2() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map2"));
    map.setCenter(new GLatLng(21.292703214288892, -157.84148812294006), 15);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    geocoder = new GClientGeocoder();
    
    // Creates a marker at the given point and displays info when clicked on
    function createMarker(latlng, infoToDisplay) {
      var marker = new GMarker(latlng);
      GEvent.addListener(marker,"click", function() {
        map.openInfoWindowHtml(latlng, infoToDisplay);
      });
      map.openInfoWindowHtml(latlng, infoToDisplay);
      return marker;
	}
	
	// Get the address and polling place text from the HTML page
	var address = document.getElementById("address").value;
	var pollingPlace = document.getElementById("polling_place").value;
	var display = "<strong>" + pollingPlace + "</strong><br />" + address;
	
    if (geocoder) {
      geocoder.getLatLng(
        address,
        function(point) {
        if (!point) {
          alert(address + " not found - load2");
        }
        else {
          // Set the center of the map to the polling place
          map.setCenter(point, 15);
          // Create the marker on the map to represent the polling place
          map.addOverlay(createMarker(point, display));
        }
      }
    );
  }
  }
}

/**
 * Displays the map with a marker to represent the polling place the user selected.
 */
function showHome() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map2"));
    map.setCenter(new GLatLng(21.292703214288892, -157.84148812294006), 15);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    geocoder = new GClientGeocoder();
    
    // Creates a marker at the given point and displays info when clicked on
    function createMarker(latlng, infoToDisplay) {
      var marker = new GMarker(latlng);
      GEvent.addListener(marker,"click", function() {
        map.openInfoWindowHtml(latlng, infoToDisplay);
      });
      map.openInfoWindowHtml(latlng, infoToDisplay);
      return marker;
	}
	
	// Get the address and polling place text from the HTML page
	//var address = document.getElementById("address").value;
	//var pollingPlace = document.getElementById("polling_place").value;
	var address = "1704 WAIOLA ST, 96826";
	var pollingPlace = "SPALDING CLUBHOUSE GYM";
    var home = document.getElementById("home").value;
	var display = "<strong>" + pollingPlace + "</strong><br />" + address;
	
	alert(address);
	
	showAddress(home);
	
    if (geocoder) {
      geocoder.getLatLng(
        address,
        function(point) {
        if (!point) {
          alert(address + " not found - load2");
        }
        else {
          // Set the center of the map to the polling place
          map.setCenter(point, 15);
          // Create the marker on the map to represent the polling place
          map.addOverlay(createMarker(point, display));
        }
      }
    );
  }
  }
}

/**
 * Takes an address and displays it on the map (in blue) if it is a valid address.
 */
function showAddress() {
  var address = document.getElementById("address").value;

  // Make the marker blue
  var blueIcon = new GIcon(G_DEFAULT_ICON);
  blueIcon.image = "http://gmaps-samples.googlecode.com/svn/trunk/markers/blue/blank.png";
  markerOptions = { icon:blueIcon };
  
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert(address + " not found");
        }
        else {
          map.setCenter(point, 15);
          var marker = new GMarker(point, markerOptions);
          map.addOverlay(marker);
          marker.openInfoWindowHtml(address);
        }
      }
    );
  }
}

