﻿function fileChanged() {
    f = document.getElementById("File");
    o = document.getElementById("preview");

    o.style.display = "none";
    o.src = "file:///" + f.value;


    var newImg = new Image();
    newImg.src = o.src;

    if (newImg.width == 0) {
        newImg.onload = function() {
            var div = 1;
            var maxSize = 300;

            if (newImg.width > newImg.height && newImg.width > maxSize) {
                div = newImg.width / maxSize;
            }
            else if (newImg.height > maxSize) {
                div = newImg.height / maxSize;
            }

            o.style.width = newImg.width / div;
            o.style.pixelHeight = newImg.height / div;
        }
    }
    else {
        var div = 1;
        var maxSize = 300;

        if (newImg.width > newImg.height && newImg.width > maxSize) {
            div = newImg.width / maxSize;
        }
        else if (newImg.height > maxSize) {
            div = newImg.height / maxSize;
        }

        o.style.width = newImg.width / div;
        o.style.pixelHeight = newImg.height / div;

    }

    o.style.display = "block";
}

function ShowImageOld(image) {
    var windowname = ""
    var details, URL
    var max = screen.height - 90

    //if (theHeight > max) theHeight = max

    //URL = "image.aspx?i=" + idx + "&u=" + username
    URL = image;

    //details = "titlebar=no,left=20,top=20,toolbar=no, address=no, status=no, scrollbars=yes, resizable=yes, height=800 ,width=600";
    details = "titlebar=no,left=20,top=20,toolbar=no, address=no, status=no, scrollbars=yes, resizable=yes, height=auto ,width=auto";
    var win2 = window.open(URL, windowname, details);
    win2.focus()
}

function setImage(image, newImg) {
    var imgObject = document.getElementById("ShowBigPicture");

    if (imgObject != null) {
        imgObject.style.display = "none";
        imgObject.src = "";
        var height = newImg.height;
        var width = newImg.width;
        var scaleFactor = 1;

        if (width > height) {
            if (width > 800) {
                scaleFactor = 800 / width;
                width = 800;
                height = height * scaleFactor;
            }
        }
        else {
            if (height > 600) {
                scaleFactor = 600 / height;
                height = 600;
                width = width * scaleFactor;
            }
        }

        imgObject.width = width;
        imgObject.height = height;
        imgObject.src = image;

        imgObject.style.display = "block";
        window.scrollTo(0, 740);
    }
}

function ShowImage(image) {
    var tableObject = document.getElementById("BigPictureTable");

    if (tableObject != null) {
        var newImg = new Image();
        newImg.src = image;

        if (navigator.appName == "Microsoft Internet Explorer") {
            setTimeout(function() { }, 100);
            newImg.onload = function() {
                setImage(image, newImg);
            }
            setImage(image, newImg);
        }
        else {
            newImg.onload = function() {
                setImage(image, newImg);
            }
        }

        tableObject.style.display = "block";
    }
}

function HideBigPicture() {
    var tableObject = document.getElementById("BigPictureTable");

    if (tableObject != null) {
        tableObject.style.display = "none";
        window.scrollTo(0, 740);
    }
}
