/**
 * Havenlytics Preloader System v2.2.0
 * Production Ready - No Debug Logs
 */

(function($, window, document) {
    'use strict';
    
    if (window.hvnlyPreloaderInitialized) {
        return;
    }
    
    const HvnlyPreloaderManager = {
        
        config: {
            enabled: true,
            minDisplay: 300,
            animation: 'shimmer',
            components: {
                filter_sidebar: false,
                top_search: false,
                property_grid: false,
                property_list: false,
                view_controls: false,
                load_more: false,
                pagination: false,
                related_properties: false
            }
        },
        
        init: function() {
            if (typeof window.hvnly_preloader_config !== 'undefined') {
                if (window.hvnly_preloader_config.min_display !== undefined) {
                    this.config.minDisplay = parseInt(window.hvnly_preloader_config.min_display) || 300;
                }
                if (window.hvnly_preloader_config.enabled !== undefined) {
                    this.config.enabled = window.hvnly_preloader_config.enabled;
                }
                if (window.hvnly_preloader_config.animation !== undefined) {
                    this.config.animation = window.hvnly_preloader_config.animation;
                }
                if (window.hvnly_preloader_config.components !== undefined) {
                    this.config.components = {
                        filter_sidebar: window.hvnly_preloader_config.components.filter_sidebar || false,
                        top_search: window.hvnly_preloader_config.components.top_search || false,
                        property_grid: window.hvnly_preloader_config.components.property_grid || false,
                        property_list: window.hvnly_preloader_config.components.property_list || false,
                        view_controls: window.hvnly_preloader_config.components.view_controls || false,
                        load_more: window.hvnly_preloader_config.components.load_more || false,
                        pagination: window.hvnly_preloader_config.components.pagination || false,
                        related_properties: window.hvnly_preloader_config.components.related_properties || false,
                    };
                }
            }
            
            if (!this.config.enabled) {
                this.showOriginalContentImmediately();
                return;
            }
            
            if (this.config.components.filter_sidebar === true) {
                this.initFilterSidebar();
            } else {
                const sidebar = document.getElementById('hvnly-filter-sidebar');
                const preloader = document.getElementById('hvnly-filter-sidebar-preloader');
                if (preloader) preloader.style.display = 'none';
                if (sidebar) {
                    sidebar.classList.remove('hvnly-original-content');
                    sidebar.style.display = 'block';
                }
            }
            
            this.initPropertyGrid();
            
            if (this.config.components.view_controls === true) {
                this.initViewControls();
            } else {
                const preloader = document.getElementById('hvnly-view-controls-preloader');
                const viewControls = document.querySelector('.hvnly-property--view--controls');
                if (preloader) preloader.style.display = 'none';
                if (viewControls) {
                    viewControls.classList.remove('hvnly-original-content');
                    viewControls.style.display = 'flex';
                }
            }
            
            const isAjaxLoadMoreEnabled = this.getAjaxLoadMoreStatus();
            
            if (this.config.components.load_more === true && isAjaxLoadMoreEnabled) {
                this.initLoadMore();
            } else {
                const preloader = document.getElementById('hvnly-load-more-preloader');
                const loadMore = document.getElementById('hvnly-load-more-container');
                if (preloader) preloader.style.display = 'none';
                if (loadMore) {
                    loadMore.classList.remove('hvnly-original-content');
                    loadMore.style.display = '';
                }
            }
            
            if (this.config.components.pagination === true && !isAjaxLoadMoreEnabled) {
                this.initPagination();
            } else {
                const preloader = document.getElementById('hvnly-pagination-preloader');
                const pagination = document.getElementById('hvnly-property-pagination');
                if (preloader) preloader.style.display = 'none';
                if (pagination) {
                    pagination.classList.remove('hvnly-original-content');
                    pagination.style.display = '';
                }
            }
            
            if (this.config.components.top_search === true) {
                this.initTopSearch();
            } else {
                const preloader = document.getElementById('hvnly-top-search-preloader');
                const searchForm = document.querySelector('.hvnly-property-search-form');
                if (preloader) preloader.style.display = 'none';
                if (searchForm) searchForm.style.display = 'block';
            }
            
            if (this.config.components.related_properties === true) {
                this.initRelatedProperties();
            }
            
            window.hvnlyPreloaderInitialized = true;
        },
        
        getAjaxLoadMoreStatus: function() {
            if (typeof window.hvnly_ajax_load_more_enabled !== 'undefined') {
                return window.hvnly_ajax_load_more_enabled === true;
            }
            return true;
        },
        
        initViewControls: function() {
            const preloader = document.getElementById('hvnly-view-controls-preloader');
            const viewControls = document.querySelector('.hvnly-property--view--controls');
            if (!preloader || !viewControls) return;
            
            preloader.style.display = 'block';
            viewControls.style.display = 'none';
            viewControls.classList.add('hvnly-original-content');
            
            setTimeout(() => {
                preloader.style.display = 'none';
                viewControls.classList.remove('hvnly-original-content');
                viewControls.style.display = 'flex';
                viewControls.classList.add('hvnly-loaded');
            }, this.config.minDisplay);
        },
        
        initLoadMore: function() {
            const preloader = document.getElementById('hvnly-load-more-preloader');
            let loadMore = document.getElementById('hvnly-load-more-container');
            
            if (!preloader) return;
            if (!loadMore) {
                loadMore = document.getElementById('hvnly-load-more');
                if (!loadMore) return;
            }
            
            preloader.style.display = 'block';
            loadMore.style.display = 'none';
            loadMore.classList.add('hvnly-original-content');
            
            setTimeout(() => {
                preloader.style.display = 'none';
                loadMore.classList.remove('hvnly-original-content');
                loadMore.style.display = '';
                loadMore.classList.add('hvnly-loaded');
            }, this.config.minDisplay);
        },
        
        initPagination: function() {
            const preloader = document.getElementById('hvnly-pagination-preloader');
            const pagination = document.getElementById('hvnly-property-pagination');
            
            if (!preloader || !pagination) return;
            
            preloader.style.display = 'block';
            pagination.style.display = 'none';
            pagination.classList.add('hvnly-original-content');
            
            setTimeout(() => {
                preloader.style.display = 'none';
                pagination.classList.remove('hvnly-original-content');
                pagination.style.display = '';
                pagination.classList.add('hvnly-loaded');
            }, this.config.minDisplay);
        },
        
        showGridImmediately: function() {
            const grid = document.getElementById('hvnly-property-grid');
            const wrapper = document.getElementById('hvnly-property-grid-preloader-wrapper');
            const loadMore = document.getElementById('hvnly-load-more-container');
            const pagination = document.getElementById('hvnly-property-pagination');
            const viewControls = document.querySelector('.hvnly-property--view--controls');
            const viewControlsPreloader = document.getElementById('hvnly-view-controls-preloader');
            const loadMorePreloaderEl = document.getElementById('hvnly-load-more-preloader');
            const paginationPreloaderEl = document.getElementById('hvnly-pagination-preloader');
            
            if (wrapper) wrapper.style.display = 'none';
            if (viewControlsPreloader) viewControlsPreloader.style.display = 'none';
            if (loadMorePreloaderEl) loadMorePreloaderEl.style.display = 'none';
            if (paginationPreloaderEl) paginationPreloaderEl.style.display = 'none';
            
            if (grid) {
                grid.classList.remove('hvnly-original-content');
                const isListView = grid.classList.contains('hvnly-list-view') || grid.classList.contains('list-view');
                grid.style.display = isListView ? 'flex' : 'grid';
                if (isListView) {
                    grid.style.flexDirection = 'column';
                    grid.style.gap = '20px';
                }
            }
            
            if (loadMore) {
                loadMore.classList.remove('hvnly-original-content');
                loadMore.style.display = '';
            }
            
            if (pagination) {
                pagination.classList.remove('hvnly-original-content');
                pagination.style.display = '';
            }
            
            if (viewControls) {
                viewControls.classList.remove('hvnly-original-content');
                viewControls.style.display = 'flex';
            }
        },
        
        showOriginalContentImmediately: function() {
            const sidebar = document.getElementById('hvnly-filter-sidebar');
            const sidebarPreloader = document.getElementById('hvnly-filter-sidebar-preloader');
            const topSearchPreloader = document.getElementById('hvnly-top-search-preloader');
            const searchForm = document.querySelector('.hvnly-property-search-form');
            const viewControlsPreloaderEl = document.getElementById('hvnly-view-controls-preloader');
            const viewControlsEl = document.querySelector('.hvnly-property--view--controls');
            const loadMorePreloaderEl = document.getElementById('hvnly-load-more-preloader');
            const paginationPreloaderEl = document.getElementById('hvnly-pagination-preloader');
            const paginationEl = document.getElementById('hvnly-property-pagination');
            
            if (sidebarPreloader) sidebarPreloader.style.display = 'none';
            if (topSearchPreloader) topSearchPreloader.style.display = 'none';
            if (viewControlsPreloaderEl) viewControlsPreloaderEl.style.display = 'none';
            if (loadMorePreloaderEl) loadMorePreloaderEl.style.display = 'none';
            if (paginationPreloaderEl) paginationPreloaderEl.style.display = 'none';
            
            if (sidebar) {
                sidebar.classList.remove('hvnly-original-content');
                sidebar.style.display = 'block';
            }
            if (searchForm) searchForm.style.display = 'block';
            if (viewControlsEl) {
                viewControlsEl.classList.remove('hvnly-original-content');
                viewControlsEl.style.display = 'flex';
            }
            if (paginationEl) {
                paginationEl.classList.remove('hvnly-original-content');
                paginationEl.style.display = '';
            }
            
            this.showGridImmediately();
            
            setTimeout(() => {
                if (window.havenlyticsFrontend && typeof window.havenlyticsFrontend.initPropertyGalleryCarousels === 'function') {
                    window.havenlyticsFrontend.initPropertyGalleryCarousels();
                }
            }, 100);
        },
        
        initFilterSidebar: function() {
            const preloader = document.getElementById('hvnly-filter-sidebar-preloader');
            const sidebar = document.getElementById('hvnly-filter-sidebar');
            if (!preloader || !sidebar) return;
            
            preloader.style.display = 'block';
            sidebar.style.display = 'none';
            sidebar.classList.add('hvnly-original-content');
            
            setTimeout(() => {
                preloader.style.display = 'none';
                sidebar.classList.remove('hvnly-original-content');
                sidebar.style.display = 'block';
                sidebar.classList.add('hvnly-loaded');
            }, this.config.minDisplay);
        },
        
        initPropertyGrid: function() {
            const preloader = document.getElementById('hvnly-property-grid-preloader');
            const grid = document.getElementById('hvnly-property-grid');
            const wrapper = document.getElementById('hvnly-property-grid-preloader-wrapper');
            const loadMore = document.getElementById('hvnly-load-more-container');
            const pagination = document.getElementById('hvnly-property-pagination');
            
            if (!preloader || !grid) return;
            
            const isListView = grid.classList.contains('hvnly-list-view') || grid.classList.contains('list-view');
            const componentToCheck = isListView ? 'property_list' : 'property_grid';
            const componentEnabled = this.config.components[componentToCheck];
            
            if (!componentEnabled) {
                if (wrapper) wrapper.style.display = 'none';
                if (isListView) {
                    grid.style.display = 'flex';
                    grid.style.flexDirection = 'column';
                    grid.style.gap = '20px';
                } else {
                    grid.style.display = 'grid';
                }
                grid.classList.remove('hvnly-original-content');
                grid.classList.add('hvnly-loaded');
                if (loadMore) {
                    loadMore.classList.remove('hvnly-original-content');
                    loadMore.classList.add('hvnly-loaded');
                    loadMore.style.display = '';
                }
                if (pagination) {
                    pagination.classList.remove('hvnly-original-content');
                    pagination.classList.add('hvnly-loaded');
                    pagination.style.display = '';
                }
                return;
            }
            
            grid.style.display = 'none';
            grid.classList.add('hvnly-original-content');
            
            if (wrapper) wrapper.style.display = 'block';
            
            if (isListView) {
                preloader.classList.add('hvnly-list-view', 'list-view');
                preloader.classList.remove('hvnly-grid-view', 'grid-view');
            } else {
                preloader.classList.add('hvnly-grid-view', 'grid-view');
                preloader.classList.remove('hvnly-list-view', 'list-view');
            }
            
            setTimeout(() => {
                if (wrapper) wrapper.style.display = 'none';
                if (isListView) {
                    grid.style.display = 'flex';
                    grid.style.flexDirection = 'column';
                    grid.style.gap = '20px';
                } else {
                    grid.style.display = 'grid';
                }
                grid.classList.remove('hvnly-original-content');
                grid.classList.add('hvnly-loaded');
                if (loadMore) {
                    loadMore.classList.remove('hvnly-original-content');
                    loadMore.classList.add('hvnly-loaded');
                    loadMore.style.display = '';
                }
                if (pagination) {
                    pagination.classList.remove('hvnly-original-content');
                    pagination.classList.add('hvnly-loaded');
                    pagination.style.display = '';
                }
                setTimeout(() => {
                    if (window.havenlyticsFrontend && typeof window.havenlyticsFrontend.initPropertyGalleryCarousels === 'function') {
                        window.havenlyticsFrontend.initPropertyGalleryCarousels();
                    }
                    $(document).trigger('hvnly-preloader-complete');
                }, 100);
            }, this.config.minDisplay);
        },
        
        initTopSearch: function() {
            const preloader = document.getElementById('hvnly-top-search-preloader');
            const searchForm = document.querySelector('.hvnly-property-search-form');
            if (!preloader || !searchForm) return;
            
            preloader.style.display = 'block';
            searchForm.style.display = 'none';
            searchForm.classList.add('hvnly-original-content');
            
            setTimeout(() => {
                preloader.style.display = 'none';
                searchForm.classList.remove('hvnly-original-content');
                searchForm.style.display = 'block';
            }, this.config.minDisplay);
        },
        
        initRelatedProperties: function() {
            const preloader = document.getElementById('hvnly-related-properties-preloader');
            const relatedContainer = document.querySelector('.hvnly-property-single__similar-properties');
            if (!preloader || !relatedContainer) return;
            
            preloader.style.display = 'block';
            relatedContainer.style.display = 'none';
            
            setTimeout(() => {
                preloader.style.display = 'none';
                relatedContainer.style.display = 'block';
            }, this.config.minDisplay);
        },
        
        forceShowContent: function() {
            const sidebarPreloader = document.getElementById('hvnly-filter-sidebar-preloader');
            const sidebar = document.getElementById('hvnly-filter-sidebar');
            const topSearchPreloader = document.getElementById('hvnly-top-search-preloader');
            const searchForm = document.querySelector('.hvnly-property-search-form');
            const paginationPreloaderEl = document.getElementById('hvnly-pagination-preloader');
            const paginationEl = document.getElementById('hvnly-property-pagination');
            
            if (sidebarPreloader && sidebar) {
                sidebarPreloader.style.display = 'none';
                sidebar.classList.remove('hvnly-original-content');
                sidebar.style.display = 'block';
                sidebar.classList.add('hvnly-loaded');
            }
            
            if (topSearchPreloader && searchForm) {
                topSearchPreloader.style.display = 'none';
                searchForm.classList.remove('hvnly-original-content');
                searchForm.style.display = 'block';
            }
            
            if (paginationPreloaderEl && paginationEl) {
                paginationPreloaderEl.style.display = 'none';
                paginationEl.classList.remove('hvnly-original-content');
                paginationEl.style.display = '';
                paginationEl.classList.add('hvnly-loaded');
            }
            
            const gridPreloader = document.getElementById('hvnly-property-grid-preloader');
            const grid = document.getElementById('hvnly-property-grid');
            const wrapper = document.getElementById('hvnly-property-grid-preloader-wrapper');
            const loadMore = document.getElementById('hvnly-load-more-container');
            
            if (gridPreloader && grid) {
                if (wrapper) wrapper.style.display = 'none';
                gridPreloader.style.display = 'none';
                const isListView = grid.classList.contains('hvnly-list-view') || grid.classList.contains('list-view');
                if (isListView) {
                    grid.style.display = 'flex';
                    grid.style.flexDirection = 'column';
                    grid.style.gap = '20px';
                } else {
                    grid.style.display = 'grid';
                }
                grid.classList.remove('hvnly-original-content');
                grid.classList.add('hvnly-loaded');
            }
            
            if (loadMore) {
                loadMore.classList.remove('hvnly-original-content');
                loadMore.classList.add('hvnly-loaded');
                loadMore.style.display = '';
            }
            
            const viewControlsPreloaderEl = document.getElementById('hvnly-view-controls-preloader');
            const viewControlsEl = document.querySelector('.hvnly-property--view--controls');
            const loadMorePreloaderEl = document.getElementById('hvnly-load-more-preloader');
            
            if (viewControlsPreloaderEl && viewControlsEl) {
                viewControlsPreloaderEl.style.display = 'none';
                viewControlsEl.classList.remove('hvnly-original-content');
                viewControlsEl.style.display = 'flex';
                viewControlsEl.classList.add('hvnly-loaded');
            }
            if (loadMorePreloaderEl && loadMore) {
                loadMorePreloaderEl.style.display = 'none';
            }
        },
        
        reset: function() {
            this.activePreloaders = {};
            $(document).trigger('hvnly-preloader-reset');
        }
    };
    
    $(document).ready(function() {
        HvnlyPreloaderManager.init();
        window.HvnlyPreloader = HvnlyPreloaderManager;
    });
    
})(jQuery, window, document);