
// Associative array of icons
// The default icons can be found on this page: http://sites.google.com/site/gmapicons/
var customIcons = {

      green: {
      icon: 'http://labs.google.com/ridefinder/images/mm_20_green.png',
        shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
      },

      tree: {
        icon: 'http://maps.google.com/mapfiles/kml/pal2/icon4.png',
        shadow: 'http://maps.google.com/mapfiles/kml/pal2/icon4s.png'
      },

      // Google default larger red icon
      googledefault: {
          icon: 'http://maps.gstatic.com/intl/en_ALL/mapfiles/marker.png',
          shadow: 'http://maps.gstatic.com/intl/en_ALL/mapfiles/shadow50.png'
      },

      // Small red icon
      normal: {
        icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
        shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
      }


    };



function load(lat, lng, zoom, country, varietyid) {
      var map = new google.maps.Map(document.getElementById("map"), {
        center: new google.maps.LatLng(lat,lng),
        zoom: zoom,
        scrollwheel: false,
        mapTypeId: 'terrain'
      });

// Create an infowindow object
      var infoWindow = new google.maps.InfoWindow;

// Get the XML dataset for this combination of country / variety
// Each tree is in a "marker" element
      downloadUrl("ajax.aspx?p=1&country=" + country + "&varietyid=" + varietyid, function(data) {
          var xml = parseXml(data);
          var markers = xml.documentElement.getElementsByTagName("marker");


          // Loop through each tree marker
          for (var i = 0; i < markers.length; i++) {


              // Create an icon
              var icon = customIcons["tree"] || {};


              // Retrieve the element values
              var owner = markers[i].getAttribute("owner") ;
//              var owner = (parseFloat(markers[i].getAttribute("lat")));
              var address = markers[i].getAttribute("address");
              var variety = markers[i].getAttribute("variety");

              // Create a point
              var point = new google.maps.LatLng(
                      parseFloat(markers[i].getAttribute("lat")),
                      parseFloat(markers[i].getAttribute("lng")));

              // Now see if following elements are at the same location, if they are, loop through them and include the variety details
              var j = i;
              var n = i;
              var multiple = 1;
              while ((j + 1) < markers.length) {
                  j++;
                  if (parseFloat(markers[n].getAttribute("lat")) == parseFloat(markers[j].getAttribute("lat")) && parseFloat(markers[n].getAttribute("lng")) == parseFloat(markers[j].getAttribute("lng")) && parseFloat(markers[n].getAttribute("customerid")) == parseFloat(markers[j].getAttribute("customerid"))) {
                      variety = variety + '<br />' + markers[j].getAttribute("variety") ;
                      i++;
                      multiple++;
                  }
                  else j = markers.length;
              }



              // HTML for the info window
              var html;
              if (multiple > 1)
                  html = "<strong>Varieties:</strong><div style='margin-left:15px;'>" + variety + "</div><strong>Location:</strong> " + address + "<br /><strong>Owner: </strong>" + owner;
              else
                  html = "<strong>Variety:</strong> " + variety + "<br /><strong>Location: </strong>" + address + "<br /><strong>Owner:</strong> " + owner;



              // Create a marker
              var marker = new google.maps.Marker({
                  map: map,
                  position: point,
                  icon: icon.icon,
                  shadow: icon.shadow
              });

              // Link an infowindow to the point
              bindInfoWindow(marker, map, infoWindow, html);


          }
      });
    }



function bindInfoWindow(marker, map, infoWindow, html) {
      google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
      });
    }

function downloadUrl(url, callback) {
      var request = window.ActiveXObject ?
          new ActiveXObject('Microsoft.XMLHTTP') :
          new XMLHttpRequest;

      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          request.onreadystatechange = doNothing;
          callback(request.responseText, request.status);
        }
      };

      request.open('GET', url, true);
      request.send(null);
    }

function parseXml(str) {
      if (window.ActiveXObject) {
        var doc = new ActiveXObject('Microsoft.XMLDOM');
        doc.loadXML(str);
        return doc;
      } else if (window.DOMParser) {
        return (new DOMParser).parseFromString(str, 'text/xml');
      }
    }

function doNothing() {}


