var viewer;

$(document).ready(function() {
    // Replace the detail-Img with the IIIF-Implementation ONLY if there is an IIIF-Link to this objcet
    if ($('.iiifLink').length) {
        if ($('.exhibitionContainer').length) {
            var listImage = $('#collectionInlineModule .listImg');
            listImage.html('');
            listImage.attr('id', 'openseadragon');
        }
        if ($('.artistContainer').length) {
            var listImage = $('#collectionInlineModule .listImg');
            listImage.html('');
            listImage.attr('id', 'openseadragon');
        } else {
            var detailImage = $('#collectionDetailItem .detailImg');
            detailImage.html('');
            detailImage.attr('id', 'openseadragon');
        }

        // Grab the link from the hidden spareSField05
        var iiifLink = $('.iiifLink span:first-child').html();
        iiifLink += "/info.json";

        // The Plugin. Render into div with id openseadragon
        viewer = OpenSeadragon({
            id: "openseadragon",
            prefixUrl: "?service=WebAsset&url=openseadragon-bin-2.1.0/images/",
            tileSources: iiifLink
        });

        // append &contentType=image/png to all navImage URLs
        var navImageURLs = $('#openseadragon img');

        for (var i = 0; i < navImageURLs.length; i++) {
            var currentURL = navImageURLs[i].getAttribute("src");
            currentURL += "&contentType=image/png";
            navImageURLs[i].setAttribute("src", currentURL);
        }


        // Fullscreen-Link
        $('#fullscreenDiv').css("display", "block");

        $('.fullscreenLink').click(function() {
            // viewer.raiseEvent("pre-full-screen");
            // viewer.raiseEvent("full-screen");
            viewer.setFullScreen(true);
        });

        // Set height of the openseadragon window to screen height
        $('.detailItem #openseadragon').attr('style', 'height: ' + ($(window).height() - 180) + "px;");

    }
});