
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:img\box.gif; top:0; left:0; background-image: url(img/box.gif); " ></span>');
    zoom = document.all[zoom_name];
  }
  else if (is.ns)
  {
    document.write('<LAYER name="'+zoom_name+'" top="0" left="0" VISIBILITY="HIDDEN" BACKGROUND="img/box.gif" width="100%" HEIGHT="100%"></LAYER>');

//    window.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP );
//    window.onMouseMove = doMouseMoveNew;
//    window.onMouseDown=doMouseDownNew;
//    window.onMouseUp=doMouseUpNew;

    if (document.layers)
      zoom = document.layers[zoom_name];
    else if (document.all)
      zoom = document.all[zoom_name];
    else if (document.getElementById)
      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)
{
//  document.forms[0].xxx.value="_zoomProcessEvent:"+e.type;

  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;
  }

//  document.forms[0].xxx.value="_zoomProcessEvent:"+x+","+y;

  if (e.type == "mousemove")
  {
    if (img.zoom.action!="start")
      return false;

//    document.forms[0].xxx.value="_zoomMove:"+x+","+y;
    _zoomMove(img, x, y);
  }
  else if (e.type == "mousedown")
  {
    if (is.ns && e.target!=img)
    {
      return false;
    }

//    document.forms[0].xxx.value="_zoomStart:"+x+","+y;
    _zoomStart(img, x, y);
  }
  else if (e.type == "mouseup")
  {
    if (img.zoom.action!="start")
      return false;

//    document.forms[0].xxx.value="_zoomEnd:"+x+","+y;
    _zoomEnd(img, x, y);
  }
  else
  {
    return false;
  }

  return true;
}

function _zoomStart(img, x, y)
{
//document.captureEvents(Event.MOUSEMOVE);
//document.onmousemove=drag;

  if (is.ie)
  {
    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="";

  _zoomMove(img, x, y);
  _spanShow(img.zoom);
}

function _zoomEnd(img, x, y)
{
//document.onmousemove=null;
//document.releaseEvents(Event.MOUSEMOVE);

  if (is.ie)
  {
    img.style.cursor = 'auto';
  }

  img.zoom.action="end";

//  img.zoom.last_x=x;
//  img.zoom.last_y=y;

  _spanHide(img.zoom);

  return true;
}

function _zoomMove(img, x2, y2)
{
  if (img.zoom.action != "start")
    return ;

  var img_x = null;
  var img_y = null;
  var img_w = null;
  var img_h = null;

  img_x = getImagePageLeft(img);
  img_y = getImagePageTop(img);

  if (is.ie)
  {
    img_w = img.offsetWidth;
    img_h = img.offsetHeight;
  }
  else if (is.ns)
  {
    img_w = img.width;
    img_h = img.height;
  }

  // if (is.ns)
  {
    // 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;

  // 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;
  }
  else if (is.ns)
  {
    // in ns clip zoom span

    img.zoom.clip.left = x;
    img.zoom.clip.top = y;
    img.zoom.clip.width=width;
    img.zoom.clip.height=height;
  }
}

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
  {
    return false;
  }
}

// In IMAGE coordinate system !!!
function _zoomGetBounds(img)
{
  var img_x = getImagePageLeft(img);
  var img_y = getImagePageTop(img);

/**
  var img_x = null;
  var img_y = null;

  if (is.ie)
  {
    img_x = img.offsetLeft;
    img_y = 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 img_x = getImagePageLeft(img);
  var img_y = getImagePageTop(img);

/**
  var img_x = null;
  var img_y = null;

  if (is.ie)
  {
    img_x = img.offsetLeft;
    img_y = 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;
}