// code for hand scrolling a page
// allan bonadio july 13, 2006

////////////////////////////////////////////////////////////////////////////// Trace
// set up a debug window so we can see what we're doing
var debugWin = null;

//  turn this true to use the debug window
if (false)
{
	if (!debugWin || debugWin.closed)
		debugWin= window.open("", "debug Win", 
			"scrollbars=yes,resizable=yes,width=600,height=500");
	debugWin.document.open("text/plain");
	debugWin.document.close();
	debugWin.document.writeln("--- Allanz Debug Window ---<p>");
}

// call this to write trace  messages.   firefox.
function traceD(msg)
{
	if (debugWin)
		debugWin.document.writeln(msg + "<br>");
}

// call at the end of a page so a reload will clear  the debug win
function closeD()
{
	if (debugWin)
		debugWin.document.close();
}


///////////////////////////////////////////////////////////////////////////////// scrolling
// true only during a drag, so the Move handler knows
var mouseIsDown = false;
var currentX, currentY;  // click coords
var previousVelocityX = 0, previousVelocityY = 0;
var velocityX = 0, velocityY = 0;
var latestScrollX = 0, latestScrollY = 0;
var timerID;

// ie  and safari use a global; firefox passes in the event, 
// and who knows what other browser sticks it where
function clarifyEvent(ev)
{
	return ev ? ev : event;
}

// called repeatedly to implement momentum scrolling
function autoScrollIt()
{
	if (latestScrollX != pageXOffset || latestScrollY != pageYOffset)
	{
		velocityX = velocityY = 0;
		clearInterval(timerID);
		timerID = null;
	}

	scrollBy(velocityX, velocityY);
	latestScrollX = pageXOffset;
	latestScrollY = pageYOffset;
	
	// kinetic friction
	velocityX -= velocityX / 100;
	velocityY -= velocityY / 100;
	
	// static friction
	if (Math.abs(velocityX) + Math.abs(velocityY) < 2)
	{
		velocityX = velocityY = 0;
		clearInterval(timerID);
		timerID = null;
	}
}


function scrollIt(ev)
{
	try
	{
		previousVelocityX = velocityX;
		previousVelocityY = velocityY;
		
		velocityX = currentX - ev.screenX;
		velocityY= currentY - ev.screenY;
		//traceD("velocity: "+ velocityX +", "+ velocityY);
		
		scrollBy(velocityX, velocityY);
		latestScrollX = pageXOffset;
		latestScrollY = pageYOffset;

		currentX = ev.screenX;
		currentY = ev.screenY;
		
		// stop pictures from beginning a d&d  operation.
		if (ev.preventDefault)
		{
			// ns/dom
			ev.preventDefault();
			ev.stopPropagation();
		}
		else if (ev.returnValue)
		{
			// ie
			ev.returnValue = false;
			ev.cancelBubble = false;
		}
	}
	catch (exc)
	{
		//traceD("Exception trying to scroll: "+ exc);
	}
}


//////////////////////////////////////////////////////////////////////// event handlers

function mouseDownHandler(ev)
{
	//traceD("DOWN event passed in is "+ ev);
	ev = clarifyEvent(ev);
	
	//traceD("mouse down: was " + (mouseIsDown ? "down" : "UP"));
	mouseIsDown = true;
	currentX = ev.screenX;
	currentY = ev.screenY;
	return false;
}

function mouseMoveHandler(ev)
{
	if (!mouseIsDown)
		return;

	//traceD("-------------------------------- MOVE");
	//var k;
	//for (k in ev)
	//	traceD("event."+ k +" = "+ ev[k]);
	//traceD("mouse Move: was " + (mouseIsDown ? "down" : "UP"));
	ev = clarifyEvent(ev);
	scrollIt(ev);
	return false;
}

function mouseUpHandler(ev)
{
	//traceD("mouse Up: was " + (mouseIsDown ? "down" : "UP"));
	ev = clarifyEvent(ev);
	mouseIsDown = false;
	
	if (velocityX || velocityY)
	{
		// problem where the very last move is in a different direction, very strange
		if (velocityX * previousVelocityX < 0 || velocityY * previousVelocityY < 0)
		{
			velocityX = previousVelocityX;
			velocityY = previousVelocityY;
		}
		timerID = setInterval("autoScrollIt()",  200);
		//traceD("started autoscroll, velocity "+ velocityX +", "+ velocityY);
	}
	return false;
}

// no, mouse down/up handlers are not always called in order.   
// but an Out event tells you there won't be a mouse up.  
function mouseOutHandler(ev)
{
	mouseIsDown = false;
}


function unloadHandler(ev)
{
	closeD();
	mouseIsDown = false;
}

function installHandlers()
{
	onunload = unloadHandler;
	
	var activeArea = document.getElementById("ActiveArea");
	activeArea.onmousedown = mouseDownHandler; 
	activeArea.onmousemove = mouseMoveHandler; 
	activeArea.onmouseup = mouseUpHandler; 
	activeArea.onmouseout = mouseOutHandler; 
}


