var showDiv ;
var x=0 ;
var y=0 ;
var vx=0 ;
var vy=0 ;
var otime,ox,oy ;
var xPointer, yPointer, vxPointer, vyPointer ;

var speedShow = window.setInterval( "showMouseSpeed()", 100 ) ;
//window.captureEvents( Event.MOUSEMOVE );
window.onmousemove = mouseMove;

function intPrint( value ) {
  var out ;
  out = ( value >= 0 ) ? " " : "" ; 
  absValue = Math.abs( value ) ;
  if( absValue < 10 ) {
    out = out + "   ";
  } else {
    if( absValue < 100 ) {
      out = out + "  ";
    } else {
      if( absValue < 1000 ) {
        out = out + " ";
      }
    }
  }
  return out + value ;
}

function showMouseSpeed() {
  if( typeof showDiv == "undefined" ) {
    // create parent div
    var pDiv = document.createElement("div");
    pDiv.setAttribute( "id", "mousedata" ) ;
    pDiv.setAttribute( "style", "font-family:monospace;" ) ;
    document.getElementsByTagName( "body" )[0].appendChild( pDiv );
    showDiv = document.getElementById( "mousedata" ) ;
    // create child nodes
    var xDiv = document.createElement("div");
    var yDiv = document.createElement("div");
    var vxDiv = document.createElement("div");
    var vyDiv = document.createElement("div");
    // set id of nodes
    xDiv.setAttribute( "id", "mousedata-x" ) ;
    yDiv.setAttribute( "id", "mousedata-y" ) ;
    vxDiv.setAttribute( "id", "mousedata-vx" ) ;
    vyDiv.setAttribute( "id", "mousedata-vy" ) ;
    // append nodes to mousdata div
    showDiv.appendChild( xDiv ) ;
    showDiv.appendChild( yDiv ) ;
    showDiv.appendChild( vxDiv ) ;
    showDiv.appendChild( vyDiv ) ;
    // create text notes
    var xData = document.createTextNode( "" ) ;
    var yData = document.createTextNode( "" ) ;
    var vxData = document.createTextNode( "" ) ;
    var vyData = document.createTextNode( "" ) ;
    // append nodes
    document.getElementById( "mousedata-x" ).appendChild( xData );
    document.getElementById( "mousedata-y" ).appendChild( yData );
    document.getElementById( "mousedata-vx" ).appendChild( vxData );
    document.getElementById( "mousedata-vy" ).appendChild( vyData );
    // return pointers
    xPointer = document.getElementById( "mousedata-x" ).firstChild  ;
    yPointer = document.getElementById( "mousedata-y" ).firstChild  ;
    vxPointer = document.getElementById( "mousedata-vx" ).firstChild  ;
    vyPointer = document.getElementById( "mousedata-vy" ).firstChild  ;
  }
  if( typeof showDiv == "object" ) {
    //var xData = document.createTextNode( "X:" + x ) ;
    //yPointer.replaceChild( xData, xPointer.firstChild ) ;
    xPointer.data = " x = " + intPrint( x ) ; 
    yPointer.data = " y = " + intPrint( y ) ; 
    //vxPointer.nodeValue = "vx =" + intPrint( Math.round( vx * 100 ) ) ; 
    //vyPointer.nodeValue = "vy =" + intPrint( Math.round( vy * 100 ) ) ;
    //showDiv.firstChild.nodeValue="X:" + x + "    Y:" + y + "    vx:" + vx + "    vy:" + vy ;
    
  }
}

function mouseMove( moveEvent ) {
  x = moveEvent.clientX ;
  y = moveEvent.clientY ;
  return;
  time = new Date();
  if( typeof ox == "number" ) {
    tdiff = time.getTime() - otime.getTime(); 
    /*vx = Math.round(( x - ox ) / tdiff * 1000 );
    vy = Math.round(( y - oy ) / tdiff * 1000 );
    ax = Math.round(( vx - ovx ) / tdiff * 1000 );
    ay = Math.round(( vy - ovy ) / tdiff * 1000 );*/
    vx = ( x - ox ) / tdiff ;
    vy = ( y - oy ) / tdiff ;
  }
  ox = x;
  oy = y ;
  otime = time;
  ovx = vx;
  ovy = vy;        
  //document.getElementById( "mousedata" ).firstChild.nodeValue="X:" + moveEvent.clientX + "\nY:" + moveEvent.clientY + "X:" + moveEvent.offsetX + "\nY:" + moveEvent.offsetY + "X:" + moveEvent.pageX + "\nY:" + moveEvent.pageY + "X:" + moveEvent.screenX + "\nY:" + moveEvent.screenY + "X:" + moveEvent.x + "\nY:" + moveEvent.y ;
}
