var map;
var gdir;
var geocoder = null;
var addressMarker;
var fromAddress;
var toAddress;
var locale = "de";


function initializePrint() {

 
	var MyUrl = String(window.location);
	
	var arg = MyUrl.split("#");
	var argtmp = arg[1].split("_");
	fromAddress = argtmp.join(" ");
	map1 = new google.maps.Map2($("map1"));
	map1.addControl(new GSmallMapControl());
	map1.addControl(new GMapTypeControl());
	map1.setCenter(new GLatLng(49, 9), 8);
	gdir = new GDirections(map1, $("directions"));

	map2 = new google.maps.Map2($("map2"));
	map2.addControl(new GSmallMapControl());
	map2.addControl(new GMapTypeControl());
	map2.setCenter(new GLatLng(49, 9), 8);
	
	map3 = new google.maps.Map2($("map3"));
	map3.addControl(new GSmallMapControl());
	map3.addControl(new GMapTypeControl());
	map3.setCenter(new GLatLng(49, 9), 8);	
        
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
	geocoder = new GClientGeocoder();
	
	showAddress(address,map3,0,15); 
	showAddress(fromAddress,map2,0,15); 
	setDirectionsPrint(fromAddress,map1)
}


    
function setDirections(fromAddress) {
	if ( fromAddress != 'Ihre Adresse' ) {
		setPrintUrl(fromAddress);

		if ( this.location.href.search(/print.+/) > 1 ) {
			$('directions').setStyle('height','auto');
			$('directions').setStyle('overflow-y','auto');
			$('directions').setStyle('overflow','auto');
		} else {
			$('directions').setStyle('height',300);
		}
		
		$('directions').setStyle('margin-bottom',10);
		gdir.load("from: " + fromAddress + " to: " + address,
			{ "locale": locale });
	}
	else {
		alert('Bitte erst eine Startadresse eingeben');
	}	
}

function setDirectionsPrint(fromAddress, map) {
	showAddress(address,map,1,13);
	gdir.load("from: " + fromAddress + " to: " + address,
		{ "locale": locale });
}

    function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

	//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	     
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	   else alert("An unknown error occurred.");
	   
	   $('directions_wrapper').className="hidden";

	   
	}
	
function openPrintWindow() {
	fromAddress = $('fromAddress').value;
	if ( fromAddress != 'Ihre Adresse' ) {
		var target = Date.parse("Tue, 1 Jan 2000 00:00:00 GMT");
		var argtmp = fromAddress.split(" ");
		fromAddress = argtmp.join("_");
		var url = 'drucken.html#'+fromAddress;
		var printwindow = window.open(url,target,'menubar=yes,scrollbars=yes,resizable=yes,width=950,height=650');
		printwindow.focus();
	}
	else {
		alert('Bitte erst eine Startadresse eingeben');
	}
}



function str_replace(search, replace, subject) {
    return subject.split(search).join(replace);
}

	function onGDirectionsLoad(){ 
      // Use this function to access information about the latest load()
      // results.

      // e.g.
      // $("getStatus").innerHTML = gdir.getStatus().code;
	  // and yada yada yada...
	} 



function setPrintUrl(fromAddress) {

	var adress_url = this.location.href.split("print.htm");

	if ( typeof(adress_url) == 'array' && adress_url.length < 2 ) {

		var el = $('print');
		var link = el.getFirst();

		var href = link.getProperty('href');
		if ( href != null ) {
			href = str_replace("print.html", "print.html#"+fromAddress, href);
			link.setProperty('href',href);
		}
	}
}


function showAddress() {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
        map.setCenter(point, 13);
        var marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml('<b>'+location_name+'</b><br />'+location_str + '<br />' + location_plz + ' ' + location_city);
      }
    }
  );
}

var address = location_str + ', ' + location_plz + ' ' + location_city + ', Germany';
var mapDIV = $('mapWrapper');
var fromAddress = $('fromAddress').getProperty('value');


window.addEvent('domready', function(){
	var adress_url = this.location.href.split("#");

	if ( adress_url.length > 1 ) {
		fromAddress = unescape(adress_url[1].replace(/\+/g,  " "));
	

		if ( fromAddress != '' && typeof(fromAddress) == 'string' ) {

			$('fromAddress').setProperty('value',fromAddress);
			setDirections(fromAddress);
		}
	}

	if ( adress_url[0].search(/print.+/) > 1 ) {
	
	}
});




if (GBrowserIsCompatible()) {
	map = new GMap2($("map"));
	geocoder = new GClientGeocoder();
	showAddress();
        gdir = new GDirections(map, $("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);	
}
