
/* Taken from http://dunnbypaul.net/js_mouse/ */

var dnd_mousex = 0;
var dnd_mousey = 0;
var dnd_grabx = 0;
var dnd_graby = 0;
var dnd_orix = 0;
var dnd_oriy = 0;
var dnd_elex = 0;
var dnd_eley = 0;
var dnd_algor = 0;

var dnd_dragobj = null;

function falsefunc() { return false; } // used to block cascading events

function dnd_init()
{
  document.onmousemove = dnd_update; // update(event) implied on NS, update(null) implied on IE
  dnd_update();
}

/*
function dnd_getMouseXY(e) // works on IE6,FF,Moz,Opera7 - updated by Dave Ratz (dratz@mt.gov)
{ 
  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)
 
  if (e)
  { 
    if (e.pageX || e.pageY)
    { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
      mousex = e.pageX;
      mousey = e.pageY;
      algor = '[e.pageX]';
      if (e.clientX || e.clientY) algor += ' [e.clientX] '
    }
    else if (e.clientX || e.clientY)
    { // works on IE6,FF,Moz,Opera7
      // Note: I am adding together both the "body" and "documentElement" scroll positions
      //       this lets me cover for the quirks that happen based on the "doctype" of the html page.
      //         (example: IE6 in compatibility mode or strict)
      //       Based on the different ways that IE,FF,Moz,Opera use these ScrollValues for body and documentElement
      //       it looks like they will fill EITHER ONE SCROLL VALUE OR THE OTHER, NOT BOTH 
      //         (from info at http://www.quirksmode.org/js/doctypes.html)
      mousex = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
      mousey = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
      algor = '[e.clientX]';
      if (e.pageX || e.pageY) algor += ' [e.pageX] '
    }
  }
}
*/

function dnd_getMouseXY(e) // works on IE6,FF,Moz,Opera7
{ 
  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)

  if (e)
  { 
    if (e.pageX || e.pageY)
    { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
      dnd_mousex = e.pageX;
      dnd_mousey = e.pageY;
      dnd_algor = '[e.pageX]';
      if (e.clientX || e.clientY) dnd_algor += ' [e.clientX] '
    }
    else if (e.clientX || e.clientY)
    { // works on IE6,FF,Moz,Opera7
      dnd_mousex = e.clientX + document.body.scrollLeft;
      dnd_mousey = e.clientY + document.body.scrollTop;
      dnd_algor = '[e.clientX]';
      if (e.pageX || e.pageY) dnd_algor += ' [e.pageX] '
    }  
  }
}


function dnd_update(e)
{
  dnd_getMouseXY(e); // NS is passing (event), while IE is passing (null)
}

function dnd_grab(context)
{
  document.onmousedown = falsefunc; // in NS this prevents cascading of events, thus disabling text selection
  dnd_dragobj = context;
  //dragobj.style.zIndex = 1000; // move it to the top
  document.onmousemove = dnd_drag;
  document.onmouseup = dnd_drop;
  dnd_grabx = dnd_mousex;
  dnd_graby = dnd_mousey;
  dnd_elex = dnd_orix = dnd_dragobj.offsetLeft;
  dnd_eley = dnd_oriy = dnd_dragobj.offsetTop;
  dnd_update();
}

function dnd_drag(e) // parameter passing is important for NS family 
{
  if (dnd_dragobj)
  {
    dnd_elex = dnd_orix + (dnd_mousex-dnd_grabx);
    dnd_eley = dnd_oriy + (dnd_mousey-dnd_graby);
    dnd_dragobj.style.position = "absolute";
    dnd_dragobj.style.left = (dnd_elex).toString(10) + 'px';
    dnd_dragobj.style.top  = (dnd_eley).toString(10) + 'px';
  }
  dnd_update(e);
  return false; // in IE this prevents cascading of events, thus text selection is disabled
}

function dnd_drop()
{
  if (dnd_dragobj)
  {
    //dragobj.style.zIndex = 0;
    dnd_dragobj = null;
  }
  dnd_update();
  document.onmousemove = dnd_update;
  document.onmouseup = null;
  document.onmousedown = null;   // re-enables text selection on NS
}
