// SCRIPT BY ANDRIS ZAČS 2006 andris.z@metaleks.lv
var IE = document.all?true:false
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;
var tempX = 0;
var tempY = 0; 
var dx = 0;
var dy = 0;
var activeEl = false;
var MOVE = false;
var DRAGNDROP = false;
function px_to_int( v ){
  return Number( v.substr( 0, v.length - 2 ) );
}
function getMouseXY(e){   
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){ tempX = 0; }
  if (tempY < 0){ tempY = 0; }  
  // ------- move obj
  if( activeEl && MOVE ){
    document.body.onmousedown = function(){ return false; };
    document.body.onselectstart = function(){ return false; };            
    activeEl.style.top = ( tempY - dy ) + 'px';
    activeEl.style.left = ( tempX - dx ) + 'px';
    activeEl.PosTop = tempY - dy;
    activeEl.PosLeft = tempX - dx;
    if( activeEl.onMove ) activeEl.onMove();     
  } else {
    document.body.onmousedown = function(){};
    document.body.onselectstart = function(){};
  }
  // ======= move obj
  // ------- Drag and drop
  if( activeEl && DRAGNDROP ){
    document.body.onmousedown = function(){ return false; };
    document.body.onselectstart = function(){ return false; };
    if( activeEl.mas ){
      if( check_where( activeEl, activeEl.mas ) ){
        activeEl.style.cursor = '';
      } else {
        activeEl.style.cursor = 'not-allowed';
      }
    }
    activeEl.style.top = ( tempY - dy ) + 'px';
    activeEl.style.left = ( tempX - dx ) + 'px';
    activeEl.PosTop = tempY - dy;
    activeEl.PosLeft = tempX - dx;
    if( activeEl.onMove ) activeEl.onMove();     
  } else {
    document.body.onmousedown = function(){};
    document.body.onselectstart = function(){};
  }
  // ======= Drag and drop
  return true;
}
