// ConstructPro SA - Main JavaScript
document.addEventListener('DOMContentLoaded', function() {
// Initialize Feather Icons
if (typeof feather !== 'undefined') {
feather.replace();
}
// Mobile Menu Toggle
const mobileMenuBtn = document.getElementById('mobile-menu-btn');
const mobileMenu = document.getElementById('mobile-menu');
if (mobileMenuBtn && mobileMenu) {
mobileMenuBtn.addEventListener('click', () => {
mobileMenu.classList.toggle('hidden');
const icon = mobileMenuBtn.querySelector('i');
if (mobileMenu.classList.contains('hidden')) {
icon.setAttribute('data-feather', 'menu');
} else {
icon.setAttribute('data-feather', 'x');
}
feather.replace();
});
}
// Sticky Navigation
const navbar = document.getElementById('navbar');
let lastScroll = 0;
window.addEventListener('scroll', () => {
const currentScroll = window.pageYOffset;
if (currentScroll > 100) {
navbar.classList.add('nav-scrolled');
} else {
navbar.classList.remove('nav-scrolled');
}
lastScroll = currentScroll;
});
// Smooth Scroll for Anchor Links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
// Close mobile menu if open
if (mobileMenu && !mobileMenu.classList.contains('hidden')) {
mobileMenu.classList.add('hidden');
}
}
});
});
// Counter Animation
const counters = document.querySelectorAll('.counter');
const speed = 200;
const animateCounter = (counter) => {
const target = +counter.getAttribute('data-target');
const count = +counter.innerText;
const inc = target / speed;
if (count < target) {
counter.innerText = Math.ceil(count + inc);
setTimeout(() => animateCounter(counter), 20);
} else {
counter.innerText = target + (target > 100 ? '+' : '');
}
};
// Intersection Observer for Counters
const counterObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
animateCounter(entry.target);
counterObserver.unobserve(entry.target);
}
});
}, { threshold: 0.5 });
counters.forEach(counter => counterObserver.observe(counter));
// Contact Form Handling
const contactForm = document.getElementById('contactForm');
if (contactForm) {
contactForm.addEventListener('submit', function(e) {
e.preventDefault();
// Add loading state
const submitBtn = this.querySelector('button[type="submit"]');
const originalText = submitBtn.innerHTML;
submitBtn.innerHTML = 'Sending...';
submitBtn.classList.add('form-loading');
feather.replace();
// Simulate form submission
setTimeout(() => {
submitBtn.innerHTML = 'Sent Successfully!';
submitBtn.classList.remove('bg-primary-600', 'hover:bg-primary-700');
submitBtn.classList.add('bg-green-600', 'hover:bg-green-700');
feather.replace();
// Reset form
this.reset();
// Reset button after 3 seconds
setTimeout(() => {
submitBtn.innerHTML = originalText;
submitBtn.classList.remove('bg-green-600', 'hover:bg-green-700', 'form-loading');
submitBtn.classList.add('bg-primary-600', 'hover:bg-primary-700');
feather.replace();
}, 3000);
}, 1500);
});
}
// Active Navigation Link
const sections = document.querySelectorAll('section[id]');
const navLinks = document.querySelectorAll('.nav-link');
window.addEventListener('scroll', () => {
let current = '';
sections.forEach(section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.clientHeight;
if (pageYOffset >= (sectionTop - 200)) {
current = section.getAttribute('id');
}
});
navLinks.forEach(link => {
link.classList.remove('text-primary-600');
if (link.getAttribute('href') === `#${current}`) {
link.classList.add('text-primary-600');
}
});
});
// Parallax Effect for Hero
const heroSection = document.querySelector('section.relative.h-screen');
if (heroSection) {
window.addEventListener('scroll', () => {
const scrolled = window.pageYOffset;
const parallax = heroSection.querySelector('img');
if (parallax) {
parallax.style.transform = `translateY(${scrolled * 0.5}px)`;
}
});
}
// Lazy Loading Images
const images = document.querySelectorAll('img[data-src]');
const imageObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
img.src = img.dataset.src;
img.removeAttribute('data-src');
imageObserver.unobserve(img);
}
});
});
images.forEach(img => imageObserver.observe(img));
// Current Year for Footer
const yearSpan = document.getElementById('current-year');
if (yearSpan) {
yearSpan.textContent = new Date().getFullYear();
}
// WhatsApp Float Button (if added later)
const whatsappBtn = document.querySelector('.whatsapp-float');
if (whatsappBtn) {
whatsappBtn.addEventListener('click', () => {
gtag && gtag('event', 'click', {
'event_category': 'engagement',
'event_label': 'WhatsApp Button'
});
});
}
});
// Utility Functions
const debounce = (func, wait) => {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
};
// Format Currency (ZAR)
const formatZAR = (amount) => {
return new Intl.NumberFormat('en-ZA', {
style: 'currency',
currency: 'ZAR',
minimumFractionDigits: 0,
maximumFractionDigits: 0
}).format(amount);
};
// Validate South African Phone Number
const validateSAPhone = (phone) => {
const regex = /^(?:\+27|0)[6-8][0-9]{8}$/;
return regex.test(phone.replace(/\s/g, ''));
};
// Expose utilities globally if needed
window.ConstructProUtils = {
formatZAR,
validateSAPhone,
debounce
};