
function getImageOriginalDimensions(a_$imgElement)
{
    var image = new Image();
    image.src = a_$imgElement.attr("src");
    return { "width": image.width, "height": image.height };
}

function resizePaintingImage()
{
    var $imageContainer = $("#content_inner");
    var imageContainerWidth = $("#sidebar").offset().left - $imageContainer.offset().left;
    var imageContainerHeight = $("#footer").offset().top - $imageContainer.offset().top;
    var $imageElement = $imageContainer.find("img");

    //var originalSize = getImageOriginalDimensions($imageElement);
    var originalSize = g_imageDimensions;
    var widthScalingPercentage = imageContainerWidth * 100 / originalSize.width;
    var heightScalingPercentage = imageContainerHeight * 100 / originalSize.height;

    if (widthScalingPercentage < heightScalingPercentage)
    {
        // Squeezing width more than height / fit to left and right of container
        $imageElement.css("width", imageContainerWidth.toString() + "px");
        $imageElement.css("height", "auto");
    }
    else
    {
        // Squeezing height more than width / fit to top and bottom of container
        $imageElement.css("height", imageContainerHeight.toString() + "px");
        $imageElement.css("width", "auto");
    }

    if (widthScalingPercentage < heightScalingPercentage && 
        $("body").hasClass("zoomed") && getCookie("zoomBehaviour") == "verticalCentre")
    {
        // Was using $imageElement.height() for this but that occasionally comes out at 0,
        // when in zoomed mode and hit 'next' link onto each diptych when loaded the first time after opening the browser
        var newImageHeight = originalSize.height * widthScalingPercentage / 100;

        $imageElement.css("position", "relative");
        $imageElement.css("top", ((imageContainerHeight - newImageHeight) / 2).toString() + "px");
    }
    else
    {
        $imageElement.css("position", "");
        $imageElement.css("top", "");
    }
}

function zoomToggle()
{
    $("body").toggleClass("zoomed");
    $(window).trigger("resize");
}

$(function () {
    try { ie_onDomContentLoaded(); } catch (e) {}
    $(window).bind("resize", resizePaintingImage);
    $(window).trigger("resize");

    //if (getCookie("zoomBehaviour") == "instant" || getCookie("zoomBehaviour") == "verticalCentre")
    $("#content img").bind("click", zoomToggle);
});

