	var mapa;  
	var menu;  
	var klikniety;  
	var marker;  
	var cent;  
	var start;
	var markerStart;
	var clickedLocation;
	var menuHTML = '<div>MENU</div>'  
			+ '<a href="javascript:showMarker();">Wyznacz trasę z tego miejsca</a><br/>'  
			+ '<a href="javascript:hideMarker();">usuń znacznik trasy</a><br/>'  
			+ '<a href="javascript:poczatkowy();">widok początkowy</a><br/>'
			+ '<a href="javascript:centruj();">wyśrodkuj</a><br/>';  
    var map;
    var gdir;
    var geocoder = null;
    var addressMarker;
	
	
	jQuery(document).ready(function() {
	//jQuery(".jsToggleButton").eq(0).click(function(){jQuery('div#directionForm').toggle('long');});
	
	try
	{
		if (jQuery("div#map")!=null)
		{
			initialize();
			
			
			}
	}
	catch (e){}
	});

	function centruj() {
		center();
		jQuery(menu).css('display','none');
	}
	function poczatkowy() {
		gdir.clear();	
		jQuery(menu).css('display','none');
		gdir.clear();
		jQuery('#directions').css('display','none');
		jQuery('#map_canvas').css('width','605px');
		map.checkResize();
		center();
	}
	function hideMarker() {
		
		jQuery(menu).css('display','none');
		gdir.clear();
		jQuery('#directions').css('display','none');
		
		map.checkResize();
	}
	function showMarker() {
		
		/*cent=new GMarker(clickedLocation, {draggable: true});
		map.addOverlay(cent);
		GEvent.addListener(cent, "dragend", function(ev) {
			clickedLocation= map.fromDivPixelToLatLng(ev);
			setDirections(clickedLocation,  address, "pl_PL");
		});*/
		jQuery(menu).css('display','none');
		
		
		gdir.clear();
		
		jQuery('#directions').css('display','block');
		
		
		setDirections(clickedLocation,  address, "pl_PL");
		
	
	}
	function center() {
		geocoder=new GClientGeocoder();
		if(geocoder)
		{
			 
			geocoder.getLatLng(address,function(point){
			if(!point)
			{
				alert(address+" not found");
			}
			else
			{
				start=point;
				map.setCenter(point,13);
				marker=new GMarker(point);
				map.addOverlay(marker);
				
				
				
			}});	  
		}
	}
    function initialize() {
		
		if (GBrowserIsCompatible()) {   
			map = new GMap2(document.getElementById("map_canvas"));
			map.setUI(map.getDefaultUI()); 			
			
			map.setMapType(G_NORMAL_MAP );
			
			gdir = new GDirections(map, document.getElementById("directions"));
			
			center();
			
			GEvent.addListener(gdir, "load", onGDirectionsLoad);
			GEvent.addListener(gdir, "error", handleErrors);
			
			GEvent.addListener(map,"click",function(p){
			menu.style.display = "none";
			
			});
		
			GEvent.addListener(map,"singlerightclick",function(p) {  
				klikniety = map.fromDivPixelToLatLng(p);  
				var menu_x = p.x;  
				var menu_y = p.y;  
				clickedLocation = klikniety;


				// jeśli kliknięto zbyt blisko brzegu ekranu  
				if(p.x > map.getSize().width - 183)  
				menu_x = map.getSize().width - 183;  

				if(p.y > map.getSize().height - 140)  
				menu_y = map.getSize().height - 140;  

				var pozycja = new GSize(menu_x,menu_y);  

				var pokazMenu = new GControlPosition(G_ANCHOR_TOP_LEFT, pozycja);  
				pokazMenu.apply(menu);  
				
				menu.style.display = "block";  
			}); 
			
			menu = document.createElement("div");   		
			menu.innerHTML = menuHTML;
			jQuery(menu).css('display','none');
			jQuery(menu).addClass('mapMenu');
			map.getContainer().appendChild(menu); 
		}
    }


    function showRoute(fromAddress, toAddress, locale) {
		geocoder=new GClientGeocoder();
		if(geocoder)
		{
			 
			geocoder.getLatLng(address,function(fromAddress){
			if(!fromAddress)
			{
				alert(address+" not found");
			}
			else
			{
				start=fromAddress;				
			}});	
			geocoder.getLatLng(address,function(toAddress){
			if(!toAddress)
			{
				alert(address+" not found");
			}
			else
			{
				end=toAddress;				
			}});		
		}
		
		jQuery('#directions').css('display','block');
		
		
		setDirections(fromAddress, toAddress, locale);
	}
    function setDirections(fromAddress, toAddress, locale) {
      gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": locale });
    }
	jQuery(window).unload( function () { GUnload(); } );

    function handleErrors(){
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
      alert("Nie odnaleziono adresu. Być może wprowadzony adres jest stosunkowo nowy" );
    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.");
    poczatkowy();
}

function onGDirectionsLoad(){
      // Use this function to access information about the latest load()
      // results.

      // e.g.
      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
   // and yada yada yada...
}