var lastFoundPoint;
var bounds;

function initialize() {
  // create map object
  map = new google.maps.Map2(document.getElementById("map"));
  baseIcon = new GIcon();
  baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
  baseIcon.iconSize = new GSize(32, 32);
  baseIcon.shadowSize = new GSize(37, 28);
  baseIcon.iconAnchor = new GPoint(9, 28);
  baseIcon.infoWindowAnchor = new GPoint(9, 2);
  baseIcon.infoShadowAnchor = new GPoint(18, 25);

  // geocoder om adressen (ipv. lat/long coords) te kunnen gebruiken
  geocoder = new GClientGeocoder();
  geocoder.setBaseCountryCode("nl");

  // show maptype control
  map.addControl(new GMapTypeControl());
  // show zoomoptions
  map.addControl(new GLargeMapControl());
  // show small overview
  map.addControl(new GOverviewMapControl());
  // center map to Amsterdam
  map.setCenter(new google.maps.LatLng(52.09127644127848, 5.614537048339844), 7);
  // set default maptype
  // G_NORMAL_MAP- the default view
  // G_SATELLITE_MAP - showing Google Earth satellite images
  // G_HYBRID_MAP - showing a mixture of normal and satellite views
  // G_DEFAULT_MAP_TYPES - an array of these three types, useful for iterative processing
  map.setMapType(G_NORMAL_MAP); 

  bounds = new GLatLngBounds();
  // set markers on map  
  setMarkers(marker_url);
}

function setMarkers(url) {
  // Create a base icon for all of our markers that specifies the
  // shadow, icon dimensions, etc.

  baseIcon = new GIcon();
  baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
  baseIcon.iconSize = new GSize(38, 23);
  baseIcon.shadowSize = new GSize(57, 25);
  baseIcon.iconAnchor = new GPoint(9, 28);
  baseIcon.infoWindowAnchor = new GPoint(9, 2);
  baseIcon.infoShadowAnchor = new GPoint(18, 25);

  var unique = new Date();
  var sep = url.indexOf("?")!=-1?"&":"?";
  url += (sep+"u="+unique.getTime());
  
  GDownloadUrl(url, function(data) {
    var xml = GXml.parse(data);
    var markers = xml.documentElement.getElementsByTagName("marker");  
    for (var i = 0; i < markers.length; i++) {
      markerlocation = markers[i].getAttribute("locatie");     
      var locfieldsep = markerlocation.indexOf(",");

      if (locfieldsep != -1) {
        var coordsLng = "";
        var coordsLat = "";
        try {
          coordsLat = parseFloat(markerlocation.substr(0,locfieldsep));          
          coordsLng = parseFloat(markerlocation.substr(locfieldsep+2,markerlocation.length+3-locfieldsep));
        }
        catch (err) {}

        if (coordsLng != "" && coordsLat != "" && parseFloat(coordsLng) && parseFloat(coordsLat))
        {
          var latlng = new GLatLng(coordsLat, coordsLng);
          var infoText = markers[i].getAttribute("text");
          var type = markers[i].getAttribute("type");
          bounds.extend(latlng);
          createLatLngMarker(latlng, infoText, type, (i+1 == markers.length));
        }      
      }  
    }
    map.setZoom(map.getBoundsZoomLevel(bounds));  
    map.setCenter(bounds.getCenter());
  });
}

function createLatLngMarker(latlng, infoText, type, focus) {
  var markerIcon = new GIcon(baseIcon);
  markerIcon.image = marker_folder + type + '.png';

  var markerOptions = { icon:markerIcon }; // Set up our GMarkerOptions object

  var googleMarker = new GMarker(latlng, markerOptions);
  lastFoundPoint = latlng;
  //if (focus) map.setCenter(latlng);
  GEvent.addListener(googleMarker, "click", function() {
    map.setCenter(latlng);
    googleMarker.openInfoWindowHtml(infoText);
  });
  map.addOverlay(googleMarker); 
}

function createAddressMarker(address, infoText, type, focus) {
  var markerIcon = new GIcon(baseIcon);
  markerIcon.image = marker_folder + type + '.png';

  var markerOptions = { icon:markerIcon }; // Set up our GMarkerOptions object

  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        // alert(address + " niet gevonden");
      } else {
        lastFoundPoint = point;
        var googleMarker = new GMarker(point, markerOptions);
        if (focus) map.setCenter(point);
        GEvent.addListener(googleMarker, "click", function() {
          map.setCenter(point);
          googleMarker.openInfoWindowHtml(infoText);
        });
        map.addOverlay(googleMarker);
      }
    }
  );
}
