File size: 8,739 Bytes
80e344e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
document.addEventListener('DOMContentLoaded', function() {
    const preloader = document.querySelector('.preloader');
    const content = document.getElementById('content');
    const body = document.body;
    
    // Immediately hide content
    if (content) content.style.display = 'none';
    
    // Start preloader animation
    if (preloader) {
        preloader.style.opacity = '1';
        
        // Handle page load completion with a fixed minimum display time
        const minDisplayTime = 500; // 0.5s minimum for all pages
        const handleLoadComplete = () => {
            const loadTime = performance.now();
            const remainingTime = Math.max(0, minDisplayTime - loadTime);
            
            setTimeout(() => {
                preloader.classList.add('hidden');
                
                setTimeout(() => {
                    preloader.style.display = 'none';
                    if (content) {
                        content.style.display = 'block';
                        content.classList.add('visible');
                    }
                    body.classList.add('loaded'); // Enable scrolling
                    initializePageFeatures();
                }, 500);
            }, remainingTime);
        };
        
        // Check if page is already loaded or handle load event
        if (document.readyState === 'complete') {
            handleLoadComplete();
        } else {
            window.addEventListener('load', handleLoadComplete);
        }
        
        // Fallback to show content after 5 seconds if load event fails
        setTimeout(() => {
            if (preloader.style.display !== 'none' && content) {
                console.warn('Preloader timeout reached, forcing content display');
                preloader.classList.add('hidden');
                setTimeout(() => {
                    preloader.style.display = 'none';
                    content.style.display = 'block';
                    content.classList.add('visible');
                    body.classList.add('loaded'); // Enable scrolling
                    initializePageFeatures();
                }, 500);
            }
        }, 5000);
    } else {
        // If no preloader, show content immediately and initialize features
        if (content) {
            content.style.display = 'block';
            content.classList.add('visible');
        }
        body.classList.add('loaded'); // Enable scrolling
        initializePageFeatures();
    }

    // Function to initialize all page features (AOS, counters, chatbot, etc.)
    function initializePageFeatures() {
        // Initialize AOS animations
        if (typeof AOS !== 'undefined') {
            AOS.init({
                duration: 1000,
                once: true
            });
        } else {
            console.warn('AOS library not loaded');
        }

        // Initialize Vanilla Tilt for card hover effects
        if (typeof VanillaTilt !== 'undefined') {
            VanillaTilt.init(document.querySelectorAll("[data-tilt]"), {
                max: 15,
                speed: 400,
                glare: true,
                "max-glare": 0.3
            });
        } else {
            console.warn('VanillaTilt library not loaded');
        }

        // Close navbar on link click (mobile)
        const navbarToggler = document.querySelector('.navbar-toggler');
        const navbarCollapse = document.querySelector('.navbar-collapse');
        if (navbarToggler && navbarCollapse) {
            document.addEventListener('click', function(event) {
                if (navbarCollapse.classList.contains('show') && !navbarCollapse.contains(event.target) && !navbarToggler.contains(event.target)) {
                    navbarToggler.click();
                }
            });
        }

        // Animated Counter
        const counters = document.querySelectorAll('.counter');
        counters.forEach(counter => {
            const updateCounter = () => {
                const target = +counter.getAttribute('data-target');
                const count = +counter.innerText.replace('%', '');
                const increment = target / 200;
                if (count < target) {
                    counter.innerText = Math.ceil(count + increment) + (counter.innerText.includes('%') ? '%' : '');
                    setTimeout(updateCounter, 20);
                } else {
                    counter.innerText = target + (counter.innerText.includes('%') ? '%' : '');
                }
            };
            const observer = new IntersectionObserver((entries) => {
                if (entries[0].isIntersecting) {
                    updateCounter();
                    observer.disconnect();
                }
            }, { threshold: 0.5 });
            observer.observe(counter);
        });

        // Chatbot Toggle
        const chatbotToggle = document.querySelector('.chatbot-toggle');
        const chatbotBox = document.querySelector('.chatbot-box');
        const chatbotClose = document.querySelector('.chatbot-close');
        if (chatbotToggle && chatbotBox && chatbotClose) {
            chatbotToggle.addEventListener('click', () => {
                chatbotBox.classList.toggle('active');
            });
            chatbotClose.addEventListener('click', () => {
                chatbotBox.classList.remove('active');
            });
        }
    }

    // Handle form submissions with AJAX
    const forms = document.querySelectorAll('form');
    forms.forEach(form => {
        form.addEventListener('submit', async (e) => {
            e.preventDefault();

            // Find the submit button within the form
            const submitButton = form.querySelector('button[type="submit"]');
            if (!submitButton) return;

            // Store the original button HTML and attributes
            const originalButtonHTML = submitButton.innerHTML;
            const originalDisabledState = submitButton.disabled;

            // Change button to loading state using Bootstrap spinner
            submitButton.disabled = true;
            submitButton.className = 'btn btn-primary disabled';
            submitButton.innerHTML = `

                <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>

                Sending...

            `;

            const formData = new FormData(form);
            const action = form.action || window.location.href;
            const method = form.method.toUpperCase();

            try {
                const response = await fetch(action, {
                    method: method,
                    body: formData,
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest'
                    }
                });

                const result = await response.json();

                // Update flash messages
                const flashMessages = document.getElementById('flashMessages');
                if (flashMessages) {
                    flashMessages.innerHTML = `

                        <div class="alert alert-${result.category} alert-dismissible fade show" role="alert">

                            ${result.message}

                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-hidden="true"></button>

                        </div>

                    `;
                }

                // Reset form if successful
                if (result.success) {
                    form.reset();
                }
            } catch (error) {
                console.error('Form submission error:', error);
                const flashMessages = document.getElementById('flashMessages');
                if (flashMessages) {
                    flashMessages.innerHTML = `

                        <div class="alert alert-danger alert-dismissible fade show" role="alert">

                            An error occurred. Please try again.

                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-hidden="true"></button>

                        </div>

                    `;
                }
            } finally {
                // Revert button to original state
                submitButton.disabled = originalDisabledState;
                submitButton.innerHTML = originalButtonHTML;
                submitButton.className = 'btn btn-primary';
            }
        });
    });

    // Debug Preloader
    console.log("Page loaded, checking preloader...");
});