// Function to dynamically load a script tag

function loadScriptConditionally() {

    // Get the current URL path
    var path = window.location.pathname;

    // Check if the current path matches the target path
    if (path ===  '/schedule-an-appointment' || path ===  '/schedule-an-appointment') {

        // Create the script element
        var script = document.createElement('script');

         // Add inline script content
        script.setAttribute("src", "https://assets.calendly.com/assets/external/widget.js");

        // Append the script tag to the page
        document.getElementById("inner").appendChild(script);
    }

}
document.addEventListener('DOMContentLoaded', loadScriptConditionally);

// Fix for Calendly widget resizing
window.addEventListener('message', function(e) {
    if (e.origin !== 'https://calendly.com' || !e.data?.event) return;
    const iframe = document.querySelector('.calendly-inline-widget iframe');
    const widget = document.querySelector('.calendly-inline-widget');
    if (e.data.event === 'calendly.page_height') {
        const newHeight = e.data?.payload?.height;
        if (iframe && newHeight) {
            iframe.style.height = newHeight;
            iframe.style.setProperty('height', newHeight, 'important');
        } // Stop Calendly's default resize to prevent page jump
        e.stopImmediatePropagation();
        e.stopPropagation();
        e.preventDefault();
        return;
    }
    if (e.data.event === 'calendly.date_and_time_selected') {
        if (widget) {
            const widgetTop = widget.getBoundingClientRect().top + window.scrollY;
            const scrollOffset = widget.offsetWidth >= 750 ? 350 : 220;

            window.scrollTo({ top: widgetTop + scrollOffset, behavior: 'smooth' });
        }
    }
    if (e.data.event === 'calendly.event_scheduled') {
        if (widget) {
            const widgetTop = widget.getBoundingClientRect().top + window.scrollY;
            const scrollOffset = widget.offsetWidth >= 750 ? 100 : 130;

            window.scrollTo({ top: widgetTop - scrollOffset, behavior: 'smooth' });
        }
    }
}, true);