
    var map;
    var gdir;
    var geocoder = null;
    var addressMarker;
    var lsc;
    var locale;
    var icon1;
    var icon2;


    function initialize(x, y, zoom) {
      if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map_canvas"));
	map.addMapType(G_PHYSICAL_MAP);
	icon1=new GIcon(G_DEFAULT_ICON, "./icong1.png");
	icon2=new GIcon(G_DEFAULT_ICON, "./icong2.png");        
	icon_mid=new GIcon(G_DEFAULT_ICON, "./middle.png");  
	icon_mid.shadow="./shadow.png";	
	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);
        map.setCenter(center, zoom);


      }
    }


    function setDirections(adr1, adr2, interest) 
    {
        geocoder = new GClientGeocoder();
	if (adr1=="")
	{
	   alert("Prosze wypełnić pola z adresami");
	   return;
	}
	if (adr1=="")
	{
	   alert("Prosze wypełnić pola z adresami");
	   return;
	}

	setTimeout(function(){showAddress(adr1, adr2, 0, 0, 0, 0, interest);},100);	
    }

    function showAddress(adr1, adr2, lat1, lng1, lat2, lng2, interest) 
    {
      if (geocoder) 
      {
        geocoder.getLatLng(
          adr1,
          function(point) 
	  {
            if (!point) 
	    {
              alert("Adres :" + adr1 + " nie został rozpoznany");
            } 
            else 
            {
              //map.setCenter(point, 13);
              var marker = new GMarker(point,{title:adr1,icon:icon1} );
              map.addOverlay(marker);
              //marker.openInfoWindowHtml(adr1);

	      setTimeout(function(){showAddress2(adr1, adr2, point.y, point.x, 0, 0, interest);},700);

            }
          }
        );
      }
    }
	

    function showAddress2(adr1, adr2, lat1, lng1, lat2, lng2, interest) 
    {
      if (geocoder) 
      {
        geocoder.getLatLng(
          adr2,
          function(point) 
	  {
            if (!point) 
	    {
              alert("Adres :" + adr2 + " nie został rozpoznany");
            } 
            else 
            {
              //map.setCenter(point, 13);
              var marker = new GMarker(point,{title:adr2,icon:icon2});
              map.addOverlay(marker);
              //marker.openInfoWindowHtml(adr2);

	      setTimeout(function(){showMidPoint(adr1, adr2, lat1, lng1, point.y, point.x, interest);},1400);
            }
          }
        );
      }
    }

   function showMidPoint(adr1, adr2, lat1, lng1, lat2, lng2, interest) 
   { 	
	
    lat1 = lat1*Math.PI / 180; 
    lat2 = lat2*Math.PI / 180;
    var dLon = (lng2-lng1)*Math.PI / 180;

    var Bx = Math.cos(lat2) * Math.cos(dLon);
    var By = Math.cos(lat2) * Math.sin(dLon);

    lat3 = Math.atan2(Math.sin(lat1)+Math.sin(lat2), Math.sqrt((Math.cos(lat1)+Bx)*(Math.cos(lat1)+Bx) + By*By ) );
    lng3 = lng1*Math.PI / 180 + Math.atan2(By, Math.cos(lat1) + Bx);

    lat3=lat3* 180 / Math.PI;
    lng3=lng3* 180 / Math.PI;

    var latlng= new GLatLng(lat3, lng3);
    map.setCenter(latlng, 11);
    var marker = new GMarker(latlng,{title:"Punkt w połowie drogi",icon:icon_mid});
    map.addOverlay(marker);
	
    if (latlng) {
        geocoder.getLocations(latlng, function(addresses) {
          if(addresses.Status.code != 200) {
            alert("Problem z ustaleniem adresu w połowie drogi " + latlng.toUrlValue());
          }
          else 
	  {
            address = addresses.Placemark[0];
            var myHtml = address.address;
            map.openInfoWindow(latlng, myHtml);
	    if (interest!="")
	            lsc.execute(interest+" near "+address.address);
          }
        });
      }


   }



     function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("Nie znaleziono podanego adresu. Proszę spawdzić wszystkie podane adresy. Możliwe przyczyny:\n- popełniono błąd w pisowni szukanego miejsca\n- podano niepełną nazwę miejscowości (np. Ostrowiec zamiast Ostrowiec Wielkopolski)\n- istnieje kilka miejscowości o tej samej nazwie (należy podać województwo albo miasto w pobliżu)\n- podana nazwę bardzo małej miejscowości (rozwiązaniem może być podanie nieco większej miejscowości znajdującej się w pobliżu)\n - miejscowości przez które przebiegać ma trasa przejazdu nie są 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("Serwer Google maps jest przeciążony proszę spróbować za chwilę. \n Kod błędu: " + gdir.getStatus().code);	   
	}




