/*
$ js/googleMap.js | 2007/06/22 16:30 | 2007/06/25 12:40 $
*/

var map = null;
var geocoder = null;
var newmarker;

function load() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(47.17991241867412, 19.503607749938965), 6); // Magyarország középrepozicionálása

    var icon = new GIcon();
    icon.image = "/style/googlemap_marker.png";
    icon.shadow = "/style/googlemap_marker_shadow.png";
    icon.iconSize = new GSize(56, 64);
    icon.shadowSize = new GSize(56, 64);
    icon.iconAnchor = new GPoint(17, 64);
    icon.infoWindowAnchor = new GPoint(5, 1);

    GEvent.addListener(map, "dblclick", function(marker, point) {
      if ( !newmarker ) {
        map.addOverlay( createMarker(point, icon) ); // Ha nem pottyantottunk még akkor ledobunk egy bubit
        saveMarkerCoords(); // Koordináták tárolása
      } else {
        newmarker.remove(); // Bubi törlése ha meggondoltuk magunkat
      }
    });

    // Ha már korábban állítottunk be bubit, akkor elhelyezzük a térképen
    if ((existingMarkerLng > 0) | (existingMarkerLat > 0)) {
      lng = document.getElementById("lng");
      lat = document.getElementById("lat");

      if (lng) { lng.value = existingMarkerLng; }
      if (lat) { lat.value = existingMarkerLat; }

      point = new GLatLng(existingMarkerLat, existingMarkerLng);
      map.setCenter(new GLatLng(existingMarkerLat, existingMarkerLng), 8);
      map.addOverlay( createMarker(point, icon) );
    }

    geocoder = new GClientGeocoder();
  }
}

function createMarker(point, icontype) {
  newmarker = new GMarker(point, {draggable: true, icon: icontype});

  GEvent.addListener(newmarker, "dragstart", function() {
    map.closeInfoWindow();
  });

  GEvent.addListener(newmarker, "dragend", function() {
    saveMarkerCoords(); // Koordináták tárolása
  });

  GEvent.addListener(newmarker, "remove", function() {
    resetMarkerCoords();
    newmarker = null;
  });

  return newmarker;
}

function saveMarkerCoords() {
  document.getElementById("lng").value = newmarker.getPoint().lng().toString();
  document.getElementById("lat").value = newmarker.getPoint().lat().toString();
}

function resetMarkerCoords() {
  document.getElementById("lng").value = 0;
  document.getElementById("lat").value = 0;
}

function positionToLocation(location, zoom) {
  if (geocoder) {
    geocoder.getLatLng(
      location,
      function(point) {
        if (!point) {
          point = new GLatLng(47.17910, 19.50450); // Magyarországra pozicionálás ha nem található a lekért ország
          map.setCenter(new GLatLng(47.17910, 19.50450), 6); // Magyarország középrepozicionálása
        } else {
          map.setCenter(point, zoom);
        }
      }
    );
  }
}

window.onload = function() {
  G_NORMAL_MAP.getName = function() { return "Térkép"; }      
  G_SATELLITE_MAP.getName = function() { return "Műhold"; }
  G_HYBRID_MAP.getName = function() { return "Hibrid"; }

  load();
};

window.onunload = function() {
  GUnload();
};
