
/**************************************************/
/* Functions to interact with the Google Maps API */
/**************************************************/

var geocoder;
var map;
var addressesNotFound = new Array();

//Called at page load complete time - initialise map
function load(cenLat,cenLong) {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    geocoder = new GClientGeocoder();
    //Set centre of map
    map.setCenter(new GLatLng(cenLat,cenLong), 14);
    //Call the function to loop round the stockist data and show markers
    showStockistLocations();
    //Add a zoom control and a map type menu
    map.addControl(new GLargeMapControl());
    map.addControl(new GMenuMapTypeControl());
  }
}

//Loop round the stockist data and show markers
function showStockistLocations() {
  for(var i = 0; i < stockists.length; i++) {
    var thisAddress = stockists[i].address_1;
    thisAddress += "," + stockists[i].postcode;
    thisAddress += "," + stockists[i].country;
    //No lat/long point saved for this stockist yet
    if(stockists[i].latitude == "0") {
      //No lat/long coord for this stockist - try to find the address
      //alert("Latitude for stockist " + stockists[i].stockistName + " is 0!");
      geocoder.getLocations(thisAddress,showStockistAddress);
    } else {
      var point = new GLatLng(parseFloat(stockists[i].latitude),parseFloat(stockists[i].longitude));

      //Create the marker on the map

      //Include name and address, phone number
      var html = "<h4>" + stockists[i].stockistName + "</h4>";
      html += "<p style=\"text-size:8px;color:black\"><small>" + stockists[i].address_1 + "</small><br/>";
      if(stockists[i].address_2.length > 0) html += "<small>" + stockists[i].address_2 + "</small><br/>";
      if(stockists[i].address_3.length > 0) html += "<small>" + stockists[i].address_3 + "</small><br/>";
      html += stockists[i].town + "<br/>";
      html += "<small>" + stockists[i].county + "</small><br/>";
      html += "<small>" + stockists[i].postcode + "</small><br/>";
      html += stockists[i].phone + "</p>";
      //If this stockist has an email address, provide a link to that
      if(stockists[i].email.length > 0) {
        html += "<p>"
        html += "Email: <a href=\"mailto:" + stockists[i].email + "\">";
        html += stockists[i].email;
        html += "</a></p>";
      }
      // Add the marker to map
      map.addOverlay(createMarker(point,html));

    }
  }
}

function createMarker(point,html) {
  var marker = new GMarker(point);

  GEvent.addListener(marker, "click", function() {
                                            marker.openInfoWindow(html);
                                          });
  return marker;
}

function showStockistAddress(response) {
   // This function adds the point to the map

  // Retrieve the object
  place = response.Placemark[0];

  // Retrieve the latitude and longitude
  var point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
  //if(place.Point.coordinates[0] <= 0)

  //alert(point);
  // Create a marker

  var html = "<h4>" + stockists[i].stockistName + "</h4>";
  html += "<p style=\"text-size:8px;color:black\"><small>" + stockists[i].address_1 + "</small><br/>";
  if(stockists[i].address_2.length > 0) html += "<small>" + stockists[i].address_2 + "</small><br/>";
  if(stockists[i].address_3.length > 0) html += "<small>" + stockists[i].address_3 + "</small><br/>";
  html += stockists[i].town + "<br/>";
  html += "<small>" + stockists[i].county + "</small><br/>";
  html += "<small>" + stockists[i].postcode + "</small><br/>";
  html += stockists[i].phone + "</p>";
  // Add the marker to map
  map.addOverlay(createMarker(point,html));
  // Add address information to marker
  //marker.openInfoWindowHtml(place.address);
}
