/**
 * Owl Fly Animation Module
 * Handles the interactive owl button that flies away on click
 */

(function() {
    'use strict';
    
    function initOwlFlyButton() {
        const owl = document.getElementById('owl-fly-button');
        if (!owl) return;
        
        // Reset owl animation when tab becomes visible again
        const observer = new MutationObserver(function(mutations) {
            mutations.forEach(function(mutation) {
                if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
                    const tabPane = owl.closest('.tab-pane');
                    if (tabPane && tabPane.classList.contains('active')) {
                        owl.classList.remove('fly-away');
                        owl.classList.add('pulse-animation');
                        owl.style.opacity = '1';
                    }
                }
            });
        });
        
        const tabPane = owl.closest('.tab-pane');
        if (tabPane) {
            observer.observe(tabPane, { attributes: true });
        }
        
        owl.addEventListener('click', function() {
            owl.classList.remove('pulse-animation');
            owl.classList.add('fly-away');
            
            setTimeout(function() {
                const nextTarget = owl.getAttribute('data-next-target');
                const additionalButton = owl.getAttribute('data-additional-button');
                
                if (nextTarget) {
                    const targetTab = document.querySelector(nextTarget);
                    if (targetTab) targetTab.click();
                }
                if (additionalButton) {
                    const addBtn = document.querySelector(additionalButton);
                    if (addBtn) addBtn.click();
                }
            }, 500);
        });
    }
    
    // Initialize when DOM is ready
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initOwlFlyButton);
    } else {
        initOwlFlyButton();
    }
})();
