jQuery(document).ready(function($) {
    'use strict';
    
    // Close announcement functionality
    $('.pc-announcements-274-close').on('click', function(e) {
        e.preventDefault();
        e.stopPropagation();
        
        var $announcement = $(this).closest('.pc-announcements-274-announcement');
        var announcementId = $announcement.data('announcement-id');
        
        // Add hiding class for animation
        $announcement.addClass('pc-announcements-274-hidden');
        
        // Store dismissal in localStorage for this session
        if (typeof(Storage) !== "undefined" && announcementId) {
            var dismissed = localStorage.getItem('pc_announcements_274_dismissed') || '[]';
            var dismissedArray = JSON.parse(dismissed);
            
            if (dismissedArray.indexOf(announcementId) === -1) {
                dismissedArray.push(announcementId);
                localStorage.setItem('pc_announcements_274_dismissed', JSON.stringify(dismissedArray));
            }
        }
        
        // Remove from DOM after animation
        setTimeout(function() {
            $announcement.attr('aria-hidden', 'true').hide();
            
            // Adjust body padding if needed
            adjustBodyPadding();
        }, 400);
    });
    
    // Adjust body padding to prevent content jump when announcement is hidden
    function adjustBodyPadding() {
        var $announcement = $('.pc-announcements-274-announcement');
        if ($announcement.length === 0) {
            $('body').css('padding-top', '');
            return;
        }
        
        if ($announcement.is(':visible')) {
            var announcementHeight = $announcement.outerHeight();
            var currentPadding = parseInt($('body').css('padding-top')) || 0;
            
            if (currentPadding < announcementHeight) {
                $('body').css('padding-top', announcementHeight + 'px');
            }
        }
    }
    
    // Initialize padding adjustment
    $(window).on('load', function() {
        adjustBodyPadding();
    });
    
    // Handle window resize
    $(window).on('resize', function() {
        adjustBodyPadding();
    });
    
    // Check for dismissed announcements on page load
    function checkDismissedAnnouncements() {
        if (typeof(Storage) !== "undefined") {
            var dismissed = localStorage.getItem('pc_announcements_274_dismissed') || '[]';
            var dismissedArray = JSON.parse(dismissed);
            
            $('.pc-announcements-274-announcement').each(function() {
                var announcementId = $(this).data('announcement-id');
                if (announcementId && dismissedArray.indexOf(announcementId) !== -1) {
                    $(this).attr('aria-hidden', 'true').hide();
                }
            });
        }
    }
    
    checkDismissedAnnouncements();
    
    // Re-check padding after checking dismissed announcements
    setTimeout(function() {
        adjustBodyPadding();
    }, 100);
    
    // Keyboard navigation
    $('.pc-announcements-274-close').on('keydown', function(e) {
        if (e.key === 'Enter' || e.key === ' ') {
            e.preventDefault();
            $(this).click();
        }
    });
    
    // Escape key to close announcement
    $(document).on('keydown', function(e) {
        if (e.key === 'Escape') {
            var $visibleAnnouncement = $('.pc-announcements-274-announcement:visible');
            if ($visibleAnnouncement.length > 0) {
                $visibleAnnouncement.find('.pc-announcements-274-close').first().focus();
            }
        }
    });
    
    // Auto-hide functionality (optional - could be enabled with a data attribute)
    function initAutoHide() {
        $('.pc-announcements-274-announcement[data-auto-hide]').each(function() {
            var $announcement = $(this);
            var autoHideTime = parseInt($announcement.data('auto-hide')) * 1000;
            
            if (autoHideTime > 0) {
                setTimeout(function() {
                    if ($announcement.is(':visible')) {
                        $announcement.find('.pc-announcements-274-close').click();
                    }
                }, autoHideTime);
            }
        });
    }
    
    initAutoHide();
    
    // Add animation classes on initial load
    $('.pc-announcements-274-announcement:visible').addClass('pc-announcements-274-show');
    
    // Handle dynamic content loading (if announcements are loaded via AJAX)
    function reinitializeAnnouncements() {
        adjustBodyPadding();
        checkDismissedAnnouncements();
        initAutoHide();
        
        $('.pc-announcements-274-announcement:visible').addClass('pc-announcements-274-show');
    }
    
    // Expose reinitialize function for global use
    window.pcAnnouncements274Reinitialize = reinitializeAnnouncements;
    
    // Smooth scroll to top when announcement appears (optional)
    function smoothScrollToTop() {
        if ($('.pc-announcements-274-announcement:visible').length > 0) {
            $('html, body').animate({
                scrollTop: 0
            }, 300);
        }
    }
    
    // Only scroll to top on initial page load if announcement is present
    if (performance.navigation.type === 0) { // First page load
        setTimeout(function() {
            if ($('.pc-announcements-274-announcement:visible').length > 0) {
                var $announcement = $('.pc-announcements-274-announcement:visible');
                var announcementId = $announcement.data('announcement-id');
                
                // Don't scroll if it was just dismissed
                if (typeof(Storage) !== "undefined" && announcementId) {
                    var dismissed = localStorage.getItem('pc_announcements_274_dismissed') || '[]';
                    var dismissedArray = JSON.parse(dismissed);
                    
                    if (dismissedArray.indexOf(announcementId) === -1) {
                        smoothScrollToTop();
                    }
                } else {
                    smoothScrollToTop();
                }
            }
        }, 100);
    }
    
    // Handle announcement stacking if multiple are shown
    function handleStacking() {
        var $announcements = $('.pc-announcements-274-announcement:visible');
        var offset = 0;
        
        $announcements.each(function(index) {
            $(this).css('top', offset + 'px');
            offset += $(this).outerHeight();
        });
    }
    
    handleStacking();
    
    // Re-handle stacking on window resize
    $(window).on('resize', function() {
        handleStacking();
    });
    
    // Accessibility: Focus management
    function manageFocus() {
        $('.pc-announcements-274-announcement').attr('role', 'banner');
        $('.pc-announcements-274-close').attr('tabindex', '0');
    }
    
    manageFocus();
    
    // Performance: Debounce resize events
    function debounce(func, wait) {
        var timeout;
        return function executedFunction() {
            var context = this;
            var args = arguments;
            var later = function() {
                timeout = null;
                func.apply(context, args);
            };
            clearTimeout(timeout);
            timeout = setTimeout(later, wait);
        };
    }
    
    var debouncedResize = debounce(function() {
        adjustBodyPadding();
        handleStacking();
    }, 250);
    
    $(window).on('resize', debouncedResize);
    
    // Log for debugging (remove in production)
    if (window.console && window.console.log && false) { // Set to true for debugging
        console.log('PC Announcements 274: Initialized');
    }
});