/**
 *  mouse functions
 *
 *  these functions base on code that I have found on the internet without any
 *  copyright information or information about the author
 *
 *  @author   Tobias Hettinger, www.hettinger.info
 *  @version  $Id: mouse.js,v 1.7 2007/11/13 12:33:14 tobias Exp $
 */


//  horizontal position of the mouse on the screen
watMousePosX = 0; 
//  vertical position of the mouse on the screen
watMousePosY = 0;    
//  width of the page
watMousePosMaxX = 0;
//  height of the page
watMousePosMaxY = 0;


/**
 *  function to get the vertical scroll position of the current document
 *
 *  @return int  the function returns the current vertical scroll position of the document
 */
function watMouseGetScrollTop()
{
  //  get the scroll position
  if (typeof window.pageYOffset != 'undefined')
    return window.pageYOffset;
  else
  {
    if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat')
      return document.documentElement.scrollTop;
    else
      if (typeof document.body != 'undefined')
        return document.body.scrollTop;
  }
        
  //  the scroll position could not be determined
  return 0;
}

/**
 *  Set Netscape up to run the "watCaptureMousePosition" function whenever
 *  the mouse is moved. For Internet Explorer and Netscape 6, you can capture
 *  the movement a little easier.
 */
if (document.layers)
{
  //  Netscape
  document.captureEvents(Event.MOUSEMOVE);
  document.onmousemove = watCaptureMousePosition;
}
else
{
  if (document.all)
    //  Internet Explorer
    document.onmousemove = watCaptureMousePosition;
  else
    if (document.getElementById)
      //  Netcsape 6
      document.onmousemove = watCaptureMousePosition;
}
  
/**
 *  function to capture the mouse position and to update the global
 *  variables that contain the state of the mouse
 */
function watCaptureMousePosition(e)
{
  if (document.layers)
  {
    watMousePosX = e.pageX;
    watMousePosY = e.pageY;
    watMousePosMaxX = window.innerWidth+window.pageXOffset;
    watMousePosMaxY = window.innerHeight+window.pageYOffset;
  }
  else
  {
    if (document.all)
    {
      if (window.event && window.event.x && document.body)
      {
        var scrollPos = watMouseGetScrollTop();
        watMousePosX = window.event.x+document.body.scrollLeft;
        watMousePosY = window.event.y+scrollPos;
        watMousePosMaxX = document.body.clientWidth+document.body.scrollLeft;
        watMousePosMaxY = document.body.clientHeight+document.body.scrollTop;
      }
    }
    else
    {
      if (document.getElementById)
      {
        //  Netscape 6 behaves the same as Netscape 4 in this regard 
        watMousePosX = e.pageX;
        watMousePosY = e.pageY;
        watMousePosMaxX = window.innerWidth+window.pageXOffset;
        watMousePosMaxY = window.innerHeight+window.pageYOffset;
      }
    }
  }
}

/**
 *  function to get the source of a mouse event
 *
 *  @param object event  mouse event
 *  
 *  @return object  the function returns the source element of the mouse event or null if it could not
 *                  be determined
 */
function watMouseGetEventSource(event)
{
  //  initialize the target
  var target = null;
 
  //  get the element
  if (!e) var e = window.event;
  if (e.target) target = e.target;
  else if (e.srcElement) target = e.srcElement;
    if (target.nodeType == 3) // defeat Safari bug
      target = target.parentNode;

  //  return the element or null if no element is available
  return targ;
}
