﻿var menuDiv;

function getNextSibling(startBrother) {
    endBrother = startBrother.nextSibling;
    while (endBrother.nodeType != 1) {
        endBrother = endBrother.nextSibling;
    }
    return endBrother;
}
function setHeight(value) {
    menuDiv.style.height = value + 'px';
}

// width to resize large images to
var maxWidth = 100;
// height to resize large images to
var maxHeight = 100;
// valid file types
var fileTypes = ["bmp", "gif", "png", "jpg", "jpeg"];
// the id of the preview image tag
var outImage = "previewField";
// what to display when the image is not valid
var defaultPic = "spacer.gif";
var globalPic;



function preview(what) {
    var source = what.value;
    console.log(source);
    
    var ext = source.substring(source.lastIndexOf(".") + 1, source.length).toLowerCase();
    for (var i = 0; i < fileTypes.length; i++) if (fileTypes[i] == ext) break;
    globalPic = new Image();
    if (i < fileTypes.length) {
        globalPic.src = "file:\/\/" + what.value;
        console.log(globalPic.src);
    }
    else {
        globalPic.src = defaultPic;
        alert("THAT IS NOT A VALID IMAGE\nPlease load an image with an extention of one of the following:\n\n" + fileTypes.join(", "));
    }
    setTimeout("applyChanges()", 200);
}
function applyChanges() {
    var field = document.getElementById(outImage);
    var x = parseInt(globalPic.width);
    var y = parseInt(globalPic.height);
    if (x == 0 && y == 0) {
        x = 100; y = 100;
    }
    if (x > maxWidth) {
        y *= maxWidth / x;
        x = maxWidth;
    }
    if (y > maxHeight) {
        x *= maxHeight / y;
        y = maxHeight;
    }
    field.style.display = (x < 1 || y < 1) ? "none" : "";
    field.src = globalPic.src;
    field.width = x;
    field.height = y;
}

