	// (c)2006 by Lars Holtrup of honet
// Creates a marker whose info window displays the letter corresponding
// to the given index.
function createMarker(point, letter, htm) {
  // Create a lettered icon for this point using our icon class
  // var letter = index.toUpperCase().charCodeAt(0);
  var icon = new GIcon(baseIcon);
  icon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
  var marker = new GMarker(point, icon);

  if (htm.length>0)
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(htm, {maxWidth:400});
  });
  return marker;
}

    function makeMarker(point) {
	marker = new GMarker( point, {draggable: true, title:"Map Point"});
	GEvent.addListener(marker, "click", function() {
	  marker.openInfoWindowHtml("<u>Hilfe:</u><br>ziehe den Pin<br>zur neuen Position.");
	  });
	GEvent.addListener(marker, "dragstart", function() {
	  map.closeInfoWindow();
	  });
	GEvent.addListener(marker, "dragend", function() {
	  var center = marker.getPoint();
	  wp[WPList.selectedIndex] = center;
	  //map.clearOverlays();
	  if(TrackOverlay != null) map.removeOverlay(TrackOverlay);
	  DrawTrack();
	  //makeMarker(center);
	  });
	map.addOverlay(marker);
    }


    // distance (in metres) between 2 points. 
    function distance ( p1, p2 ) {
	// degrees to radians
	var d2r = (Math.PI / 180.0);

        var a = d2r*( 90 - p1.y);
        var b = d2r*( 90 - p2.y);
        var t = d2r*( p2.x - p1.x);
        var c = Math.acos( Math.cos(a) * Math.cos(b) + Math.sin(a) * Math.sin(b) * Math.cos(t));

        return c * 6367000; // Earth RADIUS
    }

    function showAddress(address) {
      if (geocoder) {
	if(marker != null) map.removeOverlay(marker);
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " nicht gefunden");
            } else {
              map.setCenter(point, 13);
              //makeMarker(point);
            }
          }
        );
      }
    }

    function ShowAllMAP() {
	tdistance = 0;
	WPList.selectedIndex = null;
	for(i=WPList.length-1; i>=0; i--) 
	    WPList.options[i] = null;
	map.clearOverlays();

	for(i=0; i<wp.length; i++) {
	  var newOpt = new Option(i+" "+para[i], i);
	  WPList.options[i] = newOpt;
	  if ( i < wp.length - 1 ) {
		var lastLeg = distance(wp[i], wp[i+1]);
		tdistance += lastLeg;	
	  }
	}
	if (typeof document.getElementById("leng") == "object") {
	    document.getElementById("leng").firstChild.nodeValue = (tdistance/1000).toFixed(2)+" km";
	}
	if (wp.length>0) {
		WPList.selectedIndex = 0;
		DrawTrack();
		ShowMAPPoint();
	}
    }

    function AddMAPPoint(center) {
	var name   = document.getElementById("name");
	var WPListvalue = WPList.selectedIndex;
	map.clearOverlays();

	if (Mode == "add") {
	  var nEntry = new Option(wp.length+" "+name.value, wp.length, false, true);
	  wp.push(center);
	  para.push(name.value);
	  WPList.options[WPList.length] = nEntry;

	  window.status = "Add: "+WPListvalue;
		if ( wp.length > 1 ) {
			// Re-calculate the distance so far
			var lastLeg = distance(wp[wp.length - 2], 
						  wp[wp.length - 1]);
			tdistance += lastLeg;	
			if (typeof document.getElementById("leng") == "object") {
			    document.getElementById("leng").firstChild.nodeValue = (tdistance/1000).toFixed(2)+" km";
			}
		}

	} else if(Mode == "ins") {
	  wp.splice(WPListvalue+1, 0, center);
	  para.splice(WPListvalue+1, 0, name.value);
	  for(i=0; i<wp.length; i++) {
	    var newOpt = new Option(i+" "+para[i], i);
	    WPList.options[i] = newOpt;
	  }
	  window.status = "Ins: "+WPList.value;
		if ( wp.length > 1 ) {
			// Re-calculate the distance so far
			var lastLeg = distance(wp[WPListvalue], wp[WPListvalue +2]);
			tdistance -= lastLeg;	
			var lastLeg = distance(wp[WPListvalue], wp[WPListvalue +1]);
			tdistance += lastLeg;	
			var lastLeg = distance(wp[WPListvalue +1], wp[WPListvalue +2]);
			tdistance += lastLeg;	
			if (typeof document.getElementById("leng") == "object") {
			    document.getElementById("leng").firstChild.nodeValue = (tdistance/1000).toFixed(2)+" km";
			}
		}
	}
	WPList.selectedIndex = WPListvalue+1;
	document.getElementById("myup").style.display = "block";
	name.value = "";
	DrawTrack();
	ShowMAPPoint();
    }

    function DelMAPPoint() {
	var WPListvalue = WPList.selectedIndex, InBetween = 0;
	if ( typeof wp[WPListvalue-1] == "object" ) {
		var lastLeg = distance(wp[WPListvalue], wp[WPListvalue -1]);
		tdistance -= lastLeg;
		InBetween++;
	}
	if ( typeof wp[WPListvalue+1] == "object" ) {
		var lastLeg = distance(wp[WPListvalue], wp[WPListvalue +1]);
		tdistance -= lastLeg;
		InBetween++;
	}
	if ( InBetween > 1 ) {
		// Re-calculate the distance so far
		var lastLeg = distance(wp[WPListvalue -1], wp[WPListvalue +1]);
		tdistance += lastLeg;
	}
	if (typeof document.getElementById("leng") == "object") {
	    document.getElementById("leng").firstChild.nodeValue = (tdistance/1000).toFixed(2)+" km";
	}
	if( !isNaN(WPListvalue) ) {
	  wp.splice(WPListvalue, 1);	// del WP
	  para.splice(WPListvalue, 1);	// del Para
	  WPList.options[WPListvalue] = null;
	}
	for(i=WPListvalue; i<wp.length; i++) {
	  var newOpt = new Option(i+" "+para[i], i);
	  WPList.options[i] = newOpt;
	}
	WPList.selectedIndex = (WPListvalue>0) ? (WPListvalue-1) : 0;
	map.clearOverlays();
	DrawTrack();
	ShowMAPPoint();
    }

    function ShowMAPPoint() {
	var WPListvalue = WPList.selectedIndex;

	map.closeInfoWindow();
	if(marker != null) map.removeOverlay(marker);
	map.panTo(wp[WPListvalue]);
	if (( WPListvalue > 0 ) && ( WPListvalue < wp.length-1 )) {
	  makeMarker(wp[WPListvalue]);
	}
	Mode = (WPListvalue == wp.length-1) ? "add" : "ins";
	window.status = "Show: "+wp[WPListvalue].toString()+" "+Mode;
	ShowMode(Mode);
    }

    function SearchMAPPoint() {
	window.status = "Find: "+Mode;
	if (Mode == "find") {
	  // Optionally set the cursors to be used for dragging
	  GDraggableObject.setDraggableCursor("crosshair");
	  Mode = "ins";
	} else {
	  // Optionally set the cursors to be used for dragging
	  GDraggableObject.setDraggableCursor("help");
	  Mode = "find";

	    GEvent.clearListeners(map, "click")
	    GEvent.addListener(map, "click", function(overlay, point) {
		  if (point) {
			FindMAPPoint(point);
		}
	    });

	}
	ShowMode(Mode);
    }

    function FindMAPPoint(point) {
	var min = 1000000000, dx, dy, dist, wayp=-1;
	for(i=0; i<wp.length; i++) {
	  dx = point.x - wp[i].x;
	  dy = (point.y - wp[i].y)*2;
	  dist = dx*dx + dy*dy;
	  if (min > dist) {
	    min = dist;
	    wayp = i;
	  }
	}
	WPList.selectedIndex = (wayp<0) ? 0 : wayp;

	    GEvent.clearListeners(map, "click")
	    GEvent.addListener(map, "click", function(overlay, point) {
		  if (point) {
			AddMAPPoint(point);
		}
	    });
	ShowMAPPoint();
    }

    function ShowMode(myMode) {
      if (typeof document.getElementById("mode") == "object") {
	var MSet = (myMode == "ins") ? "einf." : (myMode == "find") ? "finde" : "hinzuf.";
	document.getElementById("mode").firstChild.nodeValue = MSet;
	document.getElementById("mimg").src = "map_"+myMode+".gif";
      }
    }

    function DrawTrack() {
	TrackOverlay = new GPolyline(wp, "#FF0088", 8);
	map.addOverlay(TrackOverlay);
	// Add a marker at the beginning and the end
	map.addOverlay(createMarker(wp[0], "S", ""));
	if ( wp.length > 1 )
		map.addOverlay(createMarker(wp[wp.length-1], "E", ""));
	if ( wp.length > 0 )
		document.getElementById("timg").src = "save.gif";
	else
		document.getElementById("timg").src = "new.gif";
    }

function such_xml() {
 var center = map.getCenter();
 var bnds = map.getBounds();
 var zoom = Math.pow(2, 16-map.getZoom());

	//window.status='lat='+center.lat()+'&lng='+center.lng()+'&radius='+zoom;
  if (document.getElementById("swiki").checked == true) {
    if (!document.getElementById("suchg")) {
	a = document.createElement('script');
	a.setAttribute('type','text/javascript');
	a.setAttribute('id','suchg');
	a.setAttribute('src','geo_xml.php?lat='+center.lat()+'&lng='+center.lng()+'&radius='+zoom);
	document.getElementsByTagName('head')[0].appendChild(a);
    } else {
	document.getElementById("suchg").src = 'geo_xml.php?lat='+center.lat()+'&lng='+center.lng()+'&radius='+zoom;
    }
  }
  if (document.getElementById("sort").checked == true) {
    if (!document.getElementById("sucho")) {
	a = document.createElement('script');
	a.setAttribute('type','text/javascript');
	a.setAttribute('id','sucho');
	a.setAttribute('src','ort_xml.php?lat='+center.lat()+'&lng='+center.lng()+'&radius='+zoom);
	document.getElementsByTagName('head')[0].appendChild(a);
    } else {
	document.getElementById("sucho").src = 'ort_xml.php?lat='+center.lat()+'&lng='+center.lng()+'&radius='+zoom;
    }
  }
  if (document.getElementById("sstart").checked == true) {
    if (!document.getElementById("suchs")) {
	a = document.createElement('script');
	a.setAttribute('type','text/javascript');
	a.setAttribute('id','suchs');
	a.setAttribute('src','strt_xml.php?bnds='+bnds.toString());
	document.getElementsByTagName('head')[0].appendChild(a);
    } else {
	document.getElementById("suchs").src = 'strt_xml.php?bnds='+bnds.toString();
    }
  }
  if (document.getElementById("sende").checked == true) {
    if (!document.getElementById("suche")) {
	a = document.createElement('script');
	a.setAttribute('type','text/javascript');
	a.setAttribute('id','suche');
	a.setAttribute('src','end_xml.php?bnds='+bnds.toString());
	document.getElementsByTagName('head')[0].appendChild(a);
    } else {
	document.getElementById("suche").src = 'end_xml.php?bnds='+bnds.toString();
    }
  }
  if (document.getElementById("spano").checked == true) {
    if (!document.getElementById("suchp")) {
	a = document.createElement('script');
	a.setAttribute('type','text/javascript');
	a.setAttribute('id','suchp');
	a.setAttribute('src','pano_obj.php?bnds='+bnds.toString());
	document.getElementsByTagName('head')[0].appendChild(a);
    } else {
	//document.getElementById("suchp").src = 'pano_obj.php?bnds='+bnds.toString();
	var sc = document.createAttribute('src');
	sc.nodeValue = 'pano_obj.php?bnds='+bnds.toString();
	document.getElementById("suchp").setAttributeNode(sc);
    }
  }
}

