﻿
    var retried = false;

    function ResizeImage(img, MaxWidth, MaxHeight)
    {
        // Find out the images dimensions from the image on disk
        var imgDisk = new Image();
        imgDisk.src = img.src;
        
        // Don't show the image until it is resized (fixes a flicker in FF)
        if (navigator.userAgent.indexOf("Firefox") > 0)
          img.style.display = "none";

        // Set the dimensions of the image on the screen to match
        img.width = imgDisk.width;
        img.height = imgDisk.height;

        // Shrink the image to fit, if needed
        if (img.height > img.width)
        {
            if (img.height > MaxHeight)
            {
                img.height = MaxHeight;
                img.width = img.width * img.height / imgDisk.height;
            }
        }
        else
        {
            if (img.width > MaxWidth)
            {
                img.width = MaxWidth;
                img.height = img.height * img.width / imgDisk.width;
            }
            else if (img.height > MaxHeight)
            {
                img.height = MaxHeight;
                img.width = img.width * img.height / imgDisk.height;
            }
        }

        img.style.display = "block";
        imgDisk = null;
        /*
        if (retried == false && (img.height > MaxHeight || img.width > MaxWidth))
        {
        alert(img.height + ":" + img.width);
            retried = true;
            ResizeImage(img, MaxWidth, MaxHeight)
        }
        */
    }
