function close_all_galleries() {
    document.getElementById("c00000000").style.display = "block";
    document.getElementById("c20060625").style.display = "none";
    document.getElementById("c20051211").style.display = "none";
    document.getElementById("c20050709").style.display = "none";
    document.getElementById("c20040619").style.display = "none";
    document.getElementById("c20010617").style.display = "none";
}

function open_gallery(canvas) {
    obj = document.getElementById(canvas);
    if (obj.style.display != "block") {
        close_all_galleries();
        obj.style.display = "block";
        document.getElementById("c00000000").style.display = "none";
    }
}

function _calc_resize_factor(width, height) {
    min_margin = 12.0;
    max_extent = 600 - 2*min_margin;
    if (width > height) {
        factor = max_extent / width;
    } else {
        factor = max_extent / height;
    }
    if (factor > 1.0) {
        factor = 1.0;
    }
    return factor;
}

function _calc_css_margin(width, height) {
    top_bottom = (600 - height) / 2;
    left_right = (600 - width) / 2;
    return top_bottom + "px " + left_right + "px";
}

// Just uses appendChild, so old_obj must be the last child.
function _replace_node(old_obj, new_obj) {
    parent_obj = old_obj.parentNode
    parent_obj.removeChild(old_obj);
    parent_obj.appendChild(new_obj);
}

function view_in_gallery(canvas, imgName, width, height) {
    open_gallery(canvas);
    old_img = document.getElementById(canvas).getElementsByTagName("img")[0];
    factor = _calc_resize_factor(width, height);
    new_img = document.createElement("img");
    new_img.src = imgName;
    new_img.width = factor * width;
    new_img.height = factor * height;
    new_img.style.margin = _calc_css_margin(new_img.width, new_img.height);
    _replace_node(old_img, new_img);
    return false;
}

