	var map = null;
    var geocoder = null;
	var bounds = new GLatLngBounds();

    function initialize() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(51.919438, 19.145136), 5);
		map.enableScrollWheelZoom();
        geocoder = new GClientGeocoder();
//        GEvent.addListener(map, "click", clicked);
		loaded();
      }
    }

	function zoomfit() {
		newzoom = map.getBoundsZoomLevel(bounds);
		newcenter = bounds.getCenter();
		map.setCenter (newcenter,newzoom);
	}

    function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert("Niestety, adres '" + address + "' nie został znaleziony.");
            } else {
     	     map.panTo(point);
			 var marker = new GMarker(point);
			 GEvent.addListener(marker,'click',function(){
				window.location += '&city_id=0';
			 });

	         map.addOverlay(marker);
			 bounds.extend(point);
			 zoomfit();
        }
      });
    }
  }

    function clicked(overlay, latlng) {
      if (latlng) {
        geocoder.getLocations(latlng, function(addresses) {
          if(addresses.Status.code != 200) {
            alert("Geocoder nie znalazł adresu " + latlng.toUrlValue());
          }
          else {
            address = addresses.Placemark[0];
            var myHtml = address.address;
            map.openInfoWindow(latlng, myHtml);
          }
        });
      }
    }
