var map = ''; var directionsPanel; var directions; 
var home = new Array(); var routeNodes = new Array();
var htmlInfoBox;
var geocoder; var point; var marker;

function initialize(ltnlng, address) {
   if (GBrowserIsCompatible()) {
	
	point = ltnlng.split(',');
	point = new GLatLng(point[0], point[1]);
	
	map = new GMap2(document.getElementById("map_canvas"));
	var customUI = map.getDefaultUI();
	map.setUI(customUI);
	map.setCenter(point, 15);
	
	address = address.split(',');
	htmlInfoBox = "<h1 class='csc-firstHeader' style='padding:0;'>Adresse:</h1><p class='bodytext' style='padding:0; margin:0;'>"+ address[0] +"</p><p class='bodytext' style='padding:0; margin:0;'>"+ address[1] +", Deutschland</p><p style='padding:0;'></p>";

	marker = new GMarker(point);
	map.addOverlay(marker);
	marker.openInfoWindowHtml(htmlInfoBox);
	var info = map.getInfoWindow(htmlInfoBox);
	GEvent.addListener(marker, "click", function() {
		if (info.isHidden()){ marker.openInfoWindowHtml(htmlInfoBox); } else { info.hide(); }
	}); 
   } else {
	jQuery('#map_canvas').append('<iframe width="190" height="190" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.de/maps?f=q&amp;source=s_q&amp;hl=de&amp;geocode=&amp;q='+ home[0] +'&amp;ie=UTF8&amp;z=15&amp;output=embed"></iframe>');
   }
   
   jQuery('#routeSubmit').parent('form').submit(function(){
	   showAddress(jQuery("#routeStart").val(), address);
	   return false;
   });
}

function showAddress(addressStart, addressEnd) {
	//empty sidebox
	jQuery('#tv_page_col2').empty();
	
	// create div structure for the routeForm
	jQuery('#tv_page_col2').append('<div class="box_shadow"><div class="box_shadow_top"></div><div class="box_shadow_middle"></div><div class="box_shadow_bottom"></div></div>');
	jQuery('.box_shadow_middle').append('<div class="box_shadow_content"><div class="csc-header csc-header-n1"><h1 claqss="csc-firstHeader">Routenplaner</h1></div><div id="route"></div></div>');
	
	
	map.clearOverlays();
	jQuery("#route").empty();
	
	routeNodes[0] = addressStart;
	routeNodes[1] = addressEnd;
	var query = "from: "+ addressStart +" to:"+ addressEnd;
	directionsPanel = document.getElementById("route");
	directions = new GDirections(map, directionsPanel);
	GEvent.addListener(directions, "error", handleError);
	GEvent.addListener(directions, "load", function(e){
		var polyline = e.getPolyline();
		GEvent.addListener(polyline, "click", function(e){
			alert("poly click");
		});
	});
	directions.load(query);
    
	return false;
}

function handleError() {
	if (directions.getStatus().code !== G_GEO_SUCCESS) {
		jQuery('#route').append('<div id="routeError" style=";margin-top:13px;border:0px none;"><p>Die Route konnte nicht errechnet werden.<br/>Pr&uuml;fen Sie bitte Ihre Eingabe.</p><br/><h2>Meinten Sie:</h2><div id="start"></div><div id="end"></div></div>');

		if(!geocoder)
				geocoder = new GClientGeocoder();
		geocoder.getLocations(routeNodes[0], function(response){
			jQuery.each(response.Placemark, function(){
				if(this.address != routeNodes[0]) {
					jQuery('#start').append('<p style="cursor:pointer;text-decoration:underline;" class="error" onclick="showAddress(\''+this.address+'\', \''+routeNodes[1]+'\'); jQuery(\'#addressStart\').val(\''+this.address+'\');">'+ this.address +'</p>');
				} else {
					jQuery('#start').append('<p class="green">'+ this.address +'</p>');
				}
			});
	    });	
	}
}

