/* -------------------------------------------------------------------------- */
/*  Set up the TF namespace.                                                  */
/* -------------------------------------------------------------------------- */

var TF = window.TF || {};
	
/* -------------------------------------------------------------------------- */
/*  Functions to initialize and perform the scrolling anchor links.           */
/* -------------------------------------------------------------------------- */

TF.Scroller = function () {

	var stepIncrement = 50;	// The number of pixels that each step moves the window.
	var stepDelay = 10;	// The number of milliseconds between steps.
	var limit = 6 * 1000;	// After 6 seconds the scroll is killed.
	
	var running = false;
	
	/* Recursive scrolling method. Steps through the complete scroll. */
		
	function scrollStep(to, dest, down) {
	
		if(!running || (down && to >= dest) || (!down && to <= dest)) {
			TF.Scroller.killScroll();
			return;
		}

		if((down && to >= (dest - (2 * stepIncrement))) ||
		   (!down && to <= (dest - (2 * stepIncrement)))) {
			stepIncrement = stepIncrement * .55;
		}

		window.scrollTo(0, to);
		
		// Assign the returned function to a public method.
		
		TF.Scroller.nextStep = callNext(+to + stepIncrement, dest, down);

		window.setTimeout(TF.Scroller.nextStep, stepDelay);
	}
	
	/* Create a closure so that scrollStep can be accessed by window.setTimeout(). */
	
	function callNext(to, dest, down) {
	
		return function() { scrollStep(to, dest, down); };
	}

	return {
	
		nextStep: null,
		killTimeout: null,
	
		/* Sets up and calls scrollStep. */
	
		anchorScroll: function(e, obj) {

			var clickedLink = YAHOO.util.Event.getTarget(e);
			var anchorId = clickedLink.href.replace(/^.*#/, '');
			var target = YAHOO.util.Dom.get(anchorId);
			
			if(target) {
			
				YAHOO.util.Event.stopEvent(e);
				running = true;
				
				var yCoord = ((YAHOO.util.Dom.getY(target) - 6) < 0) ? 0 : YAHOO.util.Dom.getY(target) - 6;
				var currentYPosition = (document.all) ? document.body.scrollTop : window.pageYOffset;
				var down = true;
	
				if(currentYPosition > yCoord) {
					stepIncrement *= -1;
					down = false;
				}
	
				// Stop the scroll once the time limit is reached.
	
				TF.Scroller.killTimeout = window.setTimeout(TF.Scroller.killScroll, limit);
	
				scrollStep(currentYPosition + stepIncrement, yCoord, down);	
			}
		},
		
		/* Kill the scroll after a timeout, to prevent an endless loop. */
		
		killScroll: function() {
			window.clearTimeout(TF.Scroller.killTimeout);
			running = false;
			stepIncrement = 50;			
		},
	
		/* Attach the scrolling method to the links with the class 'scrolling-link'. */

		init: function() {
		
			var links = YAHOO.util.Dom.getElementsByClassName('scrolling-link', 'a');
			YAHOO.util.Event.addListener(links, 'click', TF.Scroller.anchorScroll, TF.Scroller, true);
		}
	}

} ();

YAHOO.util.Event.onAvailable('doc', TF.Scroller.init, TF.Scroller, true);