var map = null;
var geocoder = null;
var gicons = [];
var markersLoaded = null;

function setLatLng() {
  var center = map.getCenter();
  document.getElementById("lattitude").innerHTML = center.lat().toString();
  document.getElementById("longitude").innerHTML = center.lng().toString();
}

function load() {
  if (GBrowserIsCompatible()) {
    gicons["union"] = new GIcon(G_DEFAULT_ICON);
    gicons["club1"] = new GIcon(G_DEFAULT_ICON, "marker_blue.png");
    gicons["club2"] = new GIcon(G_DEFAULT_ICON, "marker_green.png");
    gicons["club3"] = new GIcon(G_DEFAULT_ICON, "marker_yellow.png");

    map = new GMap2(document.getElementById("map"));

    loadInfo("show");

    geocoder = new GClientGeocoder();
    map.setCenter(new GLatLng(50.9000, 10.2), 6);
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GScaleControl());
    map.addControl(new GOverviewMapControl());
    setLatLng();

    GEvent.addListener(map,
                       "moveend",
                       function() {setLatLng();}
                      );

    var loadType = 0;
    if (loadType == 1) {
      loadMarkers("geoclubs.xml", 1);
    }
    else {
      loadMarkers("geoclubs1.xml", 0);
      loadMarkers("geoclubs2.xml", 0);
      loadMarkers("geoclubs3.xml", 1);
    }

    var hide = "hide";
    if (markersLoaded == 1)
      loadInfo(hide);
    else
      setTimeout("loadInfo(hide)",1000);
  }
}
function loaddrf() {
  if (GBrowserIsCompatible()) {
    gicons["union"] = new GIcon(G_DEFAULT_ICON);
    gicons["club1"] = new GIcon(G_DEFAULT_ICON, "marker_blue.png");
    gicons["club2"] = new GIcon(G_DEFAULT_ICON, "marker_green.png");
    gicons["club3"] = new GIcon(G_DEFAULT_ICON, "marker_yellow.png");

    map = new GMap2(document.getElementById("map"));

    loadInfo("show");

    geocoder = new GClientGeocoder();
    map.setCenter(new GLatLng(50.9000, 10.2), 6);
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GScaleControl());
    map.addControl(new GOverviewMapControl());
    setLatLng();

    GEvent.addListener(map,
                       "moveend",
                       function() {setLatLng();}
                      );

    var loadType = 0;
    if (loadType == 1) {
      loadMarkers("geoclubsdrf.xml", 1);
    }
    else {
      loadMarkers("geoclubs1.xml", 0);
      loadMarkers("geoclubsdrf.xml", 1);
    }

    var hide = "hide";
    if (markersLoaded == 1)
      loadInfo(hide);
    else
      setTimeout("loadInfo(hide)",1000);
  }
}

// Creates a marker at the given point with the given number label
function createMarker(point, text, club, lev) {
  var opts = new Object();
  opts.title = club;
  if (lev == "V")
    opts.icon = gicons["union"];
  else if (lev == "B")
    opts.icon = gicons["club1"];
  else if (lev == "R")
    opts.icon = gicons["club2"];
  var marker = new GMarker(point, opts);
  GEvent.addListener(marker,
                     "click",
                     function() {marker.openInfoWindowHtml(text);}
                    );
  return marker;
}


function loadMarkers(file, last) {
  GDownloadUrl(file, function(data, responseCode) {
      var xml = GXml.parse(data);
      if (!xml) {
       alert("invalid xml file - no xml");
      }
      if (!xml.documentElement) {
         alert("invalid xml file - no documentElement");
      }
      var markers = xml.documentElement.getElementsByTagName("marker");
      for (var i = 0; i < markers.length; i++) {
        var lat = markers[i].getAttribute("lat");
        var lng = markers[i].getAttribute("lng");
        var point = new GLatLng(parseFloat(lat), parseFloat(lng));
        var club = markers[i].getAttribute("club");
        var url = markers[i].getAttribute("url");
        var lev = markers[i].getAttribute("lev");
        var text = club + "<br>" + "<a href=http://" + url + " target=_club>" + url + "</a>";
        map.addOverlay(createMarker(point, text, club, lev));
      }
    }
  );
  if (last == 1)
    markersLoaded = 1;
}


function showAddress() {
  if (geocoder) {
    var address = document.geocode.street.value;
    var add = document.geocode.city.value;
    if (address != "") {
      if (add != "")
        address += ", " + add;
    }
    else
       address = add;
    add = document.geocode.country.value;
    if (address != "") {
      if (add != "")
        address += ", " + add;
    }
    else
       address = add;

    geocoder.getLatLng(address,
                       function(point) {
                       if (!point) {
                           alert(address + " not found");
                         } else {
                           var opts = new Object();
                           opts.title = address;
                           opts.icon = new GIcon(G_DEFAULT_ICON, "marker_purple.png");
                           map.setCenter(point, 13);
                           var marker = new GMarker(point, opts);
                           map.addOverlay(marker);
//                             marker.openInfoWindowHtml(address);
                         }
                       }
                      );
  }
  else {
    alert("geocoder nor set");
  }
}


// A function to hide/show the loading message
function loadInfo(opt)
{
  var loaddiv = document.getElementById("loaddiv");
  if (loaddiv == null) {
    alert("Sorry can't find the loaddiv");
    return;
  }
  //div found
  if (opt == "show")
    loaddiv.style.visibility="visible";
  else
    loaddiv.style.visibility="hidden";
}

