// 4/16/08 - Edited mousePos and elementLocation. Now works in IE6.

var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;

function PicExpander(areaId) {

  this.areaEl = document.getElementById(areaId);
  this.picEl = null;
  this.textEl = null;
  this.areaId = areaId;

  var allDivs = this.areaEl.getElementsByTagName('div');
  for (var i = 0; i < allDivs.length; i++) {
    if (allDivs[i].className == 'pePic') {
      this.picEl = allDivs[i];
    } else if (allDivs[i].className == 'peText') {
      this.textEl = allDivs[i];
    }
  }

  this.maxWidth = parseInt(this.picEl.getAttribute('maxWidth'));
  this.minWidth = this.picEl.clientWidth;
  this.maxHeight = parseInt(this.picEl.getAttribute('maxHeight')) || this.maxWidth;
  this.minHeight = this.picEl.clientHeight;

  registeredPics.push(this);

  document.onmousemove = doMouseMove;

  this.mouseMove = function(ev, mousePos) {
    var newWidth = scaleSize(this.picEl, mousePos, this.minWidth, this.maxWidth);
    var newHeight = scaleSize(this.picEl, mousePos, this.minHeight, this.maxHeight);
    this.picEl.style.width = newWidth + 'px';
    this.picEl.style.height = newHeight + 'px';
  }
}

registeredPics = [];
function doMouseMove(e)
{
    var ev = e || window.event;
    var mousePos = mouseCoords(ev);

    for(var i = 0; i < registeredPics.length; i++) {
      registeredPics[i].mouseMove(ev, mousePos);
    }
}

function scaleSize(el, pt, minSize, maxSize, centerSize)
{
  if (centerSize == null) centerSize = 20;
  var dist = distanceToEl2(pt, el); // Distance squared.
  dist -= centerSize * centerSize;
  var diff = maxSize - minSize;
  var maxDist = 24 * diff * diff;
  var minDist = 0;
  if (dist > maxDist) return minSize;
  if (dist < minDist) return maxSize;

  return minSize + diff * (maxDist - dist) / maxDist;
}

function pointInside(pt, el)
{
  var elLoc = elementLocation(el);
  return (pt.x >= elLoc.x && pt.x < elLoc.x + el.clientWidth &&
	  pt.y >= elLoc.y && pt.y < elLoc.y + el.clientHeight);
}

function distanceToEl2(pt, el)
{
  var elLoc = elementLocation(el);
  var center = {
    x: elLoc.x + el.clientWidth / 2,
    y: elLoc.y + el.clientHeight / 2
  };
  return distanceToPoint2(pt, center);
}

/*
  IE note:

  In order to get the position of an element correctly, all parent
  elements must have the "hasLayout" internal flag set. If the
  position does not seem to be calculating properly, ensure this flag
  is set on all parents in the hierarchy. One easy way to do this is
  to set "zoom:1" in the CSS.
*/
function elementLocation(e)
{
  var left = 0;
  var top  = 0;

  while (e){
    left += e.offsetLeft;
    top  += e.offsetTop;
    e     = e.offsetParent;
  }

  return {x:left, y:top};
}


function distanceToPoint(pt1, pt2)
{
  return Math.sqrt(distanceToPoint2(pt1, pt2));
}

function distanceToPoint2(pt1, pt2)
{
  var xDelta = pt1.x - pt2.x;
  var yDelta = pt1.y - pt2.y;
  return xDelta * xDelta + yDelta * yDelta;
}

function mouseMove(ev){
  ev           = ev || window.event;
  var mousePos = mouseCoords(ev);
}

function mouseCoords(ev)
{
  if(ev.pageX || ev.pageY){
    return {x:ev.pageX, y:ev.pageY};
  }
  return {
  x:ev.clientX + (document.documentElement.scrollLeft ?
		  document.documentElement.scrollLeft :
		  document.body.scrollLeft),
      y:ev.clientY + (document.documentElement.scrollTop ?
		      document.documentElement.scrollTop :
		      document.body.scrollTop)
      };
}

