function _zoomInit(img, zoom_name) {
    var zoom = null;

    if (is.ie) {
        // in ie create a span that will be sized to the zoom
        //document.write('<span ID="'+zoom_name+'" onClick="doClick(event)" onMouseMove="doMove(event)" onDblClick="doClick(event)" style="position:absolute; visibility:hidden; style.background:Images/box.gif; top:0; left:0; background-image: url(Images/box.gif); " ></span>');
        zoom = document.all[zoom_name];
    } else if (is.ns) {
        // in ns create a span that covers the page and clip it as necessary
//    document.write('<span id='+zoom_name+' STYLE="background-image: url(Images/box.gif);visibility:hidden">here it is</span>');
     //   document.write(
     //       "<span id='+zoom_name+' top='0' left='0' visibility='hidden' background='Images\box.gif' width='100%' HEIGHT='100%'></span>");
//document.write("<div id='"+zoom_name+ "' style='visibility:hidden'><img src='Images/box.gif'/></div>");
        //    window.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP );
        //    window.onMouseMove = doMouseMoveNew;
        //    window.onMouseDown=doMouseDownNew;
        //    window.onMouseUp=doMouseUpNew;
        zoom = document.getElementById(zoom_name);
    }
    zoom.action = "";
    // in page coordinate system
    zoom.first_x = "";
    zoom.first_y = "";
    zoom.last_x = "";
    zoom.last_y = "";
    _spanHide(zoom);
    img.zoom = zoom;

    return zoom;
}

function _zoomProcessEvent(img, e) {
    var myobj = document.images['map_image0'];
    var objLeft = myobj.offsetLeft;
    var objTop = myobj.offsetTop;
    var objParent = myobj.offsetParent;

    while (objParent != null) {
        objLeft += objParent.offsetLeft;
        objTop += objParent.offsetTop;
        objParent = objParent.offsetParent;
    }

    hspc = objLeft;
    vspc = objTop;

    var x = 0;
    var y = 0;

    if (is.ie) {
        x = e.clientX + document.body.scrollLeft;
        y = e.clientY + document.body.scrollTop;
    } else if (is.ns) {
        x = e.pageX;
        y = e.pageY;
    }

    var typ = "";
    if (e.type == "mousemove") {
        typ = "MOVE";
    } else if (e.type == "mousedown") {
        typ = "DOWN";
    } else if (e.type == "mouseup") {
        typ = "UP";
    } else {
        typ = "UNKNOWN";
    }
/*    if(window.status.length > 150)
        window.status = "";
    if (e.type == "mousedown")
        window.status = "";*/
    //window.status = window.status + typ+" - ";


    img = myobj;
    if (e.type == "mousemove") {
        if (img.zoom.action != "start")
            return false;
        //alert(e.type+" -- "+img.zoom.action);
//        window.status = window.status+"-";
//        alert("move");

        _zoomMove(img, x, y, hspc, vspc);
    } else if (e.type == "mousedown") {
//        window.status = "MOUSEDOWN "+e.type+" -- "+img.zoom.action;
        if (is.ns && e.target != img) {
            alert(e.type+" -- "+img.zoom.action+" RETURN FALSE");
            return false;
        }
        //alert(e.type+" -- "+img.zoom.action);

//        window.status = window.status + " DOWN";
//        alert("down");
        _zoomStart(img, x, y, hspc, vspc);
        //alert("started");
    } else if (e.type == "mouseup") {
        //alert(e.type+" -- "+img.zoom.action);
//        window.status = window.status + " UP";
//        alert("up");
        if (img.zoom.action != "start")
            return false;

        _zoomEnd(img, x, y);
        //alert("ended");
    } else {
        alert("UNKNOWN event "+e.type+" -- "+img.zoom.action);
        return false;
    }
    //alert("DONE ZOOMING");
    return true;
}

function _zoomStart(img, x, y, hspc, vspc) {
    if (is.ie) {
        img.style.cursor = 'crosshair';
    } else if(is.ns) {
        img.style.cursor = 'crosshair';
    }

    img.zoom.action = "start";
    img.zoom.first_x = x;
    img.zoom.first_y = y;
    img.zoom.last_x = "";
    img.zoom.last_y = "";
    //alert("start "+x+"\n"+y+"\n"+hspc+"\n"+vspc);
//    window.status = window.status + " S("+img.zoom.first_x+", "+img.zoom.first_y+") -> ("+img.zoom.last_x+", "+img.zoom.last_y+")";
    _zoomMove(img, x, y, hspc, vspc);
    _spanShow(img.zoom);
}

function _zoomEnd(img, x, y) {
    if (is.ie) {
        img.style.cursor = 'auto';
    } else if(is.ns) {
        img.style.cursor = 'auto';
    }

    img.zoom.action = "end";
    _spanHide(img.zoom);
    return true;
}

function _zoomMove(img, x2, y2, hspc, vspc) {
    if (img.zoom.action != "start")
        return;
        img.style.cursor = 'crosshair';

    var img_x = null;
    var img_y = null;
    var img_w = null;
    var img_h = null;

    if (is.ie) {
        img_x = hspc; //img.offsetLeft;
        img_y = vspc; //img.offsetTop;
        img_w = img.offsetWidth;
        img_h = img.offsetHeight;
    } else if (is.ns) {
        img_x = img.x;
        img_y = img.y;
        img_w = img.width;
        img_h = img.height;
    }

    // ensure in image bounds
    if (x2 < img_x) {
        x2 = img_x;
    } else if (x2 > (img_x + img_w)) {
        x2 = img_x + img_w;
    }

    if (y2 < img_y) {
        y2 = img_y;
    } else if (y2 > (img_y + img_h)) {
        y2 = img_y + img_h;
    }

    img.zoom.last_x = x2;
    img.zoom.last_y = y2;
//    window.status = window.status + " E("+img.zoom.first_x+", "+img.zoom.first_y+") -> ("+img.zoom.last_x+", "+img.zoom.last_y+")";
    //alert("end "+x2+"\n"+y2);
//    window.status = "MOVE last_x="+x2+"; last_y="+y2;

    // calculate top, left, width and height
    var x1 = img.zoom.first_x;
    var y1 = img.zoom.first_y;
    var width = Math.abs(x1 - x2);
    var height = Math.abs(y1 - y2);
    var x = Math.min(x1, x2);
    var y = Math.min(y1, y2);

    if (is.ie) {
        width = Math.max(width, 2) - 2;
        height = Math.max(height, 2) - 2;
    } else if (is.ns) {
        width = Math.max(width, 2) - 2;
        height = Math.max(height, 2) - 2;
        x = Math.min(x1, x2) + 2;
        y = Math.min(y1, y2) + 2;
    }

    if (is.ie) {
        // in ie set zoom span size
        img.zoom.style.pixelLeft = x;
        img.zoom.style.pixelTop = y;
        img.zoom.style.pixelWidth = width;
        img.zoom.style.pixelHeight = height;
            /*alert("pixel left"+img.zoom.style.pixelLeft+"\n"+
            "pixel top"+img.zoom.style.pixelTop +"\n"+
            "pixel width"+img.zoom.style.pixelWidth +"\n"+
            "pixel height"+img.zoom.style.pixelHeight );*/
    } else if (is.ns) {
        // in ns clip zoom span
        if(img.zoom.clip) {
            img.zoom.clip.left = x;
            img.zoom.clip.top = y;
            img.zoom.clip.width = width;
            img.zoom.clip.height = height;
        } else {
            img.zoom.style.left = x;
            img.zoom.style.top = y;
            img.zoom.style.width = width;
            img.zoom.style.height = height;
            /*alert("pixel left"+img.zoom.style.pixelLeft+"\n"+
            "pixel top"+img.zoom.style.pixelTop +"\n"+
            "pixel width"+img.zoom.style.pixelWidth +"\n"+
            "pixel height"+img.zoom.style.pixelHeight );*/
        }
    }
}

function _zoomFinished(img) {
    var done = false;

    if (img.zoom.action == "end") {
        img.zoom.action = "";

        done = true;
    }
    return done;
}

function _zoomIsBounds(img) {
    if ((Math.abs(img.zoom.first_x - img.zoom.last_x) > 3) && (Math.abs(img.zoom.first_y - img.zoom.last_y) > 3)) {
        return true;
    } else {
/*    alert("out of bounds\n"+
    "first x:"+img.zoom.first_x +"\n"+
     "last x: "+img.zoom.last_x +"\n"+
    "first y:"+img.zoom.first_y +"\n"+
    "last y:"+img.zoom.last_y +
    " click="+ clickTime+" move="+moveTime +" up="+upTime);*/
        return false;
    }
}

// In IMAGE coordinate system !!!

function _zoomGetBounds(img) {
    var myobj = document.images['map_image0'];
    var objLeft = myobj.offsetLeft;
    var objTop = myobj.offsetTop;
    var objParent = myobj.offsetParent;

    while (objParent != null) {
        objLeft += objParent.offsetLeft;
        objTop += objParent.offsetTop;
        objParent = objParent.offsetParent;
    }

    hspc = objLeft;
    vspc = objTop;

    var img_x = null;
    var img_y = null;

    if (is.ie) {
        img_x = hspc; //img.offsetLeft;
        img_y = vspc; //img.offsetTop;
    } else if (is.ns) {
        img_x = img.x;
        img_y = img.y;
    }

    var bnds = (img.zoom.first_x - img_x) + "," + (img.zoom.first_y - img_y) + ", " + (img.zoom.last_x - img_x) + ","
            + (img.zoom.last_y - img_y);
    return bnds;
}

// In IMAGE coordinate system !!!

function _zoomGetPoint(img) {
    var myobj = document.images['map_image0'];
    var objLeft = myobj.offsetLeft;
    var objTop = myobj.offsetTop;
    var objParent = myobj.offsetParent;

    while (objParent != null) {
        objLeft += objParent.offsetLeft;
        objTop += objParent.offsetTop;
        objParent = objParent.offsetParent;
    }

    hspc = objLeft;
    vspc = objTop;

    var img_x = null;
    var img_y = null;

    if (is.ie) {
        img_x = hspc; //img.offsetLeft;
        img_y = vspc; //img.offsetTop;
    } else if (is.ns) {
        img_x = img.x;
        img_y = img.y;
    }

    var pnt = (img.zoom.first_x - img_x) + "," + (img.zoom.first_y - img_y);

    return pnt;
}