var map;
var initial_center = new GLatLng(50.5917,4.3833);
var initial_zoom = 7;
var address_zoom = 16;
var geocoder;
var markers = new Array(0);
var icons = new Array(0);
var autozoom = false;
var so_busy = false;
var lastopenmarker = null;
var lastopenmarkerhtml = null;
var isopen = false;

function createIcons() {
  icons[0] = new GIcon(G_DEFAULT_ICON);
  icons[0].image = '/Sites/octa/Templates/img/icon_vergrootglas.png';
  icons[0].iconSize = new GSize(31, 31);
  icons[1] = new GIcon(G_DEFAULT_ICON);
  icons[1].image = '/Sites/octa/Templates/img/icon.png';
  icons[1].iconSize = new GSize(24, 24);
  icons[2] = new GIcon(G_DEFAULT_ICON);
  icons[2].image = '/Sites/octa/Templates/img/icon.png';
  icons[2].iconSize = new GSize(24, 24);
  icons[3] = new GIcon(G_DEFAULT_ICON);
  icons[3].image = '/Sites/octa/Templates/img/icon.png';
  icons[3].iconSize = new GSize(24, 24);
  icons[4] = new GIcon(G_DEFAULT_ICON);
  icons[4].image = '/Sites/octa/Templates/img/icon.png';
  icons[4].iconSize = new GSize(24, 24);
  icons[5] = new GIcon(G_DEFAULT_ICON);
  icons[5].image = '/Sites/octa/Templates/img/icon.png';
  icons[5].iconSize = new GSize(24, 24);
}

function createMarker(lat, lng, type, tooltip, html, icon) 
{
  var point = new GLatLng(lat, lng);
  var marker = new GMarker(point, {icon: icons[icon], title: tooltip});
 
    if (type == 0) 
    {
		GEvent.addListener(marker, "click", function() {window.location = "octa.aspx?tabid=279&culture=nl-be&site=octa&stationID=" + html + "&filter=map&"; });
    }
    
  else 
  {
    GEvent.addListener(marker, "click", function() {
      map.closeInfoWindow();
      map.setCenter(marker.getPoint(), map.getZoom()+1);
    });
  }

  return marker;
}

function createMarkerFromItem(obj) 
{
  var marker = createMarker(obj.getAttribute("lat"), obj.getAttribute("lng"), 
	obj.getAttribute("Type"), obj.getAttribute("tooltip"), obj.getAttribute("html"), obj.getAttribute("icon"));

  return marker;
}

function load() 
{
  if (GBrowserIsCompatible()) 
  {
    map = new GMap2(document.getElementById("map2"));
    map.addControl(new GMapTypeControl());
    geocoder = new GClientGeocoder();
    
    map.addControl(new GLargeMapControl());
    createIcons();
    map.setCenter(initial_center, initial_zoom);
    
    //map.setMapType(G_MAP_TYPE);
    map.setMapType(G_NORMAL_MAP);
    showObjects(map);
    
    GEvent.addListener(map, "zoomend", function() {
      showObjects(map);
    });
    
    GEvent.addListener(map, "dragend", function() {
      showObjects(map);
    });
  }
}

function showObjects(map) 
{
  var request = GXmlHttp.create();
  var bounds = map.getBounds();
  var sw = bounds.getSouthWest();
  var ne = bounds.getNorthEast();
  var size = map.getSize();
  var url = urlprefix;//"/mapitems.aspx?";
  url += "env="+sw.toUrlValue()+","+ne.toUrlValue();
  url += "&amp;width="+size.width+"&amp;height="+size.height;
  
  
  if (so_busy) return;
  so_busy = true;
  map.clearOverlays();
  request.open("GET", url, true);
  request.onreadystatechange = function() 
  {
    if (request.readyState == 1) 
    {
      //document.getElementById('message').innerHTML = '';
      //document.getElementById('message').innerHTML = 'busy...';
    }
    if (request.readyState == 4) 
    {
      var items = request.responseXML.getElementsByTagName("item");
      for (i = 0; i < items.length; i++) 
      {
				map.addOverlay(createMarkerFromItem(items[i]));
      }
      //document.getElementById('message').innerHTML = '';
      if (autozoom && items.length == 0) 
      {
				map.zoomOut();
				autozoom = false;
      }
    }
  }
  request.send(null);
  so_busy = false;
}