/**
 * A simple xy Coordinate class and a set of static methods that help in dealing 
 * with them.
 */
function Coordinate(x, y)
{
  this.x = x;
  if (!this.x)
    this.x = 0
  this.y = y;
  if (!this.y)
    this.y = 0
}
Coordinate.Statics = new Object()

/**
 * findPos - finds the coordinate of the top left of an dom node with respect 
 * to the window or an absolutely positioned parent.
 *
 * @parameter obj 	- the object that you want to know coordinate of
 * @returns					-	the coordinate
 */
Coordinate.Statics.findPos = function(obj)
{
  var position = new Coordinate(0, 0);
  var depth = 0;
  var objStyle = obj.currentStyle;
  if (!objStyle)
    objStyle = document.defaultView.getComputedStyle(obj,'');
  if (obj.offsetParent)
  {
    while (obj.offsetParent && (0==depth || 'absolute'!=objStyle.position))
    {
      position.x += obj.offsetLeft;
      position.y += obj.offsetTop;
      obj = obj.offsetParent;
      objStyle = obj.currentStyle;
      if (!objStyle)
        objStyle = document.defaultView.getComputedStyle(obj,'');
      depth += 1;
    }
  }
  else 
  {
    if (obj.x)
      position.x += obj.x;
    if (obj.y)
      position.y += obj.y;
  }
  return position;
}

/**
 * findAbsolutePos - finds absolute the coordinate of the top left of an dom node with respect to the viewport
 *
 * @parameter obj 	- the object that you want to know coordinate of
 * @returns					-	the coordinate
 */
Coordinate.Statics.findAbsolutePos = function(obj)
{
  var position = new Coordinate(0, 0);
  var offsetParent = null;
  var depth = 0;
  var objStyle = obj.currentStyle;
  if (!objStyle)
    objStyle = document.defaultView.getComputedStyle(obj,'');
  if (obj.offsetParent)
  {
    offsetParent = obj;
    while (obj.parentNode)
    {
      if('body'==obj.nodeName.toLowerCase())
        break;

      if(obj == offsetParent)
      {
          position.x += obj.offsetLeft;
          position.y += obj.offsetTop;
          offsetParent = obj.offsetParent;
      }
      position.x -= parseInt(obj.scrollLeft);
      position.y -= parseInt(obj.scrollTop);
      if(objStyle && 'absolute'==objStyle.position)
      {
        // fixme: handle nested absolutely positioned elements
        break;
      }
      obj = obj.parentNode;
      try{
      objStyle = obj.currentStyle;
      if (!objStyle)
        objStyle = document.defaultView.getComputedStyle(obj,'');
      } catch (e) {}
      depth += 1;
    }
  }
  else 
  {
    if (obj.x)
      position.x += obj.x;
    if (obj.y)
      position.y += obj.y;
  }
  return position;
}

/**
 * findEventPosition - finds the coordinate of an event
 *
 * @parameter event	- the event that you want to know coordinate of
 * @returns					-	the exent coordinate
 */
Coordinate.Statics.findEventPosition = function(event)
{
  if (!event)
    event = window.event;
  if (!event.target) 
		event.target = event.srcElement;
  var doc = event.target.ownerDocument;
  var win = doc.defaultView;

  var x = event.clientX;
  if (win && win.scrollX)
    x += win.scrollX;
  else
    x = x + doc.documentElement.scrollLeft + doc.body.scrollLeft - 2;

  var y = event.clientY;
  if (win && win.scrollY)
    y += win.scrollY;
  else
    y = y + doc.documentElement.scrollTop + doc.body.scrollTop - 2;

  return new Coordinate(x, y);
}

