/**
 * @author RWelbers
 */
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 = 5 * 1000;	// After 6 seconds the scroll is killed.
	
	var running = false;
	
	/* Recursive scrolling method. Steps through the complete scroll. */

	function scrollStep(to, dest, left) {

		if(!running || (left && to >= dest) || (!left && to <= dest)) {
			TF.Scroller.killScroll();
			return;
		}

		if((left && to >= (dest - (2 * stepIncrement))) ||
		   (!left && to <= (dest - (2 * stepIncrement)))) {
			stepIncrement = stepIncrement * .55;
		}

		//window.scrollTo(to, 0);
		setTimeout('window.scrollTo('+to+', 0)',1);
		// Assign the returned function to a public method.
		
		TF.Scroller.nextStep = callNext(+to + stepIncrement, dest, left);
		window.setTimeout(TF.Scroller.nextStep, stepDelay);
	}

	/* Create a closure so that scrollStep can be accessed by window.setTimeout(). */

	function callNext(to, dest, left) {
		
		return function() { scrollStep(to, dest, left); };
	}

	return {
	
		nextStep: null,
		killTimeout: null,
	
		/* Sets up and calls scrollStep. */
	
		/* 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 xCoord = ((YAHOO.util.Dom.getX(target)) < 0) ? 0 : YAHOO.util.Dom.getX(target);
				
				var currentXPosition = (document.all) ? ((document.documentElement) ? document.documentElement.scrollLeft : document.body.scrollLeft) : window.pageXOffset;
				//alert(document.body.scrollLeft+'--'+document.documentElement.scrollLeft);
				var left = true;

				if(currentXPosition > xCoord) {
					stepIncrement *= -1;	// Reverse the direction if we are scrolling up.
					left = false;
				}

				// Stop the scroll once the time limit is reached.
				TF.Scroller.killTimeout = window.setTimeout(TF.Scroller.killScroll, limit);

				// Start the scroll by calling scrollStep().
				scrollStep(currentXPosition + stepIncrement, xCoord, left);	
			}
		},
		
		/* 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 'fetzmalos'. */

		init: function() {
			var links = YAHOO.util.Dom.getElementsByClassName('fetzmalos', 'a');
			YAHOO.util.Event.addListener(links, 'click', TF.Scroller.anchorScroll, TF.Scroller, true);
		}
	}

} ();

window.onload = function(){
	//YAHOO.util.Event.onAvailable('wrapper', TF.Scroller.init, TF.Scroller, true);
	TF.Scroller.init();
	//TF.Scroller();
}

