/******************************************************************/
/*
**  Compute X-Y points within an EZP plot
*/

document.write(
  '<div id=ezp_info onclick=ezp_hide() style="'				+
  '	position:	absolute;'					+
  '	z-index:	1010;'						+
  '	left:		0px;'						+
  '	top:		0px;'						+
  '	visability:	hidden;'					+
  '	">'								+
  '  <table border=1 cellpadding=3 cellspacing=0 bgcolor=#fff0f0>'	+
  '    <tr><td id=ezp_block> line1 <br> line2 </td></tr>'		+
  '  </table>'								+
  '</div>');

var ezp_div   = document.getElementById("ezp_info");
var ezp_block = document.getElementById("ezp_block");
var ezp_isie  = document.all ? 1 : 0;

/******************************************************************/
/*
**  Get rid of our info block
*/

function ezp_hide()
{

  ezp_div.style.visibility = 'hidden';

}

ezp_hide();

/******************************************************************/
/*
**  Add new attributes to the images in our page
*/

function ezp_init(name, xp, yp, xo, yo, xs, ys, xt, yt)
{

  images = document.getElementsByTagName("img");

  for (i = 0; i < images.length; i++) {
    if (images.item(i).src.indexOf(name) != -1) {
      images.item(i).ezp_xp = xp;
      images.item(i).ezp_yp = yp;
      images.item(i).ezp_xo = xo;
      images.item(i).ezp_yo = yo;
      images.item(i).ezp_xs = xs;
      images.item(i).ezp_ys = ys;
      images.item(i).ezp_xt = xt ? xt : 'D';
      images.item(i).ezp_yt = yt ? yt : 'F';
      if (ezp_isie) {
	images.item(i).attachEvent("onclick", show_xy);
      } else {
	images.item(i).addEventListener("click", show_xy, true);
      }
      break;
    }
  }
}

/***********************************************************************/
/*
**  Find the upperleft corner of our image
*/

function getY(obj)
{
  return obj.offsetParent == null ? obj.offsetTop
		                  : obj.offsetTop + getY(obj.offsetParent);
}

function getX(obj)
{
  return obj.offsetParent == null ? obj.offsetLeft
				  : obj.offsetLeft + getX(obj.offsetParent);
}

/***********************************************************************/
/*
**  Convert a number of seconds into a date string
*/

function show_date(secs, scale)
{

  then = new Date();

  then.setTime(secs * 1000);
  GMTadd  = -1 * then.getTimezoneOffset() * 60;

  divider = scale > 86400 ? 86400 :
	    scale > 21600 ? 21600 :
	    scale > 3600  ? 3600 :
	    scale > 1800  ? 1800 :
	    scale > 900   ? 900 :
	    scale > 60    ? 60 :
	    scale > 1     ? 1 :
	    scale > 0.1   ? 0.1 :
	    scale > 0.01  ? 0.01 :
	    scale > 0.001 ? 0.001 : 0.0001;

  rsec = (Math.round((secs + GMTadd) / divider) * divider) - GMTadd;
  then.setTime(rsec * 1000);
  return then.toLocaleString();

}

/***********************************************************************/
/*
**  Round off a floating point value to an integer
*/

function show_int(rval, scale)
{

  divider = scale > 1000  ? 1000 :
	    scale > 100	  ? 100 :
	    scale > 10	  ? 10 : 1;

  return Math.round(rval / divider) * divider;

}

/***********************************************************************/
/*
**  Round off a floating point value to a reasonable number of places
*/

function show_real(rval, scale)
{

  if (scale >= 1) {

    divider = scale >= 1000  ? 1000 :
              scale >= 100   ? 100 :
              scale >= 10    ? 10 : 1;

    return Math.round(rval / divider) * divider;

  } else {

    var sign = rval < 0 ? "-" : "";

    digits = scale >= 0.1 ? 1 :
             scale >= 0.01 ? 2 :
             scale >= 0.001 ? 3 : 4;

    str = new String(Math.round(Math.abs(rval) * Number("1e" + digits)));
    len = str.length;

    if (len < digits + 1) {
      zero = "00000";
      return sign + "0" + "." + zero.substring(0, digits-len) + str;
    } else {
      return sign + str.substring(0, len-digits) + "." +
                str.substring(len-digits, len);
    }
  }
}

/***********************************************************************/
/*
**  Show a value in the proper format
*/

function show_val(val, scale, vtype)
{

  if (vtype == 'D') return show_date(val, scale);
  if (vtype == 'I') return show_int(val, scale);
  return show_real(val, scale);

}

/***********************************************************************/
/*
**  Compute the X-Y coordinants of the clicked point and display them.
*/

function show_xy(ev)
{

  if (ezp_div) {
    ev = (ev) ? ev : ((window.event) ? window.event : "");

    image = ev.target ? ev.target : ev.srcElement;
    ix = getX(image);
    iy = getY(image);

    mx = image.width;
    my = image.height;

    if (ezp_isie) {
      sx = document.body.scrollLeft;
      sy = document.body.scrollTop;
    } else {
      sx = window.scrollX;
      sy = window.scrollY;
    }

    /*
    **  The round function here is to keep IE from treating sy as a string.
    */

    px = Math.round(sx) + ev.clientX - ix - (ezp_isie ? 3 : 0);
    py = Math.round(sy) + ev.clientY - iy - (ezp_isie ? 3 : 0);

    x = image.ezp_xo + image.ezp_xs * (     px - image.ezp_xp);
    y = image.ezp_yo + image.ezp_ys * (my - py - image.ezp_yp);

    line2 = document.createTextNode(
		"x = " + show_val(x, image.ezp_xs, image.ezp_xt));
    line1 = document.createTextNode(
		"y = " + show_val(y, image.ezp_ys, image.ezp_yt));

    junk1 = ezp_block.replaceChild(line1, ezp_block.firstChild);
    junk2 = ezp_block.replaceChild(line2, ezp_block.lastChild);

    ezp_div.style.top  = iy + 5;
    ezp_div.style.left = ix + 5 + mx;
    ezp_div.style.visibility = 'visible';
  }
}

/***********************************************************************/

