/**
 * Safari GIF Fix
 * Fixes blank GIFs when using Safari's back button (bfcache issue)
 * 
 * Safari's back/forward cache stores pages in memory, but GIF animations
 * don't restart properly when the page is restored. This script detects
 * when a page is loaded from bfcache and forces GIFs to reload.
 */
(function() {
    'use strict';
    
    function reloadGifs() {
        // Find all GIF images on the page
        const images = document.querySelectorAll('img[src$=".gif"], img[src*=".gif?"]');
        
        images.forEach(function(img) {
            // Force reload by temporarily changing and restoring the src
            const originalSrc = img.src;
            
            // Only reload if the image has a source
            if (originalSrc) {
                // Clear the src first to force a reload
                img.src = '';
                
                // Use a small delay to ensure the browser registers the change
                setTimeout(function() {
                    // Add a cache-busting parameter to force reload
                    // But only if we're restoring from cache, not on initial load
                    img.src = originalSrc;
                }, 10);
            }
        });
    }
    
    // Listen for pageshow event
    // The 'persisted' property indicates if the page was loaded from bfcache
    window.addEventListener('pageshow', function(event) {
        if (event.persisted) {
            // Page was restored from bfcache (back/forward button was used)
            reloadGifs();
        }
    });
    
    // Also handle pagehide to ensure Safari properly caches the page
    window.addEventListener('pagehide', function(event) {
        // This helps ensure the page can be cached properly
        // No action needed, but the listener helps Safari's caching
    });
    
})();
