function showFullSize(src) { document.images["full"].src = src }

function highlight(img)
{
    var images = document.getElementsByTagName("IMG");
    for (var i = 0; i < images.length; i++)
	if (images[i].className == "highlighted")
	    images[i].className = "thumbnail";
    img.className = "highlighted";
}

function hasClass(elt, className)
{
    var classes = elt.className.split(/\s+/);
    for (var i = 0; i < classes.length; i++)
        if (classes[i] == className)
            return true;
    return false;
}

function addClass(elt, className)
{
    var classes = elt.className.split(/\s+/);
    for (var i = 0; i < classes.length; i++)
        if (classes[i] == className)
            return;
    classes.push(className);
    elt.className = classes.join(" ");
}

function removeClass(elt, className)
{
    var classes = elt.className.split(/\s+/);
    var newClasses = [];
    for (var i = 0; i < classes.length; i++)
        if (classes[i] != className)
            newClasses.push(classes[i]);
    elt.className = newClasses.join(" ");
}

function show(elt) { removeClass(elt, "hidden") }
function hide(elt) { addClass(elt, "hidden") }
function isHidden(elt) { return hasClass(elt, "hidden") }
function togglehidden(elt) { if (isHidden(elt)) show(elt); else hide(elt) }

var zoomHandlersInstalled = false;
function maybeInstallZoomHandlers(image, zoom)
{
    if (!zoomHandlersInstalled) {
	if (document.implementation.hasFeature("Events", "2.0")) {
	    // IE does not use DOM Level 2 events, but has special
	    // heuristics that make the zoom image "transparent" to
	    // events, so this special logic isn't needed there.
	    image.onmouseout = image.onmouseover = null;
	    image.addEventListener("mouseout",
				   function(e) {
				       if (e.relatedTarget != zoom) hide(zoom)
				   }, false);
	    image.addEventListener("mouseover",
				   function(e) {
				       if (e.relatedTarget != zoom) show(zoom)
				   }, false);
	}
	zoomHandlersInstalled = true;
    }
}

function showzoom()
{
    var image = document.images["full"],
	zoom = document.getElementById("zoom");
    maybeInstallZoomHandlers(image, zoom);
    show(zoom);
}

function hidezoom()
{
    hide(document.getElementById("zoom"));
}

function togglesize(width, height, fullWidth, fullHeight)
{
    var image = document.images["full"],
	thumbs = document.getElementById("thumbs"),
	zoom = document.getElementById("zoom"),
	info = document.getElementById("item-info");

    // Mozilla's incremental reflow engine is a horrible piece of garbage.
    // The screwing around we're doing below causes it to become terminally
    // confused; forcibly removing and then re-attaching the image seems to
    // be the only way to get it to DTRT (as of Gecko/20071107).
    var parent = image.parentNode, sibling = image.nextSibling;
    parent.removeChild(image);

    if (hasClass(image, "full-size")) {
	// full -> normal size
        image.width = width;
        image.height = height;
	removeClass(image, "full-size");

	thumbs.style["width"] = "auto";
	thumbs.style["marginLeft"] = "0";
	thumbs.style["clear"] = "left";
	thumbs.style["verticalAlign"] = "bottom";
    } else {
	// normal -> full size
        image.width = fullWidth;
        image.height = fullHeight;
        addClass(image, "full-size");

	thumbs.style["width"] = "64px";
	thumbs.style["marginLeft"] = (fullWidth + 10) + "px";
	thumbs.style["clear"] = "none";
	thumbs.style["verticalAlign"] = "top";
    }

    // The zoom images are named "+.png" and "-.png"; this swaps the one
    // for the other, taking care to treat IE specially (see fixPNG below).
    function swapPlusMinus(s) {
	var i = s.lastIndexOf(".png") - 1;
	return s.substring(0, i)
	    + (s.charAt(i) == "+" ? "-" : "+")
	    + s.substring(i+1);
    }
    if (navigator.userAgent.indexOf("MSIE") != -1)
	zoom.style.filter = swapPlusMinus(new String(zoom.style.filter));
    else
	zoom.src = swapPlusMinus(new String(zoom.src));

    togglehidden(info);
    parent.insertBefore(image, sibling);
}

/*
 * IE 5 & 6 support PNG, but not with transparency.  This horrible kludge
 * replaces an IMG element whose source is a PNG with a DIV of the same
 * dimensions, id, and class, to which we apply a CSS transformation that
 * loads the original image source in a way that supports transparency.
 */
function fixPNG(img)
{
    if (navigator.userAgent.indexOf("MSIE") == -1 || !/\.png$/.test(img.src))
	return;

    // IE doesn't like to report width and height for hidden images.
    var wasHidden;
    if ((wasHidden = isHidden(img)))
	show(img);

    var div = document.createElement("div");
    div.id = img.id;
    div.className = img.className;
    div.onclick = img.onclick;
    div.style.width = img.width;
    div.style.height = img.height;
    div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img.src + "', sizingMethod='scale')";

    img.parentNode.replaceChild(div, img);
    if (wasHidden)
	hide(div);
}

