var map;
var geocoder;

function initialize_map(venues, hotels, center, show_estimates) {

  geocoder = new google.maps.Geocoder();
  var primary_venue = "";
  var primary_hotel = "";

  for (i in venues) {
    var address = venues[i][1] + " " +  venues[i][2] + " " + venues[i][3] + " " + venues[i][4] + " " + venues[i][5];
    codeAddress(address, venues[i][0], 'venue', i, center)
    if (i == 0) {
    	primary_venue = address;
    }
  }
  
  for (i in hotels) {
    var address = hotels[i][1] + " " +  hotels[i][2] + " " + hotels[i][3] + " " + hotels[i][4] + " " + hotels[i][5];
    codeAddress(address, hotels[i][0], 'hotel', i, center)
    if (i == 0) {
    	primary_hotel = address;
    }
  }
    
  var latlng = new google.maps.LatLng(39.217615, -76.672798);
  var myOptions = {  
    zoom: 10,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map_id = "map";
  map = new google.maps.Map(document.getElementById(map_id),myOptions);

  if (show_estimates) {
    update_map_distance(primary_venue, primary_hotel);
  }
}
  
function codeAddress( address, label, type, count, center ) {
  var images = new Array();
  images['hotel'] = '/images/hotel.png';   
  images['venue'] = '/images/stadium.png';
        
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      var marker = new google.maps.Marker({
        map: map,  
        position: results[0].geometry.location,
        title: label,
        icon: images[type]
      });
      if (count == 0 && type == center) {
        map.setCenter(results[0].geometry.location);
      }
    }
  });
}

function update_map_distance(primary_venue, primary_hotel) {

  var hotel_lat_long = "";
  var venue_lat_long = "";

  geocoder.geocode( { 'address': primary_venue}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      venue_lat_long = results[0].geometry.location;


			  geocoder.geocode( { 'address': primary_hotel}, function(results, status) {
			    if (status == google.maps.GeocoderStatus.OK) {
			      hotel_lat_long = results[0].geometry.location;
			
						get_map_distance(venue_lat_long, hotel_lat_long, function(distance_info) {
							var duration_value = distance_info.duration.text;
							var distance_value = distance_info.distance.text;
							var estimates = document.getElementById("estimates");
							estimates.innerHTML = distance_value + " / " + duration_value;
						});
			    }
			  });

    }
  });

}

function get_map_distance(primary_venue, primary_hotel, callback) {

  var service = new google.maps.DistanceMatrixService();
  service.getDistanceMatrix(
    {
      origins: [primary_venue],
      destinations: [primary_hotel],
      travelMode: google.maps.TravelMode.DRIVING,
      unitSystem: google.maps.UnitSystem.IMPERIAL,
      avoidHighways: false,
      avoidTolls: false
    }, function(response, status) {
      if (status == google.maps.DistanceMatrixStatus.OK) {
        callback(response.rows[0].elements[0]);
      }
    }
  );
}
