var map;
var gdir;
var geocoder = null;
var addressMarker;

function load() {
	if (GBrowserIsCompatible()) {      
map = new GMap2(document.getElementById("map"));
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());

var baseIcon = new GIcon();
baseIcon.image = "/fileadmin/img/eli.png";
baseIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
baseIcon.iconSize = new GSize(70, 62);
baseIcon.shadowSize = new GSize(70, 62);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.infoShadowAnchor = new GPoint(18, 25);
var tinyIcon = new GIcon();
tinyIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
tinyIcon.iconSize = new GSize(12, 20);
tinyIcon.shadowSize = new GSize(22, 20);
tinyIcon.iconAnchor = new GPoint(6, 20);
tinyIcon.infoWindowAnchor = new GPoint(5, 1);

function createMarker(point,html,icon, draggable ) {
	var geocoder = new GClientGeocoder();
    geocoder.getLatLng(point, function(point) {
			map.setCenter(point, 13);
			marker = new GMarker(point, {icon: icon, draggable: draggable});
			map.addOverlay(marker);
			GEvent.addListener(marker, 'mouseover', function(){marker.openInfoWindowHtml(html)});
			marker.openInfoWindowHtml(html);
			return marker;
     		}
		)
}

createMarker('weimar,Markt 19','Hotel Elephant', baseIcon, false);

function MyApplication() {
  this.counter = 0;
 var geocoder = new GClientGeocoder();
    geocoder.getLatLng('weimar,Markt 19', function(point) {

var markerdrag = new GMarker(point, {icon: tinyIcon, draggable: true});	

			GEvent.addListener(markerdrag, "dragstart", function() {
			  map.closeInfoWindow();
			  });
			
			GEvent.addListener(markerdrag, "dragend", function() {
			content= markerdrag.getPoint().lat()+', '+markerdrag.getPoint().lng();
			  document.getElementById('von').value = content;
			  document.getElementById('nach').value = 'weimar, Markt 19';
			});

var myEventListener = GEvent.bind(map, "click", this, function(marker,point) {
markerdrag.setPoint(point);
    
		 	content= markerdrag.getPoint().lat()+', '+ markerdrag.getPoint().lng();
			  document.getElementById('von').value = content;
			  document.getElementById('nach').value = 'weimar, Markt 19';	 
        this.map.addOverlay(markerdrag)

  
   
  }); 
}
)

  


	   }
		
var application = new MyApplication();
	        

gdir = new GDirections(map, document.getElementById("directions"));
	        GEvent.addListener(gdir, "load", onGDirectionsLoad);
	        GEvent.addListener(gdir, "error", handleErrors);

	   //  setDirections("weimar,Markt 19", "weimar,Markt 19", "de_DE");
      				    }
    		}
				
 
function setDirections(fromAddress, toAddress, locale) {
	gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": "de_DE" });
   						       }

function handleErrors()	{
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	alert("Adresse existiert nicht (evtl. mit Latitude und Longitude versuchen)!\n Fehler: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	alert("Adresse wurde nicht gefungen (evtl. mit Latitude und Longitude versuchen)!\n Fehler: " + gdir.getStatus().code);
	   
	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	alert("Adresse vollstaendig angeben!\n Fehler: " + gdir.getStatus().code);

	else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	alert("Google Maps API Key nicht gueltig! Bitte nicht kopieren! Key kann auf http://www.google.com/apis/maps/signup.html beantragt werden! \n Fehler: " + gdir.getStatus().code);

	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	alert("Fehler bei der Berechnung. Bitte nochmal versuchen!\n Fehler: " + gdir.getStatus().code);
	    
	else alert("Unbekannter Fehler!\n Fehler: " + gdir.getStatus().code);
	   
			}

function onGDirectionsLoad()	{
	//Um an Informationen der load()-Funktion heranzukommen.
	//Bei Nichtverwendung nicht loeschen!	
				}


var Gmaps= {
	start: function(){
		
$('routethis').addEvent('click', Gmaps.setForm);
load()
  },
setForm: function() {
	$('td_dir').setStyle('width', '30%');
	$('td_map').setStyle('width', '70%');
	 $('directions').setStyle('overflow','scroll');
 	von= $('von').value;
	nach= $('nach').value;
gdir.load("from: " + von + " to: " + nach, { "locale": "de_DE" });
		       }  
  }
window.addEvent('domready', Gmaps.start);
