
var map;
var geocoder;
var gdir;
var address;
var marker;
var infotext;
var fromaddress;
var toaddress;
var myart2;
var point;

function loadold(myaddress,text) {
	  if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		map.checkResize();
		map.addControl(new GLargeMapControl());
		// map.addControl(new GMapTypeControl());
		// map.addControl(new GOverviewMapControl());
		// map.addControl(new GScaleControl());
		// map.addOverlay(new GMarker(map, icon));
		
		gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
		
		var geocoder = new GClientGeocoder();
		var newcoords="";
		

		function addAddressToMap(response) {
		  map.clearOverlays();
		  if (!response || response.Status.code != 200) {
			alert("\"" + address + "\" not found");
		  } else {
			place = response.Placemark[0];
			point = new GLatLng(place.Point.coordinates[1],
								place.Point.coordinates[0]);
			marker = new GMarker(point);
			map.addOverlay(marker);
			marker.openInfoWindowHtml(place.address + '<br>' + 
			  '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
		  }
		}

		function showAddress(address) {
		  geocoder.getLatLng(
			address,
			function(point) {
			  if (!point) {
				alert("Die Adresse '"+address + "' konnte nicht gefunden werden!");
			  } else {
				map.setCenter(point, 15);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				/*marker.openInfoWindowHtml("<div style='text-align:left;' class='text12'><strong>"+text+"</strong><br>"+address+"</div>");*/
				/*
				var myicon = new GIcon();
				myicon.image = "http://www.kammermusikfest.at/img/icons/icon_google.png";
				myicon.iconSize = new GSize(32, 32);
				myicon.iconAnchor = new GPoint(20, 32);
				myicon.infoWindowAnchor = new GPoint (20, 16);
				markerOptions = { icon:myicon };
				
				var marker1 = new GMarker(point, markerOptions);
				map.clearOverlays();
				map.addOverlay(marker1);
				marker1.openInfoWindowHtml("<div style='text-align:left;' class='text12'><strong>"+text+"</strong><br>"+address+"</div>");*/
				if (address.length>0) marker.openInfoWindowHtml("<div style='text-align:left;' class='text12'><strong>"+text+"</strong><br>"+address+"</div>");
				
			  }
			}
		  );
		}
		
		showAddress(myaddress);   
		}
    }   


function load(myaddress,text) {
	  if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		
		geocoder = new GClientGeocoder();
		gdir = new GDirections(map, document.getElementById("directions"));
			
        GEvent.addListener(gdir, "error", handleErrors);

		showLocation(myaddress,text);
		}
    }    


function showAddress(response) {
      // map.clearOverlays();
      if (!response || response.Status.code != 200) {
        alert("Die angegebene Adresse konnte leider nicht gefunden werden.");
      } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
        map.setCenter(point, 15);
		toaddress=place.address;
		// alert(toaddress);
		marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml("<div style='text-align:left;' class='text12'><strong>"+infotext+"</strong><br>"+place.address+"</div>");
		map.checkResize();
      }
    }


function setFromAddressAndGo(response) {
      if (!response || response.Status.code != 200) {
        alert("Die angegebene Adresse konnte leider nicht gefunden werden.");
      } else {
		  if (document.getElementById('myart1').checked==true) myart2=G_TRAVEL_MODE_DRIVING; else myart2=G_TRAVEL_MODE_WALKING;
		/*var myart=document.getElementById('myart').value;
		if (myart==1) myart2=G_TRAVEL_MODE_DRIVING;
		if (myart==2) myart2=G_TRAVEL_MODE_WALKING;
		*/
		place2 = response.Placemark[0];
		fromaddress=place2.address;
		document.getElementById('fromAddress').value=place2.address;
		savemapaddress(place2.address);
		
		/*if (myart==3) { openWindow(); } 
		else {
			map.clearOverlays();
			gdir.load("from: " + fromaddress + " to: " + toaddress, {travelMode:myart2 });
			document.getElementById('info').style.display='';
			}*/
		map.clearOverlays();
		gdir.load("from: " + fromaddress + " to: " + toaddress, {travelMode:myart2 });
		document.getElementById('info').style.display='';
	  }
	}


function showLocation(address,text) {
    infotext=text;
	geocoder.getLocations(address, showAddress);
    }
	
	
function openWindow() {
	fensta= window.open('http://maps.google.at/maps?f=d&source=s_d&saddr='+fromaddress+'&daddr='+toaddress+'&dirflg=r&ie=latin1&start=0','extmap');
	fensta.focus();
	}


function setDirections() {
	showlong('map');
	geocoder.getLocations(document.getElementById('fromAddress').value, setFromAddressAndGo);
	}



function handleErrors(){
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	 alert("Die angegebene Adresse konnte leider nicht gefunden werden. Bitte versuchen Sie es erneut!");
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	 alert("Fehler bei der Routenberechnung!");
	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	 alert("Fehler bei der Routenberechnung!");
	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	 alert("Fehler bei der Routenberechnung!");
	else alert("Fehler bei der Routenberechnung!");
	}



function showmap(adress,text) {
	/*document.getElementById('mapcontainer').style.display='';*/
	load(adress,text);
	}
	
function showmap2(adress,text) {
	/*document.getElementById('mapcontainer').style.display='';*/
	loadold(adress,text);
	}
	
function hidemap() {
	/*document.getElementById('mapcontainer').style.display='none';
	window.location.hash="top";*/
	}