var thresh=40;
var max_moves=2;
var height=640;
var width;
var veto=false;
var ttlcount=0;
var child,opener;
var calls=new Array();
calls['left']='history.go(-1)';
calls['left->up']='if (window.home) window.home()';
calls['right']='history.go(1)';
calls['up->down']='window.location.reload()';
calls['up']='window.scrollBy(0,-'+height+')';
calls['up->left']='prev()';
calls['up->right']='next()';
calls['down']='window.scrollBy(0,'+height+')';
calls['down->up']='maximize()';
calls['down->right']='myclose()';
calls['down->left']='childclose()';
var lx,ly;
var move_cnt=0;
var moves=new Array();
var credits=false;
var aboutpage='http://www.bitesizeinc.net/index.php/gesture.html';
var only_right=false;
function childclose() { if (child) { child.close(); child=false; } }

function myclose() {  
if (opener) { 
 if (opener.child) { opener.child=false; }
}
if (child) {
 if (child.opener) { child.opener=false; }
}
close();
}
function next() {
if (!child) {
 child=window.open(location.href);
 if (!child.opener) child.opener = self;
}
child.focus();
}

function prev() { 
  if (opener) { opener.focus(); }
  else { opener=window.open(location.href); }
}
function maximize() {
if (window.screen) {
    window.moveTo(0,0);
    window.resizeTo(screen.availWidth,screen.availHeight)
}
}
function mousemove(evt) {
evt = (evt) ? evt : ((window.event) ? window.event : "")
x=evt.clientX;
y=evt.clientY;
move=false;
if (Math.abs(lx-x)>thresh) {
 if (lx>x) { move='left'; } else { move='right'; }
} else if (Math.abs(ly-y)>thresh) {
  if (ly>y) { move='up'; } else { move='down'; }
}
if (move) {
 if (move_cnt==0) { moves[move_cnt++]=move; } else {
  if (move!=moves[move_cnt-1])
    {
      moves[move_cnt++]=move;
      if (move_cnt>max_moves) { move_cnt=0; }
    }
 }
 lx=x;
 ly=y;
}
status='mouseGestures recognized : ';
for(i=0;i<move_cnt;i++)
 {
  if (i>0){ status+='->'; }
  status+=moves[i];
 }
return true;
}
function mousedown(evt)
{
evt = (evt) ? evt : ((window.event) ? window.event : "")
if (evt) 
{
 if (only_right) {
  var clickType=1;
  if (navigator.appName=="Netscape") clickType=evt.which;
  else clickType=evt.button;
  if (clickType==1) { return true; }
 }
}
if (veto) { veto=false; return false; }
lx=evt.clientX;
ly=evt.clientY;
document.onmousemove = mousemove;
status='mouseGestures listening';
return true;
}
function mouseup(evt) {
evt = (evt) ? evt : ((window.event) ? window.event : "")
veto=false;
document.onmousemove = null;
match_str='';
if (document.all)
 { height = document.body.offsetHeight; width = document.body.offsetWidth; }
else if (document.layers)
 { height = window.innerHeight; width = window.innerWidth; }
if (move_cnt>0) {
 for(i=0;i<move_cnt;i++) {
  if (i>0) { match_str+='->'; }
  match_str+=moves[i];
 }
 if (calls[match_str]) {
  eval(calls[match_str]);
  status='mouseGesture complete';
  ttlcount++;
  if ((!credits)&&(ttlcount>10)) {
   ttlcount=-100;
   window.open(aboutpage);
  }
 } else { status='mouseGesture not recognized'; }

//THOR} else { status='MouseGesture Cancelled'; }
} else { status=defaultStatus; }

move_cnt=0;
return true;
}

function context(evt) {
evt = (evt) ? evt : ((window.event) ? window.event : "")
if (evt) {
 var elem
 if (evt.target) { elem = evt.target } else { elem = evt.srcElement }
 if (elem) {
  if ((elem.tagName=="IMG")||(elem.tagName=="A")||(elem.tagName=="INPUT")||(elem.tagName=="TEXTAREA")) {
    document.onmousemove=null;
    
    //status="MouseGesture Cancelled";
    status=defaultStatus;
    
    move_cnt=0;
    veto=true;
    return true;
  }		
 }
}
return false;
}
function mouseGestures() { document.write('<div id=mouseGestures style="	float: right;  border: 1px solid; border-color: #dddddd #666666 #666666 #dddddd;"><center><font size=-2><a id=mgAnchor href="'+aboutpage+'">MouseGestures Enabled</a></font></center></div>'); credits=true; }
function add_text(id,text) { if (!document.getElementById) { document.all[id].innerHTML += text; } else { var obj = document.getElementById(id); obj.innerHTML+=text; } }
function find_by_id(id) { if (!document.getElementById) { return document.images[id] } else { return document.getElementById(id); } }
function disableLeft() { only_right=true; add_text("mgAnchor","&nbsp;-&times;"); }

credits=false;
only_right=true;
document.onmousedown=mousedown;
document.onmouseup=mouseup;
document.oncontextmenu = context;

