// assets/js/main.js
document.addEventListener('DOMContentLoaded', function() {
// Initialize AOS
AOS.init({
duration: 800,
easing: 'ease-in-out',
once: true,
mirror: false
});
// Header Scroll Effect
window.addEventListener('scroll', function() {
if (window.scrollY > 100) {
document.querySelector('.header').classList.add('scrolled');
document.querySelector('.back-to-top').classList.add('show');
} else {
document.querySelector('.header').classList.remove('scrolled');
document.querySelector('.back-to-top').classList.remove('show');
}
});
// Mobile Menu Toggle
const mobileToggle = document.querySelector('.mobile-toggle');
const navMenu = document.querySelector('.nav-menu');
if (mobileToggle) {
mobileToggle.addEventListener('click', function() {
navMenu.classList.toggle('active');
this.querySelector('i').classList.toggle('fa-bars');
this.querySelector('i').classList.toggle('fa-times');
});
}
// Mobile Dropdown Toggle
const hasDropdown = document.querySelectorAll('.has-dropdown');
hasDropdown.forEach(item => {
item.addEventListener('click', function(e) {
if (window.innerWidth < 992) {
e.preventDefault();
this.classList.toggle('dropdown-open');
}
});
});
// Hero Slider
const heroSlides = document.querySelectorAll('.hero-slide');
const heroDots = document.querySelectorAll('.hero-dot');
let currentSlide = 0;
let heroSlideInterval;
function showSlide(index) {
heroSlides.forEach(slide => slide.classList.remove('active'));
heroDots.forEach(dot => dot.classList.remove('active'));
if (heroSlides[index]) {
heroSlides[index].classList.add('active');
heroDots[index].classList.add('active');
currentSlide = index;
}
}
function nextSlide() {
if (heroSlides.length > 0) {
currentSlide = (currentSlide + 1) % heroSlides.length;
showSlide(currentSlide);
}
}
function startSlideInterval() {
if (heroSlides.length > 1) {
heroSlideInterval = setInterval(nextSlide, 5000);
}
}
if (heroDots.length > 0) {
heroDots.forEach((dot, index) => {
dot.addEventListener('click', () => {
clearInterval(heroSlideInterval);
showSlide(index);
startSlideInterval();
});
});
}
startSlideInterval();
// Product Tabs
const productTabs = document.querySelectorAll('.product-tab');
const productCards = document.querySelectorAll('.product-card');
if (productTabs.length > 0) {
productTabs.forEach(tab => {
tab.addEventListener('click', () => {
productTabs.forEach(t => t.classList.remove('active'));
tab.classList.add('active');
const category = tab.getAttribute('data-category');
productCards.forEach(card => {
if (category === 'all') {
card.style.display = 'block';
} else {
if (card.getAttribute('data-category') === category) {
card.style.display = 'block';
} else {
card.style.display = 'none';
}
}
});
});
});
}
// Testimonial Slider
const testimonialSlides = document.querySelectorAll('.testimonial-slide');
const testimonialPrev = document.querySelector('.testimonial-prev');
const testimonialNext = document.querySelector('.testimonial-next');
let currentTestimonial = 0;
function showTestimonial(index) {
if (testimonialSlides.length > 0) {
testimonialSlides.forEach(slide => slide.classList.remove('active'));
testimonialSlides[index].classList.add('active');
currentTestimonial = index;
}
}
if (testimonialPrev && testimonialNext) {
testimonialPrev.addEventListener('click', () => {
if (testimonialSlides.length > 0) {
currentTestimonial = (currentTestimonial - 1 + testimonialSlides.length) % testimonialSlides.length;
showTestimonial(currentTestimonial);
}
});
testimonialNext.addEventListener('click', () => {
if (testimonialSlides.length > 0) {
currentTestimonial = (currentTestimonial + 1) % testimonialSlides.length;
showTestimonial(currentTestimonial);
}
});
// Auto change testimonial every 8 seconds
if (testimonialSlides.length > 1) {
setInterval(() => {
currentTestimonial = (currentTestimonial + 1) % testimonialSlides.length;
showTestimonial(currentTestimonial);
}, 8000);
}
}
// FAQ Accordion
const faqItems = document.querySelectorAll('.faq-item');
faqItems.forEach(item => {
const question = item.querySelector('.faq-question');
question.addEventListener('click', () => {
if(item.classList.contains('active')) {
item.classList.remove('active');
} else {
faqItems.forEach(i => i.classList.remove('active'));
item.classList.add('active');
}
});
});
// Counter Animation
const counters = document.querySelectorAll('.counter-number');
function startCounting() {
counters.forEach(counter => {
const target = parseInt(counter.getAttribute('data-count'));
const duration = 2000; // 2 seconds
const increment = target / (duration / 20); // Update every 20ms
let current = 0;
const updateCounter = () => {
current += increment;
if (current < target) {
counter.textContent = Math.floor(current);
setTimeout(updateCounter, 20);
} else {
counter.textContent = target;
}
};
updateCounter();
});
}
// Start counting when the counters section is in view
const counterSection = document.querySelector('.counters');
if (counterSection) {
const observer = new IntersectionObserver((entries) => {
if (entries[0].isIntersecting) {
startCounting();
observer.disconnect();
}
});
observer.observe(counterSection);
}
// Product Quick View
const quickViewButtons = document.querySelectorAll('.product-quickview');
const productModal = document.getElementById('productModal');
const closeProductModal = document.getElementById('closeProductModal');
if (quickViewButtons.length > 0 && productModal && closeProductModal) {
quickViewButtons.forEach(button => {
button.addEventListener('click', function() {
const productId = this.getAttribute('data-product-id');
// AJAX request to get product details
fetch(`api/product_modal.php?id=${productId}`)
.then(response => response.json())
.then(data => {
if (data.success) {
const product = data.product;
// Update modal content with product details
const mainImage = document.querySelector('.product-modal-main-image');
const thumbs = document.querySelector('.product-modal-thumbs');
const details = document.querySelector('.product-modal-details');
// Set main image
mainImage.innerHTML = ``;
// Set thumbnails
let thumbsHTML = '';
if (product.gallery && product.gallery.length > 0) {
thumbsHTML = `
${product.short_description}