    var map;
    var gdir;
    var geocoder = null;
    var addressMarker;
    var lsc;

    function initialize(x, y, zoom) {
      if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map_canvas"));
	map.addMapType(G_PHYSICAL_MAP);
        gdir = new GDirections(map, document.getElementById("directions"));		
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
	map.addControl(new GLargeMapControl());
	map.addControl(new GOverviewMapControl());
	map.addControl(new GScaleControl());
	var mapControl = new GMapTypeControl();
        map.addControl(mapControl);
	map.enableScrollWheelZoom();
	var options = {
	      suppressZoomToBounds : false
      	};
	lsc = new google.maps.LocalSearch();
        map.addControl(lsc, new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(100,0)));
	var center = new GLatLng(x, y);
	//ads.enable();
        map.setCenter(center, zoom)

	var marker = new GMarker(center, {draggable: true});
        GEvent.addListener(marker, "dragstart", function() 
	{
		 map.closeInfoWindow();        
	});
	GEvent.addListener(marker, "dragend", function() 
	{
          alert(marker.getLatLng());	
	});

        //map.addOverlay(marker);

      }
    }


    function gotoLocation(location) 
    {
	lsc.execute(location);
    }	
    
    function setDirections(fromAddress, toAddress, viaAddresses, locale) {

	var loadStr;

	loadStr = 'from:' + fromAddress;
		
	if (viaAddresses!="")
	{
		var viaValue = viaAddresses.replace(/[\n\r]+/,"");
		var viaSteps = viaValue.split(';');
	
		for (var n = 0 ; n< viaSteps.length ; n++ ) 
		{
			loadStr += ' to: ' + viaSteps[n];
		}
	}		
	
	loadStr += ' to: ' + toAddress;
	gdir.load(loadStr,  { "locale": locale });
	//alert(loadStr);
    }


    function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("Nie znaleziono podanego adresu. Proszę spawdzić wszystkie podane adresy. Miejscowości przez które przebiegać ma trasa przejazdu muszą być oddzielone średnikami.\nKod błedu: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("Błąd serwera. Proszę spróbować później.\n Kod błedu: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("Zbyt mało parametrów wejsciowych. Proszę wypełnić wszystkie pola.\n Kod błedu: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_UNAVAILABLE_ADDRESS)
	     alert("Trasa dojazdu nie może być wyznaczona ze względów prawnych.\n Kod błędu: " + gdir.getStatus().code);	     
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("Niepoprawny kod Google Maps. \n Kod błędu: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("Żądanie wyznaczenia trasy nie mogło zostać przetworzone.\n Kod błędu: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_UNKNOWN_DIRECTIONS)
	     alert("Trasa nie może zostać wyznaczona. Proszę o wpisanie innych danych wejściowych.\n Kod błędu: " + gdir.getStatus().code);
	   else 
             alert("Wystąpił nieznany błąd.");	   
	}

function onGDirectionsLoad()
   {
	document.getElementById("getDistance").innerHTML = "Długość trasy: "+gdir.getDistance().html+ "&nbsp;Czas podróży: "+gdir.getDuration().html;
   } 
