
document.addEventListener("DOMContentLoaded", ()=>{
   const newsFullContent = document.querySelector(".news-page-full");
   if(newsFullContent){
       const allImages = newsFullContent.querySelectorAll("img");
       if(allImages.length > 0){
           allImages.forEach((image, index)=>{
               image.addEventListener("click", ()=>{
                    openGallery(index, allImages);
               });
           });
       }
   }

});

const openGallery = (index, allImages)=>{
    let current = index;
    document.body.classList.add("overflow-hidden");
    const galleryBg = document.createElement("div");
    galleryBg.id = "gallery-content-bg";
    galleryBg.addEventListener("click", (e)=>{
        if(e.target.id === "gallery-content-bg") {
            galleryBg.remove();
            document.body.classList.remove("overflow-hidden");
        }
    });

    const galleryClose = document.createElement("div");
    galleryClose.id = "gallery-close";
    galleryClose.innerHTML = `<svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 512 512" height="24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="M405 136.798L375.202 107 256 226.202 136.798 107 107 136.798 226.202 256 107 375.202 136.798 405 256 285.798 375.202 405 405 375.202 285.798 256z"></path></svg>`;
    galleryClose.addEventListener("click", ()=> {
        galleryBg.remove();
        document.body.classList.remove("overflow-hidden");
    });
    galleryBg.appendChild(galleryClose);

    const image = document.createElement("img");
    image.src = allImages[index].getAttribute("src");

    galleryBg.appendChild(image);

    if(allImages.length > 1){
        const toNext = ()=>{
            const next = current < allImages.length - 1? current + 1 : 0;
            image.src = allImages[next].getAttribute("src");
            current = next;
        }

        const toPrev = ()=>{
            const prev = current > 0 ? current -1 : allImages.length - 1;
            image.src = allImages[prev].getAttribute("src");
            current = prev;
        }

        const leftButton = document.createElement("div");
        leftButton.id = "gallery-left";
        leftButton.className = "gallery-button";
        leftButton.addEventListener("click", toPrev);
        leftButton.innerHTML = `<svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 256 256" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M128,24A104,104,0,1,0,232,128,104.11,104.11,0,0,0,128,24Zm21.66,138.34a8,8,0,0,1-11.32,11.32l-40-40a8,8,0,0,1,0-11.32l40-40a8,8,0,0,1,11.32,11.32L115.31,128Z"></path></svg>`;
        galleryBg.appendChild(leftButton);
        const rightButton = document.createElement("div");
        rightButton.id = "gallery-right";
        rightButton.className = "gallery-button";
        rightButton.addEventListener("click", toNext);
        rightButton.innerHTML = `<svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 256 256" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M128,24A104,104,0,1,0,232,128,104.11,104.11,0,0,0,128,24Zm29.66,109.66-40,40a8,8,0,0,1-11.32-11.32L140.69,128,106.34,93.66a8,8,0,0,1,11.32-11.32l40,40A8,8,0,0,1,157.66,133.66Z"></path></svg>`;
        galleryBg.appendChild(rightButton);

        let started = false;
        let currentLeft;
        image.addEventListener("touchstart", (e)=>{
            e.preventDefault();
            started = e.touches[0].clientX;
        });

        image.addEventListener("mousedown", (e)=>{
            e.preventDefault();
            started = e.clientX;
        });

        galleryBg.addEventListener("touchend", (e)=>{
            if(started){
                if(currentLeft > 0){
                    toPrev();
                } else {
                    toNext();
                }
                image.style.marginLeft = "0px";
                started = false;
            }

        });

        galleryBg.addEventListener("mouseup", (e)=>{
            if(started){
                if(currentLeft > 0){
                    toPrev();
                } else {
                    toNext();
                }
                image.style.marginLeft = "0px";
                started = false;
            }

        });

        galleryBg.addEventListener("touchmove", (e)=>{
            if(started){
                currentLeft = e.touches[0].clientX - started
                image.style.marginLeft = currentLeft + "px";
            }
        });

        galleryBg.addEventListener("mousemove", (e)=>{
            if(started){
                currentLeft = e.clientX - started
                image.style.marginLeft = currentLeft + "px";
            }
        });
    }





    document.body.appendChild(galleryBg);
}