﻿$(document).ready(function() {

    $("div.ImageGallery").each(function(index) {
        $(this).attr("canEnter", 1);
        $(this).attr("canShift", 1);
        var imgCount = $(this).find("img.ThumbImg").length;
        $(this).attr("imgCount", imgCount);
        $(this).attr("shift", 0);
        var maxShift = Math.ceil(imgCount / 5.00) - 1;
        $(this).attr("maxShift", maxShift);
        $(this).attr("selectedImageId", $(this).find("a.ImageGalleryLink:first").attr("id"));
        $(this).find("a.ImageGalleryLink[id!='" + $(this).attr("selectedImageId") + "']").hide();

        if (maxShift < 1) {
            $(this).find(".ImageGallery_LeftButton").css("background-image", "none");
            $(this).find(".ImageGallery_RightButton").css("background-image", "none");
        }

        $(this).find("a.ImageGalleryLink").fancybox({
            'transitionIn': 'none',
            'transitionOut': 'none',
            'titlePosition': 'over',
            'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
                return '<span id="fancybox-title-over" style="float: left; width: 100%; padding: 10px 0px 10px 0px;"><span style="float:left; padding-left:10px">' + (title.length ? title : '') + '<br /><i>' + $("a[id=" + currentArray[currentIndex].id + "]").attr("credit") + '</i></span><span style="float:right; padding-right:10px">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + '</span></span>';
            }
        });
    }
    );

    $(".ThumbImg").click(function() {
        var galleryDiv = $(this).closest("div.ImageGallery");
        var canEnter = galleryDiv.attr("canEnter");
        if (canEnter == 1) {
            galleryDiv.attr("canEnter", 0);
            SelectGalleryImage($(this).attr("id").replace("_Thumb", ""), galleryDiv);
        }
    });

    $("a.ImageGalleryLink").mouseenter(
        function() {
            $(this).parent().find(".ImageGallery_NoteContainer").css('z-index', 4);
        }
    );
    $(".ImageGallery_NoteContainer").mouseleave(
        function() {
            $(this).css('z-index', 0);
        }
    );

    $(".ImageGallery_NoteContainer").click(function() {
            var selectedImageId = $(this).closest("div.ImageGallery").attr("selectedImageId");
            $("a[id=" + selectedImageId + "]").click();
        }
    );

    $(".ImageGallery_LeftButton").click(function() {
        var galleryDiv = $(this).closest("div.ImageGallery");
        var canShift = galleryDiv.attr("canShift");
        if (canShift == 1) {
            galleryDiv.attr("canShift", 0);
            scrollLeft($(this));
        }
    }
    );
    $(".ImageGallery_RightButton").click(function() {
        var galleryDiv = $(this).closest("div.ImageGallery");
        var canShift = galleryDiv.attr("canShift");
        if (canShift == 1) {
            galleryDiv.attr("canShift", 0);
            scrollRight($(this));
        }
    }
    );
    $(".ThumbImg").mouseenter(
          function() {
              $(this).css('border-color', '#0093f8');
          }
      );
    $(".ThumbImg").mouseleave(
          function() {
              $(this).css('border-color', '#FFFFFF');
          }
      );
});

function scrollLeft(button) {
    var galleryDiv = button.closest("div.ImageGallery");
    var shift = parseInt(galleryDiv.attr("shift"));
    if (shift > 0) {
        button.parent().find("#motiongallery").animate({ "left": "+=160px" }, "medium", function() {
            galleryDiv.attr("shift", parseInt(galleryDiv.attr("shift")) - 1);
            galleryDiv.attr("canShift", 1);
        });
    } else {
        galleryDiv.attr("canShift", 1);
    }
}

function scrollRight(button) {
    var galleryDiv = button.closest("div.ImageGallery");
    var shift = parseInt(galleryDiv.attr("shift"));
    var maxShift = parseInt(galleryDiv.attr("maxShift"));
    if (shift < maxShift) {
        button.parent().find("#motiongallery").animate({ "left": "-=160px" }, "medium", function() {
            galleryDiv.attr("shift", parseInt(galleryDiv.attr("shift")) + 1);
            galleryDiv.attr("canShift", 1);
        });
    } else {
        galleryDiv.attr("canShift", 1);
    }
}

function SelectGalleryImage(newSelectedImageId, galleryDiv) {
    var selectedImageId = galleryDiv.attr("selectedImageId");
    if (newSelectedImageId != selectedImageId) {
        $("a[id=" + selectedImageId + "]").css('z-index', 2);
        $("a[id=" + newSelectedImageId + "]").css('z-index', 1);
        $("a[id=" + newSelectedImageId + "]").show();
        $("a[id=" + selectedImageId + "]").fadeOut(1000, function() {
            galleryDiv.attr("selectedImageId", newSelectedImageId);
            galleryDiv.attr("canEnter", 1);
        });
    } else {
        galleryDiv.attr("canEnter", 1);
    }
}
