//<![CDATA[

    var map = null;

    function resizeMap(){
        if (map!=null) map.checkResize();
    }

    function createMarker(point,html) {
        marker = new GMarker(point, {draggable: true});

        GEvent.addListener(marker, "dragend", function() {
            $("#lat").attr('value', marker.getLatLng().lat());
            $("#lng").attr('value',marker.getLatLng().lng());
            marker.openInfoWindowHtml(address);
        });

        GEvent.addListener(marker, "dragstart", function() {
            map.closeInfoWindow();
        });
    }

    function setGoogleMap(x, y, html){

        if (GBrowserIsCompatible()) {

          // A function to create the marker and set up the event window
          // Dont try to unroll this function. It has to be here for the function closure
          // Each instance of the function preserves the contends of a different instance
          // of the "marker" and "html" variables which will be needed later when the event triggers.


          // Display the map, with some controls and set the initial location
          map= new GMap2(document.getElementById("googlemap"));
         // map.addControl(new GLargeMapControl());
         // map.addControl(new GMapTypeControl());
          map.setCenter(new GLatLng(x,y),15);

          // Set up three markers with info windows

          var point = new GLatLng(x,y);
          createMarker(point,html);
          map.addOverlay(marker);

        }

        // display a warning if the browser was not compatible
        else {
          alert("Sorry, the Google Maps API is not compatible with this browser");
        }
    }

    function setGoogleMapFromAddress(address, html, lat, lng) {
       if (GBrowserIsCompatible()) {
         map = new GMap2(document.getElementById("googlemap"));
         geocoder = new GClientGeocoder();
         showAddress(address, html, lat, lng, true);
         resizeMap();
       }
     }

    function setGoogleMapFromAddressView(address, html, lat, lng) {
       if (GBrowserIsCompatible()) {
         map = new GMap2(document.getElementById("googlemap"));
         geocoder = new GClientGeocoder();
         showAddress(address, html, lat, lng, false);
         resizeMap();
       }
     }

     function showAddress(address, html, lat, lng, edit) {
       if(lat != null && lng != null) {
        var point = new GLatLng(lat, lng);
        map.addControl(new GLargeMapControl());
        map.setCenter(point, 15);
        if(edit) {
            createMarker(point, html);
        } else {
            marker = new GMarker(point);
        }
        map.addOverlay(marker);
        $("#googlemap").css('margin-left', '0');

       } else {
           geocoder.getLatLng(
             address,
             function(point) {
                if (!point || point == null) {
                 //mapa niedostepna - wyswietlamy mape Polski z mozliwoscia wyboru lokacji
                 $("#googlemap_error").css('display', '');
                 if(edit) {
                     geocoder.getLatLng(
                        "PL",
                        function(point) {
                            map.addControl(new GLargeMapControl());
                            map.setCenter(point, 6);
                            if(edit) {
                                createMarker(point, html);
                            } else {
                                marker = new GMarker(point);
                            }
                            map.addOverlay(marker);
                        }
                     );
                     $("#googlemap").css('margin-left', '0');
                 } else {
                    $("#googlemap").css('margin-left', '0');
                    $("#googlemap").empty().css({'border':'1px solid #D0D0D0'}).append("<p style='margin:6px;'>Brak lokalizacji na mapie.</p>");
                 }
               } else {
                 $("#googlemap").css('margin-left', '0');
                 map.addControl(new GLargeMapControl());
                 map.setCenter(point, 15);
                 if(edit) {
                    createMarker(point, html);
                 } else {
                    marker = new GMarker(point);
                 }
                 map.addOverlay(marker);

               }
             }
           );
       }
     }

    // This Javascript is based on code provided by the
    // Blackpool Community Church Javascript Team
    // http://www.commchurch.freeserve.co.uk/
    // http://econym.googlepages.com/index.htm

    //]]>

