var timerId = 0;
var imageSize;
var zoomSize;
function startZoom(img) {
    var p = getAbsolutePosition(img);
    var d = document.getElementById("zoom");
    var b = document.getElementById("zoombg");
    var i = document.getElementById("zoomimg");
    imageSize = new Image();
    imageSize.src = img.src;
    zoomSize = {
        width:img.offsetWidth,
        height:img.offsetHeight,
        ratio:(imageSize.width-img.offsetWidth)/(imageSize.height-img.offsetHeight),
        movex:((d.offsetWidth-imageSize.width)/2 - p.x) / (imageSize.height-img.offsetHeight) / 5,
        movey:((d.offsetHeight-imageSize.height)/2 - p.y) / (imageSize.height-img.offsetHeight) / 5
        };
    i.src = img.src;
    i.style.width = img.offsetWidth + "px";
    i.style.height = img.offsetHeight + "px";
    i.style.top = p.y + "px";
    i.style.left = p.x + "px";
    b.style.opacity = "0.1";
    b.style.visibility = "visible";
    d.style.visibility = "visible";
    timerId = setInterval("zoom()", 50);
}
function stopZoom() {
    var d = document.getElementById("zoom");
    var b = document.getElementById("zoombg");
    d.style.visibility = "hidden";
    b.style.visibility = "hidden";
    imageSize = null;
    if (timerId) clearInterval(timerId);
}
function zoom() {    
    var d = document.getElementById("zoombg");
    var i = document.getElementById("zoomimg");
    var f = parseFloat(d.style.opacity);
    zoomSize.width = Math.min(imageSize.width, zoomSize.width + 10 * zoomSize.ratio);
    zoomSize.height = Math.min(imageSize.height, zoomSize.height + 10);
    i.style.top = (parseInt(i.style.top) + zoomSize.movey) + "px";
    i.style.left = (parseInt(i.style.left) + zoomSize.movex) + "px";
    i.style.width = parseInt(zoomSize.width) + "px";
    i.style.height = parseInt(zoomSize.height) + "px";
    d.style.opacity = Math.min(0.8, f + 0.05);
    if (zoomSize.width >= imageSize.width && zoomSize.height >= imageSize.height) clearTimeout(timerId);
}
