svnwikiGooglemap = new Object();

function addMarker( point, data ) {
  var file = data.getAttribute("file");
  var title = data.getAttribute("title");
  var marker = new GMarker(point);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml('<a target="_blank" href="../' + file + '">' + title + '</a>');
  });
  return marker;
}

function load() {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(0, 0), 2);
    GDownloadUrl("users.xml", function(data, responseCode) {
      var xml = GXml.parse(data);
      var markers = xml.documentElement.getElementsByTagName("marker");
      for (var i = 0; i < markers.length; i++) {
        var point = new GLatLng(parseFloat(markers[i].getAttribute("latitude")),
                                parseFloat(markers[i].getAttribute("longitude")));
        map.addOverlay(addMarker(point, markers[i]));
      }
    });
  }
}

svnwikiGooglemap.createMap = function (id, latitude, longitude, zoom) {
  var mapOptions = {
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    zoom: zoom || (latitude && longitude ? 4 : 1),
    center: new google.maps.LatLng(latitude || 0, longitude || 0)
  };
  var div = svnwikiJavascript.cleanContent(document.getElementById(id))
  div.style.height = '400px';
  div.style.display = 'block';
  var map = new google.maps.Map(div, mapOptions);
  return map
}

svnwikiGooglemap.editInitialize = function (property, id, latitude, longitude) {
  var map = svnwikiGooglemap.createMap('googlemap-' + id);
  var marker = new google.maps.Marker({map: map});
  if (latitude && longitude)
    svnwikiGooglemap.moveMarker(property, id, new google.maps.LatLng(latitude, longitude), marker);
  google.maps.event.addListener(map, 'click', function(event) {
    svnwikiGooglemap.moveMarker(property, id, event.latLng, marker);
  });
}

svnwikiGooglemap.moveMarker = function (property, id, location, marker) {
  marker.set_position(location);
  document.getElementById('xsvnwiki-googlemap:latitude:' + id).value = location.lat().toString();
  document.getElementById('xsvnwiki-googlemap:longitude:' + id).value = location.lng().toString();
}

svnwikiGooglemap.markers = {}

svnwikiGooglemap.tagsDisplayMap = function (id) {
  svnwikiTagsLoader.hideAll();
  svnwikiGooglemap.displayMap(id);
}

svnwikiGooglemap.displayMap = function (id) {
  if (svnwikiGooglemap.markers.length == 0) {
    var map = svnwikiGooglemap.createMap(id);
  } else if (svnwikiGooglemap.markers.length == 1) {
    var map = svnwikiGooglemap.createMap(id, svnwikiGooglemap.markers[0][1], svnwikiGooglemap.markers[0][2]);
  } else {
    var map = svnwikiGooglemap.createMap(id);
    var south, west, east, north;
    south = north = svnwikiGooglemap.markers[0][1];
    west = east = svnwikiGooglemap.markers[0][2];
    for (var i in svnwikiGooglemap.markers) {
      south = Math.min(south, svnwikiGooglemap.markers[i][1]);
      north = Math.max(north, svnwikiGooglemap.markers[i][1]);
      west = Math.min(west, svnwikiGooglemap.markers[i][2]);
      east = Math.max(east, svnwikiGooglemap.markers[i][2]);
    }
    map.fitBounds(new google.maps.LatLngBounds(new google.maps.LatLng(south - 5, west - 5), new google.maps.LatLng(north + 5, east + 5)));
  }
  for (var i in svnwikiGooglemap.markers) {
    var file = svnwikiGooglemap.markers[i][0];
    var title = svnwikiGooglemap.markers[i][3];
    var position = new google.maps.LatLng(svnwikiGooglemap.markers[i][1], svnwikiGooglemap.markers[i][2]);
    var marker = new google.maps.Marker({
      map: map, 
      position: position,
      title: title});
    svnwikiGooglemap.addInfoWindow(map, marker, svnwikiGooglemap.markers[i]);
  }
}

svnwikiGooglemap.addInfoWindow = function (map, marker, data) {
  var infowindow = (data[4] || svnwikiGooglemap.defaultInfoWindow)(data);
  google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map, marker);
  });
}

svnwikiGooglemap.defaultInfoWindow = function (data) {
  var file = data[0];
  var title = data[3];
  return new google.maps.InfoWindow({
      content: '<a target="_blank" href="../' + file + '">' + title + '</a>',
      size: new google.maps.Size(50, 50)
    });
}