﻿
function GetGalleryList() {
    $.ajax({
        type: "GET",
        url: "gallery.xml",
        dataType: "xml",
        success: function(xml) {
            galleryList = xml;
            BuildGallerySection('Branding');
        }
    })
}

function BuildGallerySection(sectionName) {
    $(galleryList).find('Category').each(function() {
        var currentCategory = this;
        $(this).find('Title').each(function() {
            if (this.firstChild.nodeValue == sectionName) {
                currentGallerySection = $(currentCategory).find('ImageResource');
            }
        })
    })

    galleryImageIndex = 0;
    SetCurrentGalleryImage();
}

function PreviousGalleryEntry() {
    galleryImageIndex = galleryImageIndex - 1;
    SetCurrentGalleryImage();
}

function NextGalleryEntry() {
    galleryImageIndex = galleryImageIndex + 1;
    SetCurrentGalleryImage();
}

function SetCurrentGalleryImage() {
    var imageLink = $('#GalleryCaption');
    var currentImage = currentGallerySection[galleryImageIndex];
    var imageUrl = $(currentImage).find('ImageSource')[0].firstChild.nodeValue;
    var caption = BuildCaption(currentImage);

    $("#imgGalleryImage").attr("src", imageUrl);
    if ($('#captionContents').length > 0) {
        $('#captionContents').remove();
    }
    imageLink.append(caption);

    EnableGalleryButtons();
}

function BuildCaption(currentImage) {
    var captionString;

    var imageLinkText = $(currentImage).find('Caption')[0].firstChild.nodeValue;
    if ($(currentImage).find('Link')[0].firstChild != null) {
        imageLinkHref = $(currentImage).find('Link')[0].firstChild.nodeValue;
        captionString = '<a id="captionContents" class="BodyLink" href="' + imageLinkHref + '" class="GreenLink" target="_blank">' + imageLinkText + '</a>';
    }
    else {
        captionString = '<span id="captionContents" class="GalleryCaption">' + imageLinkText + '</span>';
    }
    var captionBuilder = $(captionString);
    return captionBuilder;
}

function EnableGalleryButtons() {
    if (galleryImageIndex < currentGallerySection.size() - 1)
        $("#btnNextClient").css("display", "block");
    else
        $("#btnNextClient").css("display", "none");

    if (galleryImageIndex > 0)
        $("#btnPreviousClient").css("display", "block");
    else
        $("#btnPreviousClient").css("display", "none");
}