/*
 * $Id: viewer-ops.js,v 1.4 2002/08/14 17:54:01 sforde Exp $
 *
 * file: viewer-ops.js
 * purpose: functions enabling the dynamic-html, no-plugin image viewer
 */

var isNS4 = false;
var isNS6 = false;

/* These constants are used for scaling calculations during a pan. */
var srcHeight, srcWidth, getimageURL;

/* Store the original level so we can reset it if necessary. */
var origLevel;

function noop(retval) {
   return retval;
}

/**
 * offX(event)
 *
 * Calculates the x-coordinate distance between the image centerpoint
 * and the image click-point, using the click event as dictated by
 * browser type.  Value to be used in a call to panBy().
 */
function offX(evt) {
   var imgX;
   if (isNS6) {
      imgX = evt.clientX;
      for (var offMark = evt.target; offMark; offMark = offMark.offsetParent)
         imgX -= offMark.offsetLeft;
   } else if (isNS4)
      imgX = evt.x - document.imgView.x;
   else
      imgX = evt.offsetX;
   return imgX - (document.viewForm.wid.value >> 1);
}

/**
 * offY(event)
 *
 * Calculates the y-coordinate distance between the image centerpoint
 * and the image click-point, using the click event as dictated by
 * browser type.  Value to be used in a call to panBy().
 */
function offY(evt) {
   var imgY;
   if (isNS6) {
      imgY = evt.clientY;
      for (var offMark = evt.target; offMark; offMark = offMark.offsetParent)
         imgY -= offMark.offsetTop;
   } else if (isNS4)
      imgY = evt.y - document.imgView.y;
   else
      imgY = evt.offsetY;
   return imgY - (document.viewForm.hei.value >> 1);
}

/**
 * levelCvt(value, level, scale)
 *
 * Converts an image-based coordinate delta to a region-based delta,
 * taking into account the current magnification level.
 */
function levelCvt(val, lev, scale)
{
   if (lev > 0)
      val <<= lev;
   else
      val >>= -lev;
   val /= scale;
   return val;
}

/**
 * panBy(deltaX, deltaY [, flagZoom = false])
 *
 * Given a delta vector in image-based coordinates, recalculates the
 * region-based centerpoint and reloads the image.  If the zoom flag
 * is set, the image will be magnified by one level, as well.
 */
function panBy(dX, dY, fZoom)
{
   var cpArr = document.viewForm.cp.value.split(",");
   var lev = document.viewForm.lev.value;
   var cpX = parseFloat(cpArr[0]) + levelCvt(dX, lev, srcWidth);
   var cpY = parseFloat(cpArr[1]) + levelCvt(dY, lev, srcHeight);
   document.viewForm.cp.value = cpX.toString() + "," + cpY.toString();
   if (fZoom)
      return zoomBy(-1);
   else
      return reloadImage();
}

/**
 * zoomBy(numlevels)
 *
 * Changes the magnification level and reloads the image.
 */
function zoomBy(levs)
{
   document.viewForm.lev.value = parseInt(document.viewForm.lev.value) + levs;
   return reloadImage();
}

/**
 * viewReset()
 *
 * Restores the original centerpoint and magnification level, then
 * reloads the image.
 */
function viewReset()
{
   document.viewForm.lev.value = origLevel;
   document.viewForm.cp.value = "0.5,0.5";
   return reloadImage();
}

/**
 * makeArg(formElement)
 *
 * Converts the form element into a string for use as a URL argument.
 */
function makeArg(formElement)
{
   var retVal = formElement.name + "=" + formElement.value + "&";
   return retVal;
}

/**
 * reloadImage()
 *
 * Reloads the image by constructing a URL from the existing form
 * values.  The URL is loaded by setting the SRC of the IMG object.
 */
function reloadImage()
{
   var newURL = getimageURL;
   var vf = document.viewForm.elements;

   for (var i = 0; i < vf.length; i++)
   {
      if (vf[i].type == "hidden")
         newURL += makeArg(vf[i]);
   }
   document.images["imgView"].src = newURL;
}

/**
 * initViewerOps(sourceImageHeight, sourceImageWidth, imageServerURL)
 *
 * Set constants, calculate initial level, and load first image.
 */
function initViewerOps(sWidth, sHeight, sImageServer)
{
   getimageURL = sImageServer+"/getimage?"
   srcWidth = sWidth;
   srcHeight = sHeight;
   for (origLevel = 0; sHeight > (document.viewForm.hei.value * 1.4) ||
        sWidth > (document.viewForm.wid.value * 1.4); origLevel++) {
      sWidth /= 2;
      sHeight /= 2;
   }
   viewReset();

   isNS4 = (navigator.appName == "Netscape" && document.layers);
   isNS6 = (navigator.appName == "Netscape" && document.getElementById);
}
