var imgPreLoad = new Array();
var oldSrc = "";
var oldHeight = 1;
var oldWidth = 1;
var oldText = "";
var fullImgSrc = "";

function preloadImage(src, path, alt) {
	img = new Image();
	img.src = src;
	img.setAttribute("originalSrc", path);
	img.alt = alt;
	imgPreLoad[imgPreLoad.length] = img;
	if(imgPreLoad.length == 1) {
		if(img.width == 0)
			Sys.UI.DomEvent.addHandler(img, "load", loadFirstImage);
		else
			loadFirstImage();
    }
}
function setImageContainerHeight(src) {
    var newImg = findPreLoadedImage(src);
    var img = $get("prodImage");
    var imageDiv = $get("prodImages");
    if (parseInt(newImg.height) < parseInt(imageDiv.offsetHeight)) {
        // add padding to image to center image vertically
        var margin = (parseInt(imageDiv.offsetHeight) - parseInt(newImg.height)) / 2;
        img.style.marginTop = margin + "px";
        imageDiv.style.height = imageDiv.offsetHeight + "px";
    } else {
        imageDiv.style.height = newImg.height + "px";
        img.style.marginTop = "0px";
    }
}

function showImg(src) {
    setImageContainerHeight(src);
	var img = $get("prodImage");
	var newImg = findPreLoadedImage(src);

	//alert(newImg.height);
	img.setAttribute("height", newImg.height);
	img.setAttribute("width", newImg.width);
	// In IE7 need to set height and width twice for some reason!!
	if (img.height == 1 || img.width == 1) {
	    img.setAttribute("height", newImg.height);
	    img.setAttribute("width", newImg.width);
	}
    fullImgSrc = newImg.getAttribute("originalSrc");
    oldText = img.alt = $get("imgDescription").innerHTML = newImg.alt;
    img.title = img.alt + " - Click to view original full size image";
    img.src = src;
    // set anchor tag href
    img.parentNode.setAttribute("href", fullImgSrc);
    oldSrc = img.src;
    oldHeight = img.height;
    oldWidth = img.width;
}

function findPreLoadedImage(src) {
    for (var i = 0; i < imgPreLoad.length; i++) {
        if (imgPreLoad[i].src.toLowerCase() == src.toLowerCase() ||
				imgPreLoad[i].src.toLowerCase().indexOf(escape(src.toLowerCase())) != -1) {
            return imgPreLoad[i];
        }
    }
    return null;
}

function rOverImage(src) {
    setImageContainerHeight(src);
    var img = $get("prodImage");
	//oldSrc = img.src;
	oldHeight = img.height;
	oldWidth = img.width;

	var newImg = findPreLoadedImage(src);	
	img.height = newImg.height;
	img.width = newImg.width;
	$get("imgDescription").innerHTML = newImg.alt;
	img.src = src;						
}

function rOutImage() {
    setImageContainerHeight(oldSrc);
    var img = $get("prodImage");
	img.src = oldSrc;					
	img.height = oldHeight;
	img.width = oldWidth;
	if(document.getElementById) {
		var desc = document.getElementById("imgDescription");
		desc.innerHTML = oldText;
	}
}	

function loadFirstImage() {
	showImg(imgPreLoad[0].src);
}

function fittedImage() {
	window.open("/w3a_dyn/fittedImage.aspx?src=" + fullImgSrc + "&title=" + oldText,"fittedImage","height=100, width=100, scrollbars=no, location=no, address=no, resizable=yes");
}