/**********************************************************/
/*** Handles AJAX requests to/from the Google Maps API  ***/
/*** so that stockists can be marked on Google Maps     ***/
/**********************************************************/

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

//Called at page load complete time - initialise map
function load() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    geocoder = new GClientGeocoder();
    //Set centre of map on a zoomed out UK
    map.setCenter(new GLatLng(55.378050999999999,-3.4359730000000002), 5);
    //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 = 1; 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].longitude),parseFloat(stockists[i].latitude));

      //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 a website, provide a link to that
      /*if(stockists[i].websiteURL.length > 0) {
        html += "<p>";
        html += "<a href=\"" + encodeURI(stockists[i].websiteURL) + "\" target=\"blank\">";
        html += "Click here to go to " + stockists[i].stockistName + "'s website";
        html += "</a></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
  if(response.Placemark) {
    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);
  }
}

//User has drilled down to a stockist
function selectStockist(stockistId) {
  if(stockistId.length > 0) {
    //Find the stockist in the stockists list
    var found = false;
    for(var i = 0; i < stockists.length && !found; i++) {
      if(stockists[i].stockistId == stockistId) {
        thisStockist = stockists[i];
        found = true;
      }
    }
    if(found) {
      if(parseInt(thisStockist["longitude"]) > 0) {
        //This stockist is on the map - zoom into map
        map.setCenter(new GLatLng(thisStockist["longitude"],thisStockist["latitude"]), 15);
      }
      //Show address details in info panel
      var geoLocPanel = document.getElementById("stockistGeolocations");
      geoLocPanel.innerHTML = "<h4>" + thisStockist["stockistName"] + "</h4>";
      geoLocPanel.innerHTML += "<p>";
      geoLocPanel.innerHTML += thisStockist["address_1"] + "<br/>";
      if(thisStockist["address_2"].length > 0)
        geoLocPanel.innerHTML += thisStockist["address_2"] + "<br/>";
      if(thisStockist["address_3"].length > 0)
        geoLocPanel.innerHTML += thisStockist["address_3"] + "<br/>";
      geoLocPanel.innerHTML += thisStockist["town"] + "<br/>";
      geoLocPanel.innerHTML += thisStockist["county"] + "<br/>";
      geoLocPanel.innerHTML += thisStockist["country"] + "<br/>";
      geoLocPanel.innerHTML += thisStockist["town"] + "<br/>";
      geoLocPanel.innerHTML += thisStockist["postcode"] + "</p>";
      geoLocPanel.innerHTML += "<p><strong>" + thisStockist["phone"] + "</strong></p>";
      geoLocPanel.innerHTML += "<p><a href=\"mailto:" + thisStockist["email"] + "\">";
      geoLocPanel.innerHTML += thisStockist["email"] + "</a></p>";
    } else {
      alert("There has been an error - please use the map controls");
    }
  } else {
    document.getElementById("stockistGeolocations").innerHTML = "";
  }
}