﻿function getNaturalHeight(img) {
    if (img.naturalHeight) {
        return img.naturalHeight;
    } else {
        lgi = new Image();
        lgi.src = img.src;
        return lgi.height;
    }
}

function getNaturalWidth(img) {
    if (img.naturalWidth) {
        return img.naturalWidth;
    } else {
        var lgi = new Image();
        lgi.src = img.src;
        return lgi.width;
    }
}

function displayPhotoViewer() {
    if ($("div#PhotoViewer").length) {
        $("div#PhotoViewer").after("<div id='LoadingScreen'><img src='/bilder/bigrotation2.gif' alt='' /></div>");

        $(window).load(function() {
            $("div#LoadingScreen").hide();

            $("div#PhotoViewer img").each(function() {

                if (getNaturalHeight(this) > getNaturalWidth(this)) {
                    $(this).data("orientation", "vertical");
                    $(this).css("width", "40px").css("height", "auto");
                }
                else {
                    $(this).data("orientation", "horizontal");
                    $(this).css("height", "40px").css("width", "auto");
                }
                $(this).wrap("<div class='ThumbnailWrapper'></div>");
            });
            $("div#PhotoViewer").append("<div class='Clear'></div>");
            $("div#PhotoViewer").after("<div id='PhotoWindow'><img src='/imagens/estrutura/photo-window-default-pt.png' alt='' /></div>");
            $("div#PhotoWindow").append("<br /><span id='PhotoCaption'></span>");
            $("div#PhotoWindow").after("<div class='Clear'></div>");

            $("div.ThumbnailWrapper").each(function() {
                var bigImage = $("div#PhotoWindow img");
                var bigImageCaption = $("span#PhotoCaption");
                var smallImage = $(this).find("img");
                $(this).bind("click", function() {
                    $(this).stop(true, true);
                    $(bigImage).stop(true, true);
                    $(bigImageCaption).stop(true, true);
                    $(this).fadeOut("fast", function() {
                        $(this).fadeIn("fast");
                    });
                    bigImageCaption.fadeOut("fast");
                    bigImage.fadeOut("fast", function() {
                        var clickedImageSrc = smallImage.attr("src");
                        bigImage.attr("src", clickedImageSrc);
                        if (smallImage.data("orientation") == "vertical") {
                            bigImage.css("height", "450px").css("width", "auto");
                        }
                        else {
                            bigImage.css("width", "450px").css("height", "auto");
                        }
                        bigImageCaption.html(smallImage.attr("title"));
                        bigImage.fadeIn("fast");
                        bigImageCaption.fadeIn("fast");
                    });
                });
            });
            $("div#PhotoViewer").fadeIn("fast");
            $("div#PhotoWindow").fadeIn("fast");
            $("div.ThumbnailWrapper:first").click();
        });             
    }
}

$(document).ready(function() {
    displayPhotoViewer();
});

$(window).load(function() {
});            
                    
                

                
                    
                
