﻿var map;
var geocoder;
var directionsService;
var directionsDisplay;
var markers;
var bounds;


/***
 * initMap
 *    Initializes the Google map for the location.
 ***/
function initMap()
{
   if (undefined == window.addresses) {
      initSingleLocation();
   } else if (window.addresses.length && window.addresses.length > 0) {
      initMultiLocation();
   }
}

/***
 * initMultiLocation
 *    Initializes the Google map for multiple locations.
 ***/
function initMultiLocation()
{
   var address = null;
   var content = null;

   markers = 0;
   bounds = new google.maps.LatLngBounds();

   for (var i = 0; i < window.addresses.length; i++) {
      address = window.addresses[i];
      content = document.getElementById(address.info).innerHTML.replace(/(\W)dest(\W)/g, '$1destination$2');

      if (undefined == address.lat || undefined == address.lng) {
         if (null == geocoder) {
            geocoder = new google.maps.Geocoder();
         }
         geocoder.geocode({
               address: address.address
            },
            function(results, status) {
               if (status == google.maps.GeocoderStatus.OK) {
                  if (null == map) {
                     buildMap(results[0].geometry.location);
                  }
                  addMarker(results[0].geometry.location, content);
               }
            });
      } else {
         var latlng = new google.maps.LatLng(address.lat, address.lng);
         if (null == map) {
            buildMap(latlng);
         }
         addMarker(latlng, content);
      }
   }

   if (markers == 0) {
      document.getElementById('map_canvas').style.display = 'none';
   } else {
      map.fitBounds(bounds);
   }
}

/***
* initSingleLocation
*    Initializes the Google map for a single location.
***/
function initSingleLocation()
{
   var content = document.getElementById('info').innerHTML.replace(/(\W)dest(\W)/g, '$1destination$2');
   if (undefined == window.lat || undefined == window.lng) {
       if (undefined != window.address) {
         geocoder = new google.maps.Geocoder();
         geocoder.geocode({
               address: address
            },
            function(results, status) {
               if (status == google.maps.GeocoderStatus.OK) {
                  buildMap(results[0].geometry.location);
                  addMarker(results[0].geometry.location, content);
               } else {
                  document.getElementById('map_canvas').style.display = 'none';
               }
            });
      } else {
         document.getElementById('map_canvas').style.display = 'none';
      }
   } else {
      var latlng = new google.maps.LatLng(lat, lng);
      buildMap(latlng);
      addMarker(latlng, content);
   }
} // end initMap

/***
 * buildMap
 *    Builds the map for the location.
 *
 * @param latlng the geocoded LatLng object for the location's address
 ***/
function buildMap(latlng)
{
   map = new google.maps.Map(document.getElementById("map_canvas"), {
         center: latlng,
         disableDefaultUI: true,
         mapTypeId: google.maps.MapTypeId.ROADMAP,
         navigationControl: true,
         navigationControlOptions: {
               style: google.maps.NavigationControlStyle.SMALL
            },
         scrollwheel: false,
         zoom: 10
      });

   directionsService = new google.maps.DirectionsService();
   directionsDisplay = new google.maps.DirectionsRenderer();
   directionsDisplay.setMap(map);
   directionsDisplay.setPanel(document.getElementById('directions'));
}

/***
 * addMarker
 *    Adds a marker to the specified location.
 * 
 * @param latlng the geocoded LatLng object for the location's address
 * @param content the HTML content to put into the marker's info window
 ***/
function addMarker(latlng, content)
{
   var marker = new google.maps.Marker({
         map: map,
         position: latlng
      });
      
   var infoWindow = new google.maps.InfoWindow({
         content: content
      });
      
   google.maps.event.addListener(marker, 'click', function() {
         infoWindow.open(map, marker);
      });

   markers++;
   if (bounds) {
      bounds.extend(latlng);
   }
} // end buildMap


/***
 * getDirections
 *    Gets directions to the location from the user input.
 *
 * @param index the index into the addresses array
 ***/
function getDirections(index)
{
   var dest;

   if (null == index) {
      if (undefined == window.lat || undefined == window.lng) {
         if (undefined != window.address) {
            dest = address;
         }
      } else {
         dest = new google.maps.LatLng(lat, lng);
      }
   } else if (undefined != window.addresses && window.addresses.length > index) {
      var address = window.addresses[index];
      if (undefined == address.lat || undefined == address.lng) {
         if (undefined != address.address) {
            dest = address;
         }
      } else {
         dest = new google.maps.LatLng(address.lat, address.lng);
      }
   }

   if (dest) {
      directionsService.route({
            origin: document.getElementById('destination').value,
            destination: dest,
            travelMode: google.maps.DirectionsTravelMode.DRIVING
         },
         function(results, status) {
            var div = document.getElementById('directions');
            if (status == google.maps.DirectionsStatus.OK && div) {
               div.innerHTML = '';
               directionsDisplay.setDirections(results);
            }
            return false;
         });
   } else {
      alert('Missing destination latitude/longitude or address.');
   }
      
   return false;
} // end getDirections
