﻿/* -------------------------------------------------------------------------- */
/*  Set up the TF namespace.                                                  */
/* -------------------------------------------------------------------------- */



/* -------------------------------------------------------------------------- */
/*  Functions to initialize and perform the scrolling anchor links.			  */
/*	taken from : http://techfoolery.com/archives/2006/08/11/2021/			  */
/* -------------------------------------------------------------------------- */

CT.Scroller = function() {

	var stepIncrement = 15; // The number of pixels that each step moves the window.
	var stepDelay = 15; // 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)) {
			CT.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.

		CT.Scroller.nextStep = callNext(+to + stepIncrement, dest, down);

		window.setTimeout(CT.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 {
		scrollComplete: new YAHOO.util.CustomEvent("scrollComplete", this),
		nextStep: null,
		killTimeout: null,

		/* Sets up and calls scrollStep. */

		anchorScroll: function(e, obj) {
			var clickedLink = YAHOO.util.Event.getTarget(e);
			if (clickedLink.tagName != "A") {
				clickedLink = YAHOO.util.Dom.get("OfferScrollingLink");
			}
			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.

				CT.Scroller.killTimeout = window.setTimeout(CT.Scroller.killScroll, limit);

				scrollStep(currentYPosition + stepIncrement, yCoord, down);
			}
		},

		/* Kill the scroll after a timeout, to prevent an endless loop. */

		killScroll: function() {
			window.clearTimeout(CT.Scroller.killTimeout);
			running = false;
			stepIncrement = 50;
			this.scrollComplete.fire();
		},

		/* 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', CT.Scroller.anchorScroll, CT.Scroller, true);
		}
	};

} ();

YAHOO.util.Event.onAvailable('doc', CT.Scroller.init, CT.Scroller, true);
YAHOO.util.Event.onAvailable('doc', function() {
	CT.Scroller.scrollComplete.subscribe(onScrollComplete, this);
});


