var map;
var geocoder;

var customIcons = [];

var icon16Icon = new GIcon();
icon16Icon.image = "http://maps.e-one.it/upload/sedi.png";
icon16Icon.iconSize = new GSize(30, 30);
icon16Icon.iconAnchor = new GPoint(15, 30);
icon16Icon.infoWindowAnchor = new GPoint(15, 5);
customIcons["sedi"] = icon16Icon;
var icon17Icon = new GIcon();
icon17Icon.image = "http://maps.e-one.it/upload/comuni.png";
icon17Icon.iconSize = new GSize(30, 30);
icon17Icon.iconAnchor = new GPoint(15, 30);
icon17Icon.infoWindowAnchor = new GPoint(15, 5);
customIcons["comuni"] = icon17Icon;
var icon12Icon = new GIcon();
icon12Icon.image = "http://maps.e-one.it/upload/fattorie.png";
icon12Icon.iconSize = new GSize(30, 30);
icon12Icon.iconAnchor = new GPoint(15, 30);
icon12Icon.infoWindowAnchor = new GPoint(15, 5);
customIcons["fattorie"] = icon12Icon;
var icon14Icon = new GIcon();
icon14Icon.image = "http://maps.e-one.it/upload/parco.png";
icon14Icon.iconSize = new GSize(30, 30);
icon14Icon.iconAnchor = new GPoint(15, 30);
icon14Icon.infoWindowAnchor = new GPoint(15, 5);
customIcons["parco"] = icon14Icon;
var icon15Icon = new GIcon();
icon15Icon.image = "http://maps.e-one.it/upload/bici.png";
icon15Icon.iconSize = new GSize(30, 30);
icon15Icon.iconAnchor = new GPoint(15, 30);
icon15Icon.infoWindowAnchor = new GPoint(15, 5);
customIcons["bici"] = icon15Icon;
var icon13Icon = new GIcon();
icon13Icon.image = "http://maps.e-one.it/upload/info.png";
icon13Icon.iconSize = new GSize(30, 30);
icon13Icon.iconAnchor = new GPoint(15, 30);
icon13Icon.infoWindowAnchor = new GPoint(15, 5);
customIcons["info"] = icon13Icon;
var icon11Icon = new GIcon();
icon11Icon.image = "http://maps.e-one.it/upload/libri.png";
icon11Icon.iconSize = new GSize(30, 30);
icon11Icon.iconAnchor = new GPoint(15, 30);
icon11Icon.infoWindowAnchor = new GPoint(15, 5);
customIcons["libri"] = icon11Icon;
var icon9Icon = new GIcon();
icon9Icon.image = "http://maps.e-one.it/upload/sport.png";
icon9Icon.iconSize = new GSize(30, 30);
icon9Icon.iconAnchor = new GPoint(15, 30);
icon9Icon.infoWindowAnchor = new GPoint(15, 5);
customIcons["sport"] = icon9Icon;

function load() {
  if (GBrowserIsCompatible()) {
    geocoder = new GClientGeocoder();
    map = new GMap2(document.getElementById('map'));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(45.5000000, 8.7500000), 11);

 var searchUrl = 'localMarkers.php';
 GDownloadUrl(searchUrl, function(data) {
   var xml = GXml.parse(data);
   var markers = xml.documentElement.getElementsByTagName('marker');
   map.clearOverlays();

   if (markers.length == 0) {
     return;
   }

   var allmarkers = [];
   var bounds = new GLatLngBounds();
   for (var i = 0; i < markers.length; i++) {
     var name = markers[i].getAttribute('name');
     var address = markers[i].getAttribute('address');
     var type = markers[i].getAttribute('type');
     var info = String(markers[i].getAttribute('info'));
     if(info != " ")
     {
         info = info.replace(/&amp;/g, "&");
         info = info.replace(/&lt;/g, "<");
         info = info.replace(/&gt;/g, ">");
         info = info.replace(/&quot;/g, '"');
         info = info.replace(/&#39;/g, "'");
     }
     var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')),
                             parseFloat(markers[i].getAttribute('lng')));
     var marker = createMarker(point, name, address, type, info);
     map.addOverlay(marker);
     bounds.extend(point);
   }
   var markerCluster = new MarkerClusterer(map, allmarkers);
 });
}

function createMarker(point, name, address, type, info) {
  var marker = new GMarker(point, customIcons[type]);
  var html = '<b>' + name + '</b> <br/>' + address + '<br />'+info+'<br clear="both" />';
  GEvent.addListener(marker, 'click', function() {
    marker.openInfoWindowHtml(html);
  });
  return marker;
}
}

