var map;
var marker;
var point;
var changedpoint;
var loaded = false;
    
function load() {
  if (!loaded && GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
         
    // Links oben Karten verschieben, vergrößern, verkleinern
    map.addControl(new GLargeMapControl());
        
    // Maßstabsangabe
    //map.addControl(new GScaleControl(), new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(64,15)));
        
    // Oben rechts "Karte, Satellit, Hybrid
    //map.addControl(new GMapTypeControl());
        
    // Übersichtskarte rechts unten
    map.addControl(new GOverviewMapControl());

    map.enableContinuousZoom();
    map.enableDoubleClickZoom();
        
    loaded = true;
  }
}

function getCoordinatesCallback (oPlacemarks) {
  if (!oPlacemarks.Placemark) {
    document.getElementById("getCoordinatesResult").innerHTML = 'Adresse nicht gefunden: <input type="button" value="Manuelle Suche" onclick="manuellSearch(); return false;" />';
    document.getElementById("getCoordinatesResult").className = 'geocode_error';
  } 
  else if (oPlacemarks.Placemark.length > 1) {
    var inner = 'Adresse nicht eindeutig:<br />';
    for (i = 0; i < oPlacemarks.Placemark.length; i++)
      inner += 'Breite: ' + oPlacemarks.Placemark[i].Point.coordinates[1] + '-  Länge: ' + oPlacemarks.Placemark[i].Point.coordinates[0] + '<br />';
    inner += '<input type="button" value="Manuelle Suche" onclick="manuellSearch(); return false;" />';
    document.getElementById("getCoordinatesResult").innerHTML = inner;
    document.getElementById("getCoordinatesResult").className = 'geocode_error';
  } else {
    var placemark = oPlacemarks.Placemark[0];
    point = new GLatLng(placemark.Point.coordinates[1], placemark.Point.coordinates[0]);
    if (point) {
      document.getElementById('Latitude').value = point.y;
      document.getElementById('Longitude').value = point.x;
      document.getElementById("getCoordinatesResult").innerHTML = 'Adresse gefunden: <input type="button" value="Auf Karte prüfen" onclick="manuellSearch(); return false;" />';
      document.getElementById("getCoordinatesResult").className = 'geocode_success';
    }
  }
}

    
function getCoordinates (addressid, zipid, cityid, countryid) {
  var gcoder  = new GClientGeocoder();
  var address = document.getElementById(addressid).value;
  var zip     = document.getElementById(zipid).value;
  var city    = document.getElementById(cityid).value;
  var country = document.getElementById(countryid).value;
  document.getElementById("getCoordinatesResult").innerHTML = 'Suche Adresse...';
  document.getElementById("getCoordinatesResult").className = 'geocode_progress';

  if (city.length > 3 || zip.length > 3) {
    gcoder.getLocations(address + ', ' + zip + ' ' + city + ' ' + country, getCoordinatesCallback);
  }
  else {
    document.getElementById('Latitude').value = '';
    document.getElementById('Longitude').value = '';
  }
}
    
function getLatLonFromManuellSearch () {
  movedpoint = marker.getPoint();
}
    
function manuellSearch() {
  document.getElementById("modalPage").style.display = "block";
  //document.getElementById("map").style.display = "block";
  //document.getElementById("close").style.display = "block";
  load();
  if (point) {
    map.setCenter(point, 14);
    map.openInfoWindow(map.getCenter(), document.createTextNode("Derzeitige Adresse. Ist sie falsch, so korrigiere sie bitte, indem du den Marker auf die richtige Adresse ziehst."));
  }
  else {
    point = new GLatLng(47.30379626140715, 13.085403442382812);
    map.setCenter(point, 6);
    map.openInfoWindow(map.getCenter(), document.createTextNode("Ziehe den Marker mit der Maus auf deine Adresse."));
  }
  movedpoint = point;
  if (marker) {
    marker.setLatLng (point);
  } else {
    marker = new GMarker(point, {draggable: true});
    GEvent.addListener(marker, 'dragend', getLatLonFromManuellSearch);
    map.addOverlay(marker);
  }
}
    
function closeMap () {
  document.getElementById("modalPage").style.display = "none";
  //document.getElementById("map").style.display = "none";
  //document.getElementById("close").style.display = "none";
  
  // this is not really nice
  if (whichBrs () == 'Internet Explorer') {
    GUnload ();
    delete marker;
    marker = null;
    loaded = false;
  }
}
function closeMapConfirm () {
  if (movedpoint) {
    point = movedpoint;
    document.getElementById('Latitude').value = point.y;
    document.getElementById('Longitude').value = point.x;
    document.getElementById("getCoordinatesResult").innerHTML = 'Adresse gefunden: <input type="button" value="Adresse prüfen" onclick="manuellSearch(); return false;" />';
  }
  document.getElementById("modalPage").style.display = "none";
  
  qd_print_message ('Die Adresskoordinaten wurden noch nicht gespeichert. Drücke hierfür unten auf der Seite auf \'Einstellungen übernehmen\'', 'warning');
  //document.getElementById("map").style.display = "none";
  //document.getElementById("close").style.display = "none";
  
  // this is not really nice
  if (whichBrs () == 'Internet Explorer') {
    GUnload ();
    delete marker;
    marker = null;
    loaded = false;
  }
  
}


// Browser Detection Javascript
// copyright 1 February 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function whichBrs() {
var agt=navigator.userAgent.toLowerCase();
if (agt.indexOf("opera") != -1) return 'Opera';
if (agt.indexOf("staroffice") != -1) return 'Star Office';
if (agt.indexOf("webtv") != -1) return 'WebTV';
if (agt.indexOf("beonex") != -1) return 'Beonex';
if (agt.indexOf("chimera") != -1) return 'Chimera';
if (agt.indexOf("netpositive") != -1) return 'NetPositive';
if (agt.indexOf("phoenix") != -1) return 'Phoenix';
if (agt.indexOf("firefox") != -1) return 'Firefox';
if (agt.indexOf("safari") != -1) return 'Safari';
if (agt.indexOf("skipstone") != -1) return 'SkipStone';
if (agt.indexOf("msie") != -1) return 'Internet Explorer';
if (agt.indexOf("netscape") != -1) return 'Netscape';
if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
if (agt.indexOf('\/') != -1) {
if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
return navigator.userAgent.substr(0,agt.indexOf('\/'));}
else return 'Netscape';} else if (agt.indexOf(' ') != -1)
return navigator.userAgent.substr(0,agt.indexOf(' '));
else return navigator.userAgent;
}

