// JavaScript Document

/*
This code presents a Google Map with a readout for the lat and lng for the center of the map,
and forms for geocoding and reverse geocoding.
*/

  var geocoder;
  var map;
  var zoomLevel;
  var centerMarker;
  var infowindow = new google.maps.InfoWindow();
  var centerChangedLast;
  var reverseGeocodedLast;
  var currentReverseGeocodeResponse;

  
function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(47.576, -122.275772);
    var myOptions = {
      zoom: 11,
      center: latlng,
      mapTypeId: 'terrain',
	  scrollwheel: false
    }

    map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
    document.getElementById("CenterLatLng").innerHTML = "Center Latitude = 47.576000, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Center Longitude = -122.275772, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Zoom = 11" ;
    geocoder = new google.maps.Geocoder();

	initElevation();
	addListeners()
}

function addListeners() {

  google.maps.event.addListener(map, 'center_changed', function() {
	setLatLngZoom();
  });
	
  google.maps.event.addListener(map, 'zoom_changed', function() {
	setLatLngZoom();
  });
}

function getCenterLatLngText() {
    return '(' + map.getCenter().lat() +', '+ map.getCenter().lng() +')';
}

function reverseGeocode() {
    geocoder.geocode({latLng:map.getCenter()},reverseGeocodeResult);
}

function reverseGeocodeResult(results, status) {
    currentReverseGeocodeResponse = results;
    if(status == 'OK') {
      if(results.length == 0) {
        document.getElementById('formatedAddress').innerHTML = 'None';
      } else {
       document.getElementById('formatedAddress').innerHTML = results[0].formatted_address;
      }
    } else {
      document.getElementById('formatedAddress').innerHTML = 'Error';
    }
}


										  
function setLatLngZoom() {
	centerLatLng = map.getCenter();
    zoomLevel = map.getZoom();
    document.getElementById("CenterLatLng").innerHTML = "Center Latitude = " + centerLatLng.lat().toFixed(6) +", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Center Longitude = " + centerLatLng.lng().toFixed(6) + ", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Zoom = " + zoomLevel;
    if (zoomLevel == 0) {
      map.setZoom(10);
    }
    document.getElementById('formatedAddress').innerHTML = '';
}

function initElevation() {
  // Create an ElevationService
  elevator = new google.maps.ElevationService();

  // Add a listener for the click event and call getElevation on that location
  google.maps.event.addListener(map, 'click', getElevation);
}

function getElevation(event) {

  var locations = [];

  // Retrieve the clicked location and push it on the array
  var clickedLocation = event.latLng;
  locations.push(clickedLocation);

  // Create a LocationElevationRequest object using the array's one value
  var positionalRequest = {
    'locations': locations
  }

  // Initiate the location request
  if (elevator) {
    elevator.getElevationForLocations(positionalRequest, function(results, status) {
      if (status == google.maps.ElevationStatus.OK) {

        // Retrieve the first result
        if (results[0]) {

          // Open an info window indicating the elevation at the clicked position
          infowindow.setContent("The elevation at this point is " + (3.28084*results[0].elevation).toFixed(1) + " feet.");
          infowindow.setPosition(clickedLocation);
          infowindow.open(map);
        } else {
          alert("No results found");
        }
      } else {
        alert("Elevation service failed due to: " + status);
      }
    });
  }
}

  function codeAddress() {
    var address = document.getElementById("address").value;
    if (geocoder) {
      geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          map.setCenter(results[0].geometry.location);
          var marker = new google.maps.Marker({
              map: map, 
              position: results[0].geometry.location
          });
        } else {
          alert("Geocode was not successful for the following reason: " + status);
        }
      });
    }
  }


// The following functions pan and zoom to specific locations when activated by a click on a hypertext link at the bottom of the page.
function HouseDetail(){
    var latlng = new google.maps.LatLng(47.4992, -122.2831);
    var myOptions = {
      zoom: 17,
      center: latlng,
      mapTypeId: 'terrain'}
   map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
	initElevation();
	addListeners();
	setLatLngZoom();
}

function OfficeDetail()
{
	var latlng = new google.maps.LatLng(47.676696, -122.096198);
    var myOptions = {
	  zoom: 17,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.HYBRID}
   map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
	addListeners();
	setLatLngZoom();
}

function DowntownMap()
{
	var latlng = new google.maps.LatLng(47.605237, -122.330017);
    var myOptions = {
	  zoom: 13,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.HYBRID}
   map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
	addListeners();
	setLatLngZoom();
}

function AreaMap()
{
	var latlng = new google.maps.LatLng(47.576, -122.275772);
    var myOptions = {
	  zoom: 11,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.HYBRID}
   map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
	addListeners();
	setLatLngZoom();
}

function BucharestMap()
{
	var latlng = new google.maps.LatLng(44.429612, 26.104202);
    var myOptions = {
	  zoom: 11,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.HYBRID}
   map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
	addListeners();
	setLatLngZoom();
}

