$(document).ready(function() {
try {
  load();
} catch (e) {
  // static map
}
})

try {
  addUnloadEvent(GUnload);
} catch (e) {
  // static map
}


function singleAddress(map, address, marker, zoom, geocoding) {
  if (geocoding == 1 && geocoder) {
    geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        //alert(address + " not found");
      } else {
        map.setCenter(point, zoom);
        if (marker == 1) map.addOverlay(new GMarker(point));
      }
    }
    );
  } else {
    coordinates = address.split(',');
    point = new GLatLng(coordinates[0], coordinates[1]);
    map.setCenter(point, zoom);
    if (marker == 1) {
      var icon = new GIcon(G_DEFAULT_ICON);
      /*icon.image = marker_url + '/marker.png';
      icon.shadow = null;
      icon.iconSize = new GSize(16,28);
      icon.iconAnchor = new GPoint(8, 28);*/

      map.addOverlay(new GMarker(point, {icon: icon}));
    }
  }
}

function multipleAddresses(map, bounds, addresses, marker, connect, geocoding) {
  addresses = addresses.split('$$');
  
  var points = [];
  var i = 0;
  z = addresses.length;
  for (key in addresses) {
    address = addresses[key];
    if (geocoding == 1 && geocoder) {
      geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          //alert(address + " not found " + point);
          z--;
          if (z == i) {
            setMap(map, points, bounds, marker, connect);
          }
        } else {
          bounds.extend(point);
          points.push(point);
          i++;
          if (z == i) {
            setMap(map, points, bounds, marker, connect);
          }
        }
      });
    } else {
      coordinates = addresses[key].split(',');
      point = new GLatLng(coordinates[0], coordinates[1]);
      bounds.extend(point);
      points.push(point);
      i++;
      if (z == i) {
        setMap(map, points, bounds, marker, connect);
      }
    }
  }
}

function setMap(map, points, bounds, marker, connect) {
  if (marker == 1) {
    var i = 0;
    for (key in points) {
      // Marker setzen
      map.addOverlay(createMarker(points[key], i));
      i++;
    }
  }
  // Zoomen und zentrieren
  center = bounds.getCenter();
  zoom = map.getBoundsZoomLevel(bounds);
  map.setZoom(zoom);
  map.setCenter(center);
  // Marker verbinden
  if (connect == 1) {
    var polyline = new GPolyline(points, "#ff0000", 5);
    map.addOverlay(polyline);
  }
}

var marker;
function showGmap(address, old, zoom) {
  if (geocoder && address.length > 3) {
    address = address + ' ' + old;
    geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        //alert(address + " not found");
      } else {
        map.setZoom(zoom);
        map.panTo(point);
        map.clearOverlays();

        var icon = new GIcon(G_DEFAULT_ICON);
        /*icon.image = marker_url + '/marker.png';
        icon.shadow = null;
        icon.iconSize = new GSize(16,28);
        icon.iconAnchor = new GPoint(8, 28);*/

        marker = new GMarker(point, {draggable: true, icon:icon});
        $('#ReisetippLatlng').val(marker.getLatLng());
        GEvent.addListener(marker, "dragstart", function() {
        });
    
        GEvent.addListener(marker, "dragend", function() {
          map.panTo(marker.getLatLng());
          $('#ReisetippLatlng').val(marker.getLatLng());
          //alert(marker.getLatLng());
        });
        map.addOverlay(marker);
      }
    }
    );
  }
}

// Creates a marker whose info window displays the letter corresponding
// to the given index.
function createMarker(point, index) {
  // Create a lettered icon for this point using our icon class
  var letter = String.fromCharCode("A".charCodeAt(0) + index);
  var icon = new GIcon(baseIcon);
  icon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

  /*var icon = new GIcon(G_DEFAULT_ICON);
  icon.image = marker_url + '/marker.png';
  icon.shadow = null;
  icon.iconSize = new GSize(16,28);
  icon.iconAnchor = new GPoint(8, 28);*/

  // Set up our GMarkerOptions object
  markerOptions = { icon:icon };
  var marker = new GMarker(point, markerOptions);

  //GEvent.addListener(marker, "click", function() {
    //marker.openInfoWindowHtml("Marker <b>" + letter + "</b>");
  //});
  return marker;
}

function static_map(map, single, addresses, show_marker, connect_markers) {
  var map_size = $("#"+map).width() + 'x' + $("#"+map).height();
  var map_url = 'http://maps.google.com/staticmap?size=' + map_size + '&sensor=false&key=' + gmaps_key + '&';
  if (single == 1) {
    map_url += 'center=' + addresses + '&';
  } else {
    addresses = addresses.split('|');
  }
  
  if (show_marker == 1) {
    var letter = 'a';
    var markers = [];
    if (single == 1) {
      markers.push(addresses + ',' + letter);
    } else {
      for (key in addresses) {
        markers.push(addresses[key] + ',' + letter);
        letter = String.fromCharCode(letter.charCodeAt(0) + 1);
      }
    }
    map_url += 'markers=' + markers.join('|') + '&';
  }
  
  if (connect_markers == 1) {
    map_url += 'path=rgb:0xff0000,weight:5|' + addresses.join('|') + '&';
  }
  
  // falls kein marker, rauszoomen fuer uebersicht
  if (show_marker == 0 && single == 1) {
    map_url += 'zoom=10&';
  }
  
  var map_url = 
  $("#"+map).html('<img src="' + map_url + '" />');
}

