document.addEventListener('DOMContentLoaded', function () {
// --- Start of projects.js specific logic ---
let projectsData = [];
let currentProject = null;
let currentImageIndex = 0;
// --- Core Functions for fetching and rendering project cards ---
async function loadProjects() {
try {
const response = await fetch("data/projects.json");
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
projectsData = await response.json();
renderProjects();
} catch (error) {
console.error("Error loading projects:", error);
const grid = document.getElementById("projectsGrid");
if(grid) grid.innerHTML = `
Failed to load projects.
`;
}
}
function renderProjects() {
const grid = document.getElementById("projectsGrid");
if (!grid) return;
grid.innerHTML = projectsData.map((project, index) => `
${project.category}${project.completionDate}
${project.name}
${project.description}
${project.size}${project.location}
`).join("");
if (window.initDynamicAnimations) window.initDynamicAnimations();
}
// --- MODAL Functionality (Restored from original) ---
function openProjectDetails(projectId) {
const project = projectsData.find((p) => p.id === projectId);
if (!project) return;
currentProject = project;
currentImageIndex = 0;
document.getElementById("modalTitle").textContent = project.name;
document.getElementById("modalCategory").textContent = project.category;
document.getElementById("modalDescription").textContent = project.description;
document.getElementById("modalClient").textContent = project.client;
document.getElementById("modalTypeOfWork").textContent = project.typeOfWork;
document.getElementById("modalSize").textContent = project.size;
document.getElementById("modalCompletionDate").textContent = project.completionDate;
document.getElementById("modalLocation").textContent = project.location;
document.getElementById("modalHighlights").innerHTML = project.highlights.map(h => `${h}`).join("");
setupImageCarousel(project.images); // RESTORED CALL
const modal = document.getElementById("projectModal");
const modalContent = document.getElementById("modalContent");
modal.classList.remove("hidden");
document.body.style.overflow = "hidden";
setTimeout(() => modalContent.classList.remove("scale-95", "opacity-0"), 10);
}
function closeModal() {
const modal = document.getElementById("projectModal");
const modalContent = document.getElementById("modalContent");
modalContent.classList.add("scale-95", "opacity-0");
setTimeout(() => {
modal.classList.add("hidden");
document.body.style.overflow = "auto";
}, 500);
}
// --- MODAL CAROUSEL Functionality (Restored from original) ---
function setupImageCarousel(images) {
const mainImage = document.getElementById("modalMainImage");
const imageDots = document.getElementById("imageDots");
const imageCounter = document.getElementById("imageCounter");
const totalImages = document.getElementById("totalImages");
mainImage.src = images[0];
mainImage.alt = currentProject.name;
totalImages.textContent = images.length;
imageDots.innerHTML = images.map((_, index) => ``).join("");
updateImageCarousel(); // Initial call to set counter
}
function updateImageCarousel() {
const mainImage = document.getElementById("modalMainImage");
const imageDots = document.getElementById("imageDots");
const imageCounter = document.getElementById("imageCounter");
if (!mainImage || !currentProject) return;
mainImage.style.opacity = "0";
setTimeout(() => {
mainImage.src = currentProject.images[currentImageIndex];
mainImage.style.opacity = "1";
}, 250);
imageCounter.textContent = currentImageIndex + 1;
const dots = imageDots.querySelectorAll("button");
dots.forEach((dot, index) => {
dot.classList.toggle("bg-kh3-red", index === currentImageIndex);
dot.classList.toggle("bg-white/30", index !== currentImageIndex);
});
}
// --- IMAGE ENLARGEMENT Functionality (Restored from original) ---
function enlargeImage() {
if (!currentProject) return;
const enlargedImage = document.getElementById("enlargedImage");
document.getElementById("enlargedImageCounter").textContent = currentImageIndex + 1;
document.getElementById("enlargedTotalImages").textContent = currentProject.images.length;
enlargedImage.src = currentProject.images[currentImageIndex];
document.getElementById("imageEnlargeModal").classList.remove("hidden");
}
function closeEnlargeModal() {
document.getElementById("imageEnlargeModal").classList.add("hidden");
}
// --- Global Accessibility for onclick and keyboard events (Restored) ---
window.openProjectDetails = openProjectDetails;
window.closeModal = closeModal;
window.enlargeImage = enlargeImage;
window.goToImage = (index) => { currentImageIndex = index; updateImageCarousel(); };
window.nextImage = () => { currentImageIndex = (currentImageIndex + 1) % currentProject.images.length; updateImageCarousel(); };
window.prevImage = () => { currentImageIndex = (currentImageIndex === 0) ? currentProject.images.length - 1 : currentImageIndex - 1; updateImageCarousel(); };
// Event listeners for modal controls
document.getElementById("closeModal")?.addEventListener("click", closeModal);
document.getElementById("projectModal")?.addEventListener("click", (e) => { if (e.target.id === "projectModal") closeModal(); });
document.getElementById("prevImage")?.addEventListener("click", window.prevImage);
document.getElementById("nextImage")?.addEventListener("click", window.nextImage);
document.getElementById("closeEnlargeModal")?.addEventListener("click", closeEnlargeModal);
document.getElementById("enlargePrevImage")?.addEventListener("click", () => { window.prevImage(); enlargeImage(); });
document.getElementById("enlargeNextImage")?.addEventListener("click", () => { window.nextImage(); enlargeImage(); });
document.getElementById("imageEnlargeModal")?.addEventListener("click", (e) => { if (e.target.id === "imageEnlargeModal") closeEnlargeModal(); });
document.addEventListener("keydown", (e) => {
const isProjectModalOpen = !document.getElementById("projectModal")?.classList.contains("hidden");
const isEnlargeModalOpen = !document.getElementById("imageEnlargeModal")?.classList.contains("hidden");
if (isEnlargeModalOpen) {
if (e.key === "ArrowLeft") { window.prevImage(); enlargeImage(); }
if (e.key === "ArrowRight") { window.nextImage(); enlargeImage(); }
if (e.key === "Escape") closeEnlargeModal();
} else if (isProjectModalOpen) {
if (e.key === "ArrowLeft") window.prevImage();
if (e.key === "ArrowRight") window.nextImage();
if (e.key === "Escape") closeModal();
}
});
// --- Initialize Page ---
loadProjects();
});