(function() {
const apiKey = 'AIzaSyBJCj3yPfN7HONiYhcszhyu9MBUCZp_olg';
const countryRestriction = 'pk';
const defaultZoom = 15;
let countryArray = [];
let countryRestrictionValue = null;
if (countryRestriction && countryRestriction.trim() !== '') {
countryArray = countryRestriction.split(',').map(c => c.trim().toLowerCase()).filter(c => c.length === 2);
if (countryArray.length === 1) {
countryRestrictionValue = countryArray[0];
} else if (countryArray.length > 1) {
countryRestrictionValue = countryArray;
}
}
let map = null;
let marker = null;
let autocomplete = null;
let geocoder = null;
function loadGoogleMaps() {
if (typeof google !== 'undefined' && google.maps) {
initAddressAutocomplete();
return;
}
if (!apiKey) {
console.warn('Google Maps API key not configured');
return;
}
const script = document.createElement('script');
script.src = 'https://maps.googleapis.com/maps/api/js?key=' + apiKey + '&libraries=places&callback=initZipSchedulerMaps';
script.async = true;
script.defer = true;
document.head.appendChild(script);
window.initZipSchedulerMaps = function() {
initAddressAutocomplete();
};
}
function initAddressAutocomplete() {
const input = document.getElementById('jrb-booking-address');
if (!input || typeof google === 'undefined' || !google.maps) {
setTimeout(initAddressAutocomplete, 500);
return;
}
geocoder = new google.maps.Geocoder();
const autocompleteOptions = {
types: ['address']
};
if (countryRestrictionValue !== null) {
autocompleteOptions.componentRestrictions = { country: countryRestrictionValue };
}
autocomplete = new google.maps.places.Autocomplete(input, autocompleteOptions);
autocomplete.addListener('place_changed', function() {
const place = autocomplete.getPlace();
if (!place.geometry) return;
let city = '', zipcode = '';
place.address_components.forEach(function(component) {
if (component.types.includes('locality')) {
city = component.long_name;
}
if (component.types.includes('postal_code')) {
zipcode = component.long_name.toUpperCase().replace(/\s+/g, '');
}
});
const cityInput = document.getElementById('jrb-booking-city');
if (cityInput && city) {
cityInput.value = city;
}
const zipcodeInput = document.getElementById('jrb-postcode-field');
if (zipcodeInput && zipcode) {
zipcodeInput.value = zipcode;
jQuery(zipcodeInput).trigger('change');
}
window.storedLocation = {
location: place.geometry.location,
address: place.formatted_address || input.value,
zipcode: zipcode
};
});
}
function setupLocationButton() {
const locationButton = document.getElementById('jrb-show-location-map');
const addressInput = document.getElementById('jrb-booking-address');
if (!locationButton || !addressInput) {
setTimeout(setupLocationButton, 100);
return;
}
locationButton.addEventListener('click', function(e) {
e.preventDefault();
e.stopPropagation();
const address = addressInput.value.trim();
if (!address) {
alert('Please enter an address first');
return false;
}
if (!geocoder) {
if (typeof google !== 'undefined' && google.maps) {
geocoder = new google.maps.Geocoder();
} else {
alert('Map is not ready yet. Please wait a moment and try again.');
return false;
}
}
if (window.storedLocation && window.storedLocation.location) {
showMapForLocation(window.storedLocation.location, window.storedLocation.address);
return false;
}
const geocodeOptions = { address: address };
if (countryRestrictionValue !== null) {
geocodeOptions.componentRestrictions = { country: countryRestrictionValue };
}
geocoder.geocode(geocodeOptions, function(results, status) {
if (status === 'OK' && results[0]) {
const location = results[0].geometry.location;
const formattedAddress = results[0].formatted_address;
let zipcode = '';
if (results[0].address_components) {
results[0].address_components.forEach(function(component) {
if (component.types.includes('postal_code')) {
zipcode = component.long_name.toUpperCase().replace(/\s+/g, '');
}
});
}
const zipcodeInput = document.getElementById('jrb-postcode-field');
if (zipcodeInput && zipcode) {
zipcodeInput.value = zipcode;
jQuery(zipcodeInput).trigger('change');
}
window.storedLocation = {
location: location,
address: formattedAddress,
zipcode: zipcode
};
showMapForLocation(location, formattedAddress);
} else {
alert('Could not find location for this address. Please check the address and try again.');
}
});
return false;
});
locationButton.addEventListener('mouseenter', function() {
this.style.background = '#256600';
});
locationButton.addEventListener('mouseleave', function() {
this.style.background = '#2F7800';
});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', setupLocationButton);
} else {
setupLocationButton();
}
function showMapForLocation(location, address) {
const mapContainer = document.getElementById('jrb-address-map-container');
const mapElement = document.getElementById('jrb-booking-address-map');
if (!mapContainer || !mapElement) return;
mapContainer.style.display = 'block';
if (!map && typeof google !== 'undefined' && google.maps) {
map = new google.maps.Map(mapElement, {
zoom: defaultZoom,
center: location,
mapTypeControl: true,
streetViewControl: true,
fullscreenControl: true
});
marker = new google.maps.Marker({
map: map,
draggable: false
});
}
if (map && marker) {
map.setCenter(location);
marker.setPosition(location);
marker.setTitle(address);
const infoWindow = new google.maps.InfoWindow({
content: '
Pickup Location
' + address + '
'
});
marker.addListener('click', function() {
infoWindow.open(map, marker);
});
setTimeout(function() {
infoWindow.open(map, marker);
}, 300);
}
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', loadGoogleMaps);
} else {
loadGoogleMaps();
}
})();