﻿function GalleryImagesWithIndicator(p_prevLink, p_prevImage, p_currentLink, p_currentImage, p_title, p_description, p_nextLink, p_nextImage, p_nextTitle, p_nextDescription, p_indicatorId, p_indicatorIncrement)
{
    this.prevLink = p_prevLink;
    this.prevImage = p_prevImage;
    this.currentLink = p_currentLink;
    this.currentImage = p_currentImage;
    this.title = p_title;
    this.description = p_description;
    this.nextLink = p_nextLink;
    this.nextImage = p_nextImage;
    this.nextTitle = p_nextTitle;
    this.nextDescription = p_nextDescription;
    this.index = 0;
    this.galleryImages = new Dictionary();
    this.indicatorId = p_indicatorId;
    this.indicatorIncrement = p_indicatorIncrement;
}

GalleryImagesWithIndicator.prototype.imageNext = function()
{
    if (this.index < this.galleryImages.length - 1)
        this.index++;
    else
        this.index = 0;

    this.updateGalleryImages();
}

GalleryImagesWithIndicator.prototype.imagePrevious = function()
{
    if (this.index > 0)
        this.index--;
    else
        this.index = this.galleryImages.length - 1;
        
    this.updateGalleryImages();
}

GalleryImagesWithIndicator.prototype.updateGalleryImages = function()
{
    var images = this.galleryImages;
    var currentIndex = this.index

    if (images.length > 0)
    {
        if (currentIndex == 0)
        {
            if (document.getElementById(this.prevLink) != null && images[images.length - 1].url != null)
                document.getElementById(this.prevLink).href = images[images.length - 1].url;

            var img = document.getElementById(this.prevImage)

            if (img != null)
            {
                img.src = images[images.length - 1].image;
                img.alt = images[images.length - 1].title;
                img.title = images[images.length - 1].title;
            }

            if (document.getElementById(this.currentLink) != null && images[currentIndex].url != null)
                document.getElementById(this.currentLink).href = images[currentIndex].url;

            var img = document.getElementById(this.currentImage)

            if (img != null)
            {
                if (images[currentIndex].image != null)
                    img.src = images[currentIndex].image;

                if (images[currentIndex].title != null)
                {
                    img.alt = images[currentIndex].title;
                    img.title = images[currentIndex].title;
                }
            }

            if (document.getElementById(this.title) != null && images[currentIndex].title != null)
                document.getElementById(this.title).innerHTML = images[currentIndex].title;

            if (document.getElementById(this.description) != null && images[currentIndex].description != null)
                document.getElementById(this.description).innerHTML = images[currentIndex].description;

            if (document.getElementById(this.nextLink) != null && images[currentIndex + 1].url != null)
                document.getElementById(this.nextLink).href = images[currentIndex + 1].url;

            var img = document.getElementById(this.nextImage)

            if (img != null)
            {
                if (images[currentIndex + 1].image != null)
                    img.src = images[currentIndex + 1].image;

                if (images[currentIndex + 1].title != null)
                {
                    img.alt = images[currentIndex + 1].title;
                    img.title = images[currentIndex + 1].title;
                }

                if (document.getElementById(this.nextTitle) != null && images[currentIndex + 1].title != null)
                    document.getElementById(this.nextTitle).innerHTML = images[currentIndex + 1].title;

                if (document.getElementById(this.nextDescription) != null && images[currentIndex + 1].description != null)
                    document.getElementById(this.nextDescription).innerHTML = images[currentIndex + 1].description;
            }

            var indicatorElem = document.getElementById(this.indicatorId);
            indicatorElem.style.backgroundPosition = 'right 0';
        }
        else if (currentIndex == images.length - 1)
        {
            document.getElementById(this.prevLink).href = images[currentIndex - 1].url;
            var img = document.getElementById(this.prevImage)
            img.src = images[currentIndex - 1].image;
            img.alt = images[currentIndex - 1].title;
            img.title = images[currentIndex - 1].title;

            document.getElementById(this.currentLink).href = images[currentIndex].url;
            var img = document.getElementById(this.currentImage)
            img.src = images[currentIndex].image;
            img.alt = images[currentIndex].title;
            img.title = images[currentIndex].title;
            document.getElementById(this.title).innerHTML = images[currentIndex].title;
            document.getElementById(this.description).innerHTML = images[currentIndex].description;

            document.getElementById(this.nextLink).href = images[0].url;
            var img = document.getElementById(this.nextImage)
            img.src = images[0].image;
            img.alt = images[0].title;
            img.title = images[0].title;

            var indicatorElem = document.getElementById(this.indicatorId);

            var change = currentIndex * this.indicatorIncrement;
            indicatorElem.style.backgroundPosition = 'right ' + change + 'px';

            if (document.getElementById(this.nextTitle) != null && images[0].title != null)
                document.getElementById(this.nextTitle).innerHTML = images[0].title;

            if (document.getElementById(this.nextDescription) != null && images[0].description != null)
                document.getElementById(this.nextDescription).innerHTML = images[0].description;
        }
        else
        {
            if (images[currentIndex - 1].url != null)
                document.getElementById(this.prevLink).href = images[currentIndex - 1].url;

            var img = document.getElementById(this.prevImage)

            if (images[currentIndex - 1].image != null)
                img.src = images[currentIndex - 1].image;

            if (images[currentIndex - 1].title != null)
            {
                img.alt = images[currentIndex - 1].title;
                img.title = images[currentIndex - 1].title;
            }

            if (images[currentIndex].url != null)
                document.getElementById(this.currentLink).href = images[currentIndex].url;

            var img = document.getElementById(this.currentImage)

            if (images[currentIndex].image != null)
                img.src = images[currentIndex].image;

            if (images[currentIndex].title != null)
            {
                img.alt = images[currentIndex].title;
                img.title = images[currentIndex].title;
            }

            if (images[currentIndex].title != null)
                document.getElementById(this.title).innerHTML = images[currentIndex].title;

            if (images[currentIndex].description != null)
                document.getElementById(this.description).innerHTML = images[currentIndex].description;

            if (images[currentIndex + 1].url != null)
                document.getElementById(this.nextLink).href = images[currentIndex + 1].url;

            var img = document.getElementById(this.nextImage)

            if (images[currentIndex + 1].url != null)
                img.src = images[currentIndex + 1].image;

            if (images[currentIndex + 1].title != null)
            {
                img.alt = images[currentIndex + 1].title;
                img.title = images[currentIndex + 1].title;
            }

            var indicatorElem = document.getElementById(this.indicatorId);

            var change = currentIndex * this.indicatorIncrement;
            indicatorElem.style.backgroundPosition = 'right ' + change + 'px';

            if (document.getElementById(this.nextTitle) != null && images[currentIndex + 1].title != null)
                document.getElementById(this.nextTitle).innerHTML = images[currentIndex + 1].title;

            if (document.getElementById(this.nextDescription) != null && images[currentIndex + 1].description != null)
                document.getElementById(this.nextDescription).innerHTML = images[currentIndex + 1].description;
        }
    }
}

