/**
 * compression notes: (using Angus Turnbull's Codetrim utility)
 * browser->b 
 * cont->c
 * delta->d 
 * debugDiv->dD
 * event->e 
 * maxPixelRate->mPR 
 * pixelRate->pR
 * rate->r
 * stopScroll->sS 
 * thisDirection->tD
 * thread->th
 * time->t
 * timeoutDelay->tD 
 */

//====================
// horizontal scrolling globals
//====================

var pixelRate = 0;
var browser=navigator.appName;
var stopScroll = false;
var timeoutDelay = 25;
var debugDiv;
var thread = 0;

//====================
// scroll by arrows
//====================

/**
 * scroll the window
 * @param {Integer} movement 0=still, 1=move to right, -1=move to left
 * @author Thomas Deng (original code)
 * @author Mike Smith (completely rewritten)
 */
function scrollWindow(thisDirection)
{
	debugDiv = document.getElementById('debugDiv');
	stopScroll = false;
	pixelRate = 0;
	scrollWindowR(0, 0.1, thisDirection, ++thread);
}

/**
 * function that does all the work for scrolling
 * 'S' stands for 'Single' call (vs. recursive)
 * @param {Integer} pxr pixel rate increase passed by controller function that determines slope of speed change
 * @author Mike Smith
 */
function scrollWindowS(pxr, thisDirection)
{
	var maxPixelRate = 7;
	// For some reason Firefox moves faster, thus the sniffing.
	if (browser == "Netscape") {
		maxPixelRate = 4;
	}
	if (thisDirection == 0) {
		return false;
	}
	else {
		if (stopScroll) {
			pixelRate -= Math.sqrt(pixelRate) / 2;
			if (pixelRate <= 0) {
				thisDirection = 0;
				pixelRate = 0;
			}
		} else {
			pixelRate = pxr * maxPixelRate;
		}
		var px = Math.round(maxPixelRate * pxr * thisDirection);
//		debugDiv.innerHTML = 'px=' + px + ', pixelRate=' + pixelRate + ' (pxr=' + pxr + ')' + '| thread=' + thread;
		window.scrollBy(px, 0);
	}
	return true;
}

/**
 * Recursive scrolling function that will scroll forever until the single-call function detects a stop flag
 * @author Mike Smith
 */
function scrollWindowR(thisTime, thisRate, thisDirection, thisThread)
{
	// only allow one thread to scroll at a time, 
	if (thisThread == thread) {
		var cont = scrollWindowS(smoothRamp(thisTime), thisDirection);
		if (cont) {
			setTimeout(	function(){ scrollWindowR(thisTime + thisRate, thisRate, thisDirection, thisThread); }, timeoutDelay);
		}		
	}
}

/**
 * signal the scrolling to stop (allows the recursive function to "ramp-down" the speed)
 * @author Thomas Deng
 * @author Mike Smith
 */
function stop()
{
	stopScroll = true;
}

//====================
// scroll by mouse wheel
//====================

/**
 * description???
 * @param {Integer} delta
 * @author Thomas Deng
 */
function handle(delta) 
{
	if (delta > 0) {
		window.scrollBy(-80, 0);
	}
	else {
		window.scrollBy(80, 0);
	}
}

/**
 * description???
 * @param {Event} event
 * @author Thomas Deng
 */
function wheel(event)
{
	var delta = 0;
	if (!event) {
		event = window.event;
	}
	if (event.wheelDelta) {
		delta = event.wheelDelta / 120;
		if (window.opera) {
			delta = -delta;
		}
	}
	else {
		if (event.detail) {
			delta = -event.detail / 3;
		}
	}
	if (delta) {
		handle(delta);
	}
    if (event.preventDefault) {
		event.preventDefault();
	}
    event.returnValue = false;
}

// mouse wheel initialization code 
if (window.addEventListener) {
	window.addEventListener('DOMMouseScroll', wheel, false);
}
window.onmousewheel = document.onmousewheel = wheel;


