﻿$(document).ready(function () {
    // Load theme
    Galleria.loadTheme('/scripts/galleria.fullscreen.js');

    var galleryInnerHtml = $('#galleria').html();
    var galleryInnerHtmlSmall = $('#small-galleria').html();
    var galleryInnerHtmlInfo = $('#info-galleria').html();

    $('a.carousel-link, img.img-carousel-link').live('click', function () {
        var imageIndex = $('a.carousel-link').index(this);
        var galleryInnerHtml = $('#galleria').html();
        initializeGallery(galleryInnerHtml, imageIndex);
        $("#galleria").show();
    });

    $('a[id$=lnkImageViewer]').live('click', function () {
        var imageIndex = 0;
        var galleryInnerHtml = $('#galleria').html();
        initializeGallery(galleryInnerHtml, imageIndex);
        $("#galleria").show();
    });

    $('a.small-carousel-link, img.img-carousel-link').live('click', function () {
        var imageIndex = $('a.small-carousel-link').index(this);
        var galleryInnerHtmlSmall = $('#small-galleria').html();
        initializeGallerySmall(galleryInnerHtmlSmall, imageIndex);
        $("#small-galleria").show();
    });

    $('a[id$=lnkImageViewerSmall]').live('click', function () {
        var imageIndex = 0;
        var galleryInnerHtmlSmall = $('#small-galleria').html();
        initializeGallerySmall(galleryInnerHtmlSmall, imageIndex);
        $("#small-galleria").show();
    });

    $('a[id$=lnkImageViewerInfo]').live('click', function () {
        var imageIndex = indexTemp; //used from jquery.npFullBgImg.js for getting index of current background image
        var galleryInnerHtmlInfo = $('#info-galleria').html();
        initializeGalleryInfo(galleryInnerHtmlInfo, imageIndex);
        $("#info-galleria").show();
    });
});

var exitFullScreen = false;
function initializeGallery(initialHtml, showIndex) {
    $('#galleria').galleria({
        image_crop: false, // crop all images to fit
        thumb_crop: true, // crop all thumbnails to fit
        transition: 'fade', // crossfade photos
        transition_speed: 700, // slow down the crossfade
        show: showIndex,
        autoplay: ImageViewerTimeInterval,
        data_config: function (img) {
            // will extract and return image captions from the source:
            return {
                title: $(img).parent().next('strong').html(),
                description: $(img).parent().next('strong').next().html()
            };
        },
        extend: function () {
            this.bind(Galleria.FULLSCREEN_EXIT, function (e) {
                exitFullScreen = true;
                $("#galleria").hide();
                $('#galleria').html(initialHtml);
            });
        }
    });
}
   
function initializeGallerySmall(initialHtml, showIndex) {
    $('#small-galleria').galleria({
        image_crop: false, // crop all images to fit
        thumb_crop: true, // crop all thumbnails to fit
        transition: 'fade', // crossfade photos
        transition_speed: 700, // slow down the crossfade
        show: showIndex,
        autoplay: ImageViewerTimeInterval,
        data_config: function (img) {
            // will extract and return image captions from the source:
            return {
                title: $(img).parent().next('strong').html(),
                description: $(img).parent().next('strong').next().html()
            };
        },
        extend: function () {
            this.bind(Galleria.FULLSCREEN_EXIT, function (e) {
                exitFullScreen = true;
                $("#small-galleria").hide();
                $('#small-galleria').html(initialHtml);
            });
        }
    });
}

    function initializeGalleryInfo(initialHtml, showIndex) {
    $('#info-galleria').galleria({
        image_crop: false, // crop all images to fit
        thumb_crop: true, // crop all thumbnails to fit
        transition: 'fade', // crossfade photos
        transition_speed: 700, // slow down the crossfade
        show: showIndex,
        autoplay: ImageViewerTimeInterval,
        data_config: function (img) {
            // will extract and return image captions from the source:
            return {
                title: $(img).parent().next('strong').html(),
                description: $(img).parent().next('strong').next().html()
            };
        },
        extend: function () {
            this.bind(Galleria.FULLSCREEN_EXIT, function (e) {
                exitFullScreen = true;
                $("#info-galleria").hide();
                $('#info-galleria').html(initialHtml);
            });
        }
    });
}
