// $Id: drag.js,v 1.1.1.1 2005/11/04 09:07:03 khlut Exp $
// Scrolling by dragging
// Usage:
// add to srollable tag this parameter:
//    onmousedown=dragBegin(window.document.body)
// function argument is scrollable object
// For reverse scrolling call to dragReverse(1)

// Copyright 2005 Elphel, Inc.
//
// This file is part of GenReS.
//
//    GenReS is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    GenReS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with GenReS; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


var dBx,dBy, dOb;

function show(e) {
  var t="";
  t+=e;
  for(x in e) {
     t+=",<br>\n";
     t+=x;
     if(e[x])  t+="="+e[x];
     else t+="---";
  }
  return  "("+t+")" ;
}
function debug(e) {
  w=window.open();
  w.document.write(show(e));
  w.document.close();
}

function dragBegin(t) {
  dOb=t;
}

function dragReverse(rev) {
 if(rev)
  scroll=function (dx,dy) {
    dBx+=dx;
    dBy+=dy;
    dOb.scrollLeft+=dx>>1;
    dOb.scrollTop+=dy>>1;    
  }
 else{
  scroll=function (dx,dy) {
    dBx+=dx;
    dBy+=dy;
    dOb.scrollLeft-=dx>>1;
    dOb.scrollTop-=dy>>1;    
  }
 }
}

dragReverse(0);

document.onmousedown = function (e)
{
  if(dOb)
    if(e.pageX) {
        dBx=e.screenX;
	dBy=e.screenY;
	return false;
    }else {
	debug(e);
    }
  else return true;
}

document.onmousemove = function (e)
{
  if(dOb) {
    if(e.pageX) scroll(e.screenX-dBx,e.screenY-dBy);
  }
  return true;
}

document.onmouseup = function (e)
{
  if(dOb) {
    scroll(e.screenX-dBx,e.screenY-dBy)
    dOb=null;
//  debug(document.getElementById("dvf").run);
  }
  return true;
}
