//Scroll Element Script
//previous version 011108
//current version 010509	
//copyright © 2009 Hal Barwood & Finite Arts

//..............................................................................

//globals...
pageScroll = new Array(2);
floaterName = null;
floatOffset = null;
floatY = null;
floatInterval = null;

//global constants...
YMINDIFF = 0;  //minimum difference in vertical position between page & floater needed to trigger scroll
FLOATFACTOR = 0.2;  //how much of the difference in position we want to reduce in each interval
FLOATINTERVAL = 33; //update ~30 times a second

//..............................................................................

//startup...
window.onUnload = clearInterval(floatInterval);


function testAlert()
{
	window.alert("started");
}


function startScroll(fName, yOffset)
{
	//set up floating element...
	floaterName = fName;
	floatOffset = yOffset;
	pageScroll = getScrollXY();
	document.getElementById(floaterName).style.top = floatOffset + "px";
	floatInterval = setInterval("scrollNow()", FLOATINTERVAL);
}


function scrollNow()
{
	pageScroll = getScrollXY();
	floatY = parseInt(document.getElementById(floaterName).style.top);
	var topDiff = floatY - (pageScroll[0] + floatOffset);
	var moveDist = topDiff * FLOATFACTOR;

	if (topDiff < -YMINDIFF)
	{
		//too high on page, move down a little bit...
		floatY -= moveDist;
		document.getElementById(floaterName).style.top = floatY + "px";
	}

	else if (topDiff > YMINDIFF)
	{
		//too low, move up a little bit...
		floatY -= moveDist;
		document.getElementById(floaterName).style.top = floatY + "px";
	}
}


function getScrollXY()
{
	var sTop = 0;
	var sLeft = 0;
	if (typeof(window.pageYOffset) == 'number')
	{
		//NS compliant...
		sTop = window.pageYOffset;
		sLeft = window.pageXOffset;
	}
	else if (document.body && (document.body.scrollLeft || document.body.scrollTop))
	{
		//DOM compliant...
		sTop = document.body.scrollTop;
		sLeft = document.body.scrollLeft;
	}
	else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
	{
		//IE6 standards compliant...
		sTop = document.documentElement.scrollTop;
		sLeft = document.documentElement.scrollLeft;
	}

	return [sTop, sLeft];
}


