/* <![CDATA[ */
    
    jQuery(function() {
        initialize_autofill_address();
    });
    
    var placeSearch, autocomplete;
    var componentForm = {
        street_number: 'short_name',
        route: 'long_name',
        locality: 'long_name',
        administrative_area_level_1: 'short_name',
        country: 'long_name',
        postal_code: 'short_name'
    };

    function initialize_autofill_address() {

        var options = {
        types: ['geocode'],
        
        };

        if (typeof google !== 'undefined' && typeof google.maps !== 'undefined') {
            // Create the autocomplete object, restricting the search
            // to geographical location types.
            autocomplete = new google.maps.places.Autocomplete(
                /** @type {HTMLInputElement} */
                (document.getElementById('post_address')), options);
            // When the user selects an address from the dropdown,
            // populate the address fields in the form.
            google.maps.event.addListener(autocomplete, 'place_changed', function() {
                fillInAddress();
            });
        }
    }

    // [START region_fillform]
    function fillInAddress() {
        // Get the place details from the autocomplete object.
        var place = autocomplete.getPlace();

        //blank fields
        jQuery('#post_country option[value=""]').attr("selected",true);
        jQuery("#post_country").trigger("chosen:updated");

        if (!jQuery('#post_region option[value=""]').length) {
            jQuery("#post_region").append('<option value="">Select Region</option>');
        }
        jQuery('#post_region option[value=""]').attr("selected",true);
        jQuery("#post_region").trigger("chosen:updated");

        if (!jQuery('#post_city option[value=""]').length) {
            jQuery("#post_city").append('<option value="">Select City</option>');
        }
        jQuery('#post_city option[value=""]').attr("selected",true);
        jQuery("#post_city").trigger("chosen:updated");

        jQuery('#post_zip').val('');

        var newArr = new Array();
        newArr[0] = place;
        geocodeResponse(newArr);

        if (place.name) {
        jQuery('#post_address').val(place.name);
        }

        geodir_codeAddress(true);

    }
    // [END region_fillform]
    function geodir_region_fix(ISO2, ZIP, prefix) {
    var _wpnonce = jQuery('#gd_location').closest('form').find('#_wpnonce').val();

    jQuery.post("http://www.new-brennanhealingscience.org/directory/wp-content/plugins/geodir_location_manager/zip_arrays/"+ISO2+".php", {
        ISO2: ISO2, ZIP:ZIP
    }).done(function(data) {
        if (data) {
            getState =  data;

            if (getState) {
                if (jQuery("#"+prefix+"region option:contains('"+getState+"')").length == 0) {
                    jQuery("#"+prefix+"region").append('<option value="'+getState+'">'+getState+'</option>');
                }

                jQuery('#'+prefix+'region option[value="'+getState+'"]').attr("selected",true);
                jQuery("#"+prefix+"region").trigger("chosen:updated");
            }
        }
    });
}
    if ((window.gdSetMap=='google' || window.gdSetMap=='auto') && window.google && typeof google.maps!=='undefined') {
        gdMaps = 'google';
    } else if ((window.gdSetMap=='osm' || window.gdSetMap=='auto') && typeof L!=='undefined' && typeof L.version!=='undefined') {
        gdMaps = 'osm';
    } else {
        gdMaps = null;
    }
    window.gdMaps = window.gdMaps || gdMaps;
    
    user_address = false;
    jQuery('#post_address').keypress(function () {
        user_address = true;
    });

    baseMarker = '';
    geocoder = '';
    var post_CITY_MAP_CENTER_LAT = 40.7127837;
    var post_CITY_MAP_CENTER_LNG = -74.00594130000002;
        var post_CITY_MAP_ZOOMING_FACT = 12;
    var minZoomLevel = 0;
    var oldstr_address;
    var oldstr_zip;
    var strictBounds;
    function geocodePosition(latLon, address) {
        console.log(address);
        if (address && (address.locality || address.postal_town) && address.country!='TR' && address.country!='SG' ) {// turkey select address does not return enough info so we get info from GPS only.
            doGeoCode = address;
        } else {
            doGeoCode = {
                latLng: baseMarker.getPosition()
            };
        }

        geocoder.geocode(doGeoCode, function (responses) {
            geocodeResponse(responses)
        });
    }
    
    function geocodeResponse(responses) {
        console.log(responses);//keep this for debugging
        if (responses && responses.length > 0) {
            var getAddress = '';
            var getZip = '';
            var getCity = '';
            var getState = '';
            var getCountry = '';
            getCountryISO = '';

            street_number = '';
            premise = ''; // In Russian ;
            establishment = '';
            route = '';
            administrative_area_level_1 = '';
            administrative_area_level_2 = '';
            administrative_area_level_3 = '';
            sublocality_level_1 = '';
            postal_town = '';
            locality = '';
            country = '';
            postal_code = '';
            postal_code_prefix = '';
            rr = '';
            has_address_been_set = false;

            // get the proper response as somtimes the GPS results will return names in English when they should not.
            responses.forEach(function(response) {
                if(response.types[0] == "locality"){

                    for (var i = 0; i < response.address_components.length; i++) {
                        var addr = response.address_components[i];
                        if (addr.types[0] == 'administrative_area_level_1') {
                            administrative_area_level_1 = addr;
                        }
                        if (addr.types[0] == 'administrative_area_level_2') {
                            administrative_area_level_2 = addr;
                        }
                        if (addr.types[0] == 'administrative_area_level_3') {
                            administrative_area_level_3 = addr;
                        }
                        if (addr.types[0] == 'sublocality_level_1') {
                            sublocality_level_1 = addr;
                        }
                        if (addr.types[0] == 'postal_town') {
                            postal_town = addr;
                        }
                        if (addr.types[0] == 'locality') {
                            locality = addr;
                        }
                    }

                }
            });

            
            for (var i = 0; i < responses[0].address_components.length; i++) {
                var addr = responses[0].address_components[i];
                if (addr.types[0] == 'street_number') {
                    street_number = addr;
                }
                if (addr.types[0] == 'route') {
                    route = addr;
                }
                if (addr.types[0] == 'premise') {
                    premise = addr;
                }
                if (administrative_area_level_1 == '' && addr.types[0] == 'administrative_area_level_1') {
                    administrative_area_level_1 = addr;
                }
                if (administrative_area_level_2 == '' && addr.types[0] == 'administrative_area_level_2') {
                    administrative_area_level_2 = addr;
                }
                if (administrative_area_level_3 == '' && addr.types[0] == 'administrative_area_level_3') {
                    administrative_area_level_3 = addr;
                }
                if (sublocality_level_1 == '' && addr.types[0] == 'sublocality_level_1') {
                    sublocality_level_1 = addr;
                }
                if (postal_town == '' && addr.types[0] == 'postal_town') {
                    postal_town = addr;
                }
                if (locality == '' && addr.types[0] == 'locality') {
                    locality = addr;
                }
                if (addr.types[0] == 'country') {
                    country = addr;
                }
                if (addr.types[0] == 'postal_code') {
                    postal_code = addr;
                }
                if (addr.types[0] == 'postal_code_prefix') {
                    postal_code_prefix = addr;
                }

                if (addr.types[0] == 'establishment') {
                    establishment = addr;
                }

                if (postal_code == '') {
                    postal_code = postal_code_prefix;
                }
                if (responses[0].formatted_address != '') {
                    address_array = responses[0].formatted_address.split(",", 2);
                    
                    if (address_array.length > 1) {
                        if (!(typeof(street_number.long_name) == 'undefined' || street_number.long_name == null) && street_number.long_name.toLowerCase() == address_array[0].toLowerCase().trim()) {
                            getAddress = street_number.long_name + ', ' + address_array[1];
                        }

                        if (getAddress == '' && !(typeof(street_number.long_name) == 'undefined' || street_number.long_name == null) && street_number.long_name.toLowerCase() == address_array[1].toLowerCase().trim()) {
                            getAddress = address_array[0] + ', ' + street_number.long_name;
                        }

                        if (getAddress == '' && !(typeof(street_number.short_name) == 'undefined' || street_number.short_name == null) && street_number.short_name.toLowerCase() == address_array[0].toLowerCase().trim()) {
                            getAddress = street_number.short_name + ', ' + address_array[1];
                        }

                        if (getAddress == '' && !(typeof(street_number.short_name) == 'undefined' || street_number.short_name == null) && street_number.short_name.toLowerCase() == address_array[1].toLowerCase().trim()) {
                            getAddress = address_array[0] + ', ' + street_number.short_name;
                        }

                        if (getAddress == '' && !(typeof(premise.long_name) == 'undefined' || premise.long_name == null) && premise.long_name.toLowerCase() == address_array[0].toLowerCase().trim()) {
                            getAddress = premise.long_name + ', ' + address_array[1];
                        }

                        if (getAddress == '' && !(typeof(premise.long_name) == 'undefined' || premise.long_name == null) && premise.long_name.toLowerCase() == address_array[1].toLowerCase().trim()) {
                            getAddress = address_array[0] + ', ' + premise.long_name;
                        }

                        if (getAddress == '' && !(typeof(premise.short_name) == 'undefined' || premise.short_name == null) && premise.short_name.toLowerCase() == address_array[0].toLowerCase().trim()) {
                            getAddress = premise.short_name + ', ' + address_array[1];
                        }

                        if (getAddress == '' && !(typeof(premise.short_name) == 'undefined' || premise.short_name == null) && premise.short_name.toLowerCase() == address_array[1].toLowerCase().trim()) {
                            getAddress = address_array[0] + ', ' + premise.short_name;
                        }

                        if (getAddress == '') {
                            getAddress = 'none'
                        }
                    }
                }
            }

            // if establishment then grab second arr
            if (getAddress == 'none' && typeof(establishment.long_name) !== 'undefined' && typeof(address_array[1]) !== 'undefined') {
                getAddress = address_array[1];
            } else {
                getAddress = address_array[0];
            }

            if (getAddress == '') {
                if (street_number.long_name)
                    getAddress += street_number.long_name + ' ';//street_number
                if (route.long_name)
                    getAddress += route.long_name;//route
            }

            getZip = postal_code.long_name;//postal_code

            //getCountry
            if (country.long_name) {
                getCountry = country.long_name;
            }
            if (country.short_name) {
                getCountryISO = country.short_name;
            }

            //getState
            if (country.short_name) {
                rr = country.short_name;
            }


            //$country_arr = ["US", "CA", "IN","DE","NL"];
            // fix for regions in GB

            $country_arr = ["GB","ES"];
            if (jQuery.inArray(rr, $country_arr) !== -1) {
                if (administrative_area_level_2.long_name) {
                    getState = administrative_area_level_2.long_name;
                }
                else if (administrative_area_level_1.long_name) {
                    getState = administrative_area_level_1.long_name;
                }
            } else {
                if (administrative_area_level_1.long_name) {
                    getState = administrative_area_level_1.long_name;
                }
                else if (administrative_area_level_2.long_name) {
                    getState = administrative_area_level_2.long_name;
                }
            }

            // fix some countries without regions, Isle of Man, Singapore
            if(getCountryISO=='IM'){
                getState = "Isle of Man";
            }else if(getCountryISO=='SG'){
                getState = "Singapore";
            }

            //getCity
            // fix for cities in Ireland
            $country_arr2 = ["IE"];
            if (jQuery.inArray(rr, $country_arr2) !== -1) {
                if (administrative_area_level_2.long_name && administrative_area_level_2.long_name.indexOf(" City") >= 0) {
                    getCity = administrative_area_level_2.long_name;
                }
                else if (locality.long_name) {
                    getCity = locality.long_name;
                }
                else if (postal_town.long_name) {
                    getCity = postal_town.long_name;
                }
                else if (sublocality_level_1.long_name) {
                    getCity = sublocality_level_1.long_name;
                }
                else if (administrative_area_level_3.long_name) {
                    getCity = administrative_area_level_3.long_name;
                }
            } else if(rr=="TR") {
                if (locality.long_name) {
                    getCity = locality.long_name;
                }else if (postal_town.long_name) {
                    getCity = postal_town.long_name;
                }
                else if (sublocality_level_1.long_name) {
                    getCity = sublocality_level_1.long_name;
                }
                else if (administrative_area_level_3.long_name) {
                    getCity = administrative_area_level_3.long_name;
                }
                else if (administrative_area_level_1.long_name) {
                    getCity = administrative_area_level_1.long_name;
                }
            }else if(rr=="FR") {

                if (administrative_area_level_2.long_name=='Paris') {
                    getCity = administrative_area_level_2.long_name;
                }else{
                    if (locality.long_name) {
                        getCity = locality.long_name;
                    }else if (postal_town.long_name) {
                        getCity = postal_town.long_name;
                    }
                    else if (sublocality_level_1.long_name) {
                        getCity = sublocality_level_1.long_name;
                    }
                    else if (administrative_area_level_3.long_name) {
                        getCity = administrative_area_level_3.long_name;
                    }
                    else if (administrative_area_level_1.long_name) {
                        getCity = administrative_area_level_1.long_name;
                    }
                }
            }else {
                if (locality.long_name) {
                    getCity = locality.long_name;
                }else if (postal_town.long_name) {
                    getCity = postal_town.long_name;
                }
                else if (sublocality_level_1.long_name) {
                    getCity = sublocality_level_1.long_name;
                }
                else if (administrative_area_level_3.long_name) {
                    getCity = administrative_area_level_3.long_name;
                }
            }

            //getCountry 
            if (country.long_name) {
                getCountry = country.long_name;
            }
            //getZip 
            if (postal_code.long_name) {
                getZip = postal_code.long_name;
            }
             
window.neighbourhood = '';
window.gdGeo = true;
if (window.gdMaps == 'google' && typeof responses != 'undefined' && responses && responses.length > 0) {    
    for (var j=0; j < responses[0].address_components.length; j++) {
        var component = responses[0].address_components[j];
        if (component.types[0] == 'neighbourhood' || component.types[0] == 'neighborhood') {
            window.neighbourhood = component.long_name;
        }
    }
} 
                        updateMarkerAddress(getAddress, getZip, getCity, getState, getCountry);
        } else {
            updateMarkerAddress('Cannot determine address at this location.');
        }
    }
    function centerMap(latlng) {
        jQuery("#post_map").goMap();
        if (window.gdMaps == 'google') {
            jQuery.goMap.map.panTo(baseMarker.getPosition());
        } else if (window.gdMaps == 'osm') {
            latlng = latlng ? latlng : baseMarker.getLatLng();
            jQuery.goMap.map.panTo(latlng);
        }
    }
    function centerMarker() {
        jQuery("#post_map").goMap();
        var center = jQuery.goMap.map.getCenter();
        if (window.gdMaps == 'google') {
            baseMarker.setPosition(center);
        } else if (window.gdMaps == 'osm') {
            baseMarker.setLatLng(center);
        }
    }
    function updateMapZoom(zoom) {
        jQuery('#post_mapzoom').val(zoom);
    }
    function updateMarkerPosition(markerlatLng) {
        jQuery("#post_map").goMap();
        jQuery('#post_latitude').val(markerlatLng.lat());
        jQuery('#post_longitude').val(markerlatLng.lng());
    }
    function updateMarkerPositionOSM(markerlatLng) {
        jQuery('#post_latitude').val(markerlatLng.lat);
        jQuery('#post_longitude').val(markerlatLng.lng);
    }
    function updateMarkerAddress(getAddress, getZip, getCity, getState, getCountry) {
        var set_map_val_in_fields = '1';
                var old_country = jQuery("#post_country").val();
        var old_region = jQuery("#post_region").val();
        
        if (user_address == false || jQuery('#post_address').val() == '') {
            jQuery("#post_address").val(getAddress);
        }
        if (getAddress) {
            oldstr_address = getAddress;
        }

        jQuery("#post_zip").val(getZip);
        if (getZip) {
            oldstr_zip = getZip;
        }
        if (set_map_val_in_fields) {
            if (getCountry) {
                jQuery('#post_country option[value=""]').attr("selected",false);
                jQuery('#post_country option[data-country_code="' + getCountryISO + '"]').attr("selected", true);
                jQuery("#post_country").trigger("chosen:updated");
            }
            if (getState) {
                if (jQuery('input[id="post_region"]').attr('id')) {
                    jQuery("#post_region").val(getState);
                }
            }
            if (getCity) {
                if (jQuery('input[id="post_city"]').attr('id')) {
                    jQuery("#post_city").val(getCity);
                }
            }
        }
        
        var error = false;
        var loc_error_checking_start_count = 0;
        var loc_error_checking_end_count = 0;
                if (getCountry && getCity && error == false) {
        jQuery.post("http://www.new-brennanhealingscience.org/directory/wp-admin/admin-ajax.php?action=geodir_locationajax_action&location_ajax_action=set_region_on_map", {
        country: getCountry,
        state: getState,
        city: getCity
        }).done(function(data) {
        if (jQuery.trim(data) != '') {
        getState = data;
        }

                });
        }

        function gd_location_error_done() {
            if (loc_error_checking_start_count != loc_error_checking_end_count) {
                setTimeout(function() {
                    gd_location_error_done();
                }, 100);
            } else {
                if (error == false) {
                    var mapLang = 'en';
                    var countryChanged = jQuery.trim(old_country) != jQuery.trim(getCountry) ? true : false;
                    old_country = jQuery.trim(old_country);
                    if (mapLang != 'en' && old_country) {
                                                var oldISO2 = jQuery('#post_country option[value="' + old_country + '"]').attr('data-country_code');
                                                if (oldISO2 && oldISO2 == getCountryISO) {
                            countryChanged = false;
                        }
                    }

                    if (countryChanged) {
                        jQuery('#post_region option').remove();

                        if (jQuery("#post_region option:contains('" + getState + "')").length == 0) {
                            jQuery("#post_region").append('<option value="' + getState + '">' + getState + '</option>');
                        }

                        jQuery('#post_region option[value="' + getState + '"]').attr("selected", true);
                        jQuery("#post_region").trigger("chosen:updated");

                        jQuery('#post_city option').remove();

                        if (jQuery("#post_city option:contains('" + getCity + "')").length == 0) {
                            jQuery("#post_city").append('<option value="' + getCity + '">' + getCity + '</option>');
                        }

                        jQuery('#post_city option[value="' + getCity + '"]').attr("selected", true);
                        jQuery("#post_city").trigger("chosen:updated");
                    }

                    if (jQuery.trim(old_region) != jQuery.trim(getState)) {
                        jQuery('#post_city option').remove();

                        if (jQuery("#post_city option:contains('" + getCity + "')").length == 0) {
                            jQuery("#post_city").append('<option value="' + getCity + '">' + getCity + '</option>');
                        }

                        jQuery('#post_city option[value="' + getCity + '"]').attr("selected", true);
                        jQuery("#post_city").trigger("chosen:updated");
                    }

                    if (getCountry) {
                        jQuery('#post_country option[value="' + getCountry + '"]').attr("selected", true);
                        jQuery("#post_country").trigger("chosen:updated");
                    }

                    if (getZip) {
                        if (getCountryISO == 'SK' || getCountryISO == 'TR' || getCountryISO == 'DK' || getCountryISO == 'ES' || getCountryISO == 'CZ') {
                            geodir_region_fix(getCountryISO, getZip, 'post_');
                        }
                    }
                    
                    var $chosen_supported = geodir_lm_chosen_supported();
                                        if (!$chosen_supported) {
                        var $new_loc = jQuery("#post_region_new");
                        if (!$new_loc.attr('id')) {
                            var $new_loc_html = '<div style="display:none" class="gd-new-loc-div gd-new-region-div"><input field_type="address" placeholder="Add new region (if not in the list)" id="post_region_new" class="geodir_textfield" value="" type="text" /><input  onclick="geodir_lm_type_new_location(this, \'region\');" value="Add" type="button" /></div>';
                            jQuery("#post_region").after($new_loc_html);
                        }
                    }
                                                            if (!$chosen_supported) {
                        var $new_loc = jQuery("#post_city_new");
                        if (!$new_loc.attr('id')) {
                            var $new_loc_html = '<div style="display:none" class="gd-new-loc-div gd-new-city-div"><input field_type="address" placeholder="Add new city (if not in the list)" id="post_city_new" class="geodir_textfield" value="" type="text" /><input  onclick="geodir_lm_type_new_location(this, \'city\');" value="Add" type="button" /></div>';
                            jQuery("#post_city").after($new_loc_html);
                        }
                    }
                    
                    if (getState) {
                        jQuery('.gd-new-region-div').hide();
                        
                        if (jQuery("#post_region option:contains('" + getState + "')").length == 0) {
                            jQuery("#post_region").append('<option value="' + getState + '">' + getState + '</option>');
                        }

                        jQuery('#post_region option[value="' + getState + '"]').attr("selected", true);
                        jQuery("#post_region").trigger("chosen:updated");
                    } else {
                                                if (!$chosen_supported) {
                            jQuery('.gd-new-region-div').show();
                            jQuery('.gd-new-region-div [field_type="address"]').val("");
                        }
                                            }

                    if (getCity) {
                        jQuery('.gd-new-city-div').hide();
                        
                        if (jQuery("#post_city option:contains('" + getCity + "')").length == 0) {
                            jQuery("#post_city").append('<option value="' + getCity + '">' + getCity + '</option>');
                        }

                        jQuery('#post_city option[value="' + getCity + '"]').attr("selected", true);
                        jQuery("#post_city").trigger("chosen:updated");

                        jQuery('select.geodir_location_add_listing_chosen').each(function () {
                            if (jQuery(this).attr('id') == 'post_city') {
                                jQuery(this).change();
                            }
                        });
                    } else {
                                                if (!$chosen_supported) {
                            jQuery('.gd-new-city-div').show();
                            jQuery('.gd-new-city-div [field_type="address"]').val("");
                        }
                                            }
                                        if (window.neighbourhood) {
                        var $neighbourhood = jQuery('#post_neighbourhood');
                        if ($neighbourhood.find('option:contains("' + window.neighbourhood + '")').length == 0) {
                            $neighbourhood.append('<option value="' + window.neighbourhood + '">' + window.neighbourhood + '</option>');
                        }
                        $neighbourhood.find('option[value="' + window.neighbourhood + '"]').attr("selected", true);
                        $neighbourhood.trigger("chosen:updated");
                    }
                                    } else {
                    geodir_set_map_default_location('post_map', '40.7127837', '-74.00594130000002');
                    return false;
                }

                if (error) {
                    geodir_set_map_default_location('post_map', '40.7127837', '-74.00594130000002');
                    return false;
                }
            }
        }

        gd_location_error_done();
            //geodir_get_neighbourhood_dl(getCity);
            }
    function geodir_codeAddress(set_on_map) {
        var address = jQuery('#post_address').val();
        var zip = jQuery('#post_zip').val();
        var city = jQuery('#post_city').val();
        var region = jQuery('#post_region').val();
        var country = jQuery('#post_country').val();
        var country_selected = jQuery('#post_country').find('option:selected');
        var ISO2 = country_selected.data('country_code');
        if (!ISO2 && jQuery('#post_country').data('country_code')) {
            ISO2 = jQuery('#post_country').data('country_code');
        }
        if(!ISO2){
                    }
        if (ISO2 == '--') {
            ISO2 = '';
        }
        
        if (typeof zip == "undefined") {
            zip = '';
        }
        if (typeof city == "undefined") {
            city = 'New York';
        }
        if (typeof region == "undefined") {
            region = 'New York';
        }
        if (typeof country == "undefined") {
            country = 'United States';
        }
        var is_restrict = '';
                if (set_on_map && is_restrict) {
            if (zip != '' && address != '') {
                address = address + ',' + zip;
            }
        } else {
            if (typeof address === 'undefined')
                address = '';

            if( address == city || address == region || address == country || address == zip )
                address = '';
            
            if (ISO2 == 'GB') {
                address = address + ',' + city + ',' + country; // UK is funny with regions
            } else {
                address = address + ',' + city + ',' + region + ',' + country;
            }

            if(zip!=''){
                address = address + ',' + zip;
            }

            // incase there are any null values
            address =  address.replace(",null,", ",");
        }
                if (!window.gdMaps) { // No Google Map Loaded
            return;
        }

        if ( window.gdMaps == 'osm' ) {
            if (address != '') {
                if (zip != '') {
                    searchZip = "," + zip;
                    var nAddress = address.toLowerCase().lastIndexOf(searchZip.toLowerCase());
                    address = address.slice(0, nAddress) + address.slice(nAddress).replace(new RegExp(searchZip, 'i'), "");
                }
                geocodePositionOSM('', address, ISO2, true);
            }
        } else {
            geocoder.geocode({'address': address, 'country': ISO2},
                function (results, status) {
                    console.log(status);
                    jQuery("#post_map").goMap();
                    if (status == google.maps.GeocoderStatus.OK) {
                        baseMarker.setPosition(results[0].geometry.location);
                        jQuery.goMap.map.setCenter(results[0].geometry.location);
                        updateMarkerPosition(baseMarker.getPosition());
                        //if(set_on_map && is_restrict) {
                                                geocodePosition(baseMarker.getPosition(), {'address': address, 'country': ISO2});
                        //}
                    } else {
                        alert('Geocode was not successful for the following reason: ' + status);
                    }
                });
        }
    }
    function gdMaxMap() {
        jQuery("#post_map").goMap();

        jQuery('#post_map').toggleClass('map-fullscreen');
        jQuery('.map_category').toggleClass('map_category_fullscreen');
        jQuery('#post_trigger').toggleClass('map_category_fullscreen');
        jQuery('body').toggleClass('body_fullscreen');
        jQuery('#post_loading_div').toggleClass('loading_div_fullscreen');
        jQuery('#post_advmap_nofound').toggleClass('nofound_fullscreen');
        jQuery('#post_triggermap').toggleClass('triggermap_fullscreen');
        jQuery('.TopLeft').toggleClass('TopLeft_fullscreen');
        window.setTimeout(function () {
            if (window.gdMaps == 'google') {
                google.maps.event.trigger($.goMap, 'resize');
            } else if (window.gdMaps == 'osm') {
                jQuery.goMap.map.invalidateSize();
                jQuery.goMap.map._onResize();
            }                
        }, 100);
    }
    
    function geocodeResponseOSM(response, updateMap) {
        if (response.display_address) {
            var getAddress = response.display_address;
            var getZip = response.postcode;
            var getCity = response.city;
            var getState = response.state;
            var getCountry = response.country;
            getCountryISO = response.country_code;
            
            if (updateMap && response.lat && response.lon) {
                var newLatLng = new L.latLng(response.lat, response.lon);
                baseMarker.setLatLng(newLatLng);
                centerMap(newLatLng);
                updateMarkerPositionOSM(baseMarker.getLatLng());
            }
             
window.neighbourhood = '';
window.gdGeo = true;
if (window.gdMaps == 'google' && typeof responses != 'undefined' && responses && responses.length > 0) {    
    for (var j=0; j < responses[0].address_components.length; j++) {
        var component = responses[0].address_components[j];
        if (component.types[0] == 'neighbourhood' || component.types[0] == 'neighborhood') {
            window.neighbourhood = component.long_name;
        }
    }
} 
                        updateMarkerAddress(getAddress, getZip, getCity, getState, getCountry);
        } else {
            alert('Cannot determine address at this location.');
        }
    }
    
    jQuery(function ($) {
        $("#post_map").goMap({
            latitude: post_CITY_MAP_CENTER_LAT,
            longitude: post_CITY_MAP_CENTER_LNG,
            zoom: post_CITY_MAP_ZOOMING_FACT,
            maptype: 'ROADMAP', // Map type - HYBRID, ROADMAP, SATELLITE, TERRAIN
            streetViewControl: true,
            scrollwheel: false,        });

        if (window.gdMaps) {
            geocoder = window.gdMaps == 'google' ? new google.maps.Geocoder() : [];

            baseMarker = $.goMap.createMarker({
                latitude: post_CITY_MAP_CENTER_LAT,
                longitude: post_CITY_MAP_CENTER_LNG,
                id: 'baseMarker',
                icon: 'https://www.new-brennanhealingscience.org/directory/wp-content/plugins/geodirectory/geodirectory-functions/map-functions/icons/pin.png',
                draggable: true,
                addToMap: true, // For OSM
                w: parseFloat('20'),
                h: parseFloat('34'),
            });
        } else {
            jQuery('#post_advmap_nofound').hide();
            jQuery('#post_advmap_notloaded').show();
        }
        
        $("#post_set_address_button").click(function () {
            var set_on_map = true;
            geodir_codeAddress(set_on_map);
        });
        
        if (window.gdMaps == 'google') {
            // Add dragging event listeners.
            google.maps.event.addListener(baseMarker, 'dragstart', function () {
            //updateMarkerAddress('Dragging...');
            });
            google.maps.event.addListener(baseMarker, 'drag', function () {
                // updateMarkerStatus('Dragging...');
                updateMarkerPosition(baseMarker.getPosition());
            });
            google.maps.event.addListener(baseMarker, 'dragend', function () {
                // updateMarkerStatus('Drag ended');
                centerMap();
                                geocodePosition(baseMarker.getPosition());
                                updateMarkerPosition(baseMarker.getPosition());
            });
            google.maps.event.addListener($.goMap.map, 'dragend', function () {
                // updateMarkerStatus('Drag ended');
                centerMarker();
                                geocodePosition(baseMarker.getPosition());
                
                updateMarkerPosition(baseMarker.getPosition());
            });
            google.maps.event.addListener($.goMap.map, 'zoom_changed', function () {
                updateMapZoom($.goMap.map.zoom);
            });

            var maxMap = document.getElementById('post_triggermap');
            google.maps.event.addDomListener(maxMap, 'click', gdMaxMap);

                        // Limit the zoom level
            google.maps.event.addListener($.goMap.map, 'zoom_changed', function () {
                $("#post_map").goMap();
                if ($.goMap.map.getZoom() < minZoomLevel) $.goMap.map.setZoom(minZoomLevel);
            });
        } else if (window.gdMaps == 'osm') {
            // Add dragging event listeners.
            baseMarker.on('drag', function(e) {
                updateMarkerPositionOSM(baseMarker.getLatLng());
            });
            baseMarker.on('dragend', function(e) {
                centerMap();
                                geocodePositionOSM(baseMarker.getLatLng());
                                updateMarkerPositionOSM(baseMarker.getLatLng());
            });
            $.goMap.map.on('dragend', function(e) {
                                geocodePositionOSM(baseMarker.getLatLng());
                                centerMarker();
                updateMarkerPositionOSM(baseMarker.getLatLng());
            });
            $.goMap.map.on('zoom', function(e) {
                updateMapZoom($.goMap.map.getZoom());
            });

            L.DomEvent.addListener($('post_triggermap'), 'click', gdMaxMap);

                        // Limit the zoom level
            $.goMap.map.on('zoom', function(e) {
                if ($.goMap.map.getZoom() < minZoomLevel) {
                    $.goMap.map.setZoom(minZoomLevel);
                }
            });
        }
    });
    /* ]]> */