
/************************************************************************
 *																		*
 *		gotoAnchor.js													*
 *																		*
 *			Scrolls page to anchor name. Fast/far, Slow/close.			*
 *			To use, simply call gotoAnchor, and specify anchor name.	*
 *																		*
 *		Special credits to:												*
 *	http://www.kryogenix.org/code/browser/smoothscroll/smoothscroll.js	*
 *																		*
 *																		*
 *		Copyright © 2009 by Josef Guenther								*
 *			All rights reserved.										*
 *																		*
 *			Contact me at:												*
 *			josefguenther@gmail.com										*
 *																		*
 *																		*
 ************************************************************************/


// Variables
var ga_INTERVAL;
var ga_stepsize;
var ga_init;
var ga_dir;

// -----------------------------------------------------------------------------------

//
// gotoAnchor - Scrolls page to anchor specified
//
function gotoAnchor (aName, jump)
{
	var i, lnk;
	
	// Find the <a name> tag corresponding to the name given
	var allLinks = document.getElementsByTagName('a');
	var destinationLink = null;
	for (i = 0; i < allLinks.length; i++) {
		lnk = allLinks[i];
		if (lnk.name && lnk.name == aName) {
			destinationLink = lnk;
			break;
		}
	}
	
	// If we didn't find a destination, give up
	if (!destinationLink) {
		return;
	}
	
	// Find the destination's position
	var destx = destinationLink.offsetLeft; 
	var desty = destinationLink.offsetTop;
	var thisNode = destinationLink;
	while (thisNode.offsetParent && (thisNode.offsetParent != document.body)) {
		thisNode = thisNode.offsetParent;
		destx += thisNode.offsetLeft;
		desty += thisNode.offsetTop;
	}
	
	// Stop any current scrolling
	clearInterval(ga_INTERVAL);
	
	// initial speed up
	ga_init = true;
	ga_stepsize = 1;
	
	if (ga_getCurrentYPos() < desty) {
		ga_dir = 1;
	} else {
		ga_dir = -1;
	}
	
	
	// jump, or scroll?
	if (jump) {
		window.scrollTo(0, desty);
	} else {
		ga_INTERVAL = setInterval('ga_scrollWindow(' + desty + ',"' + aName + '")', 10);
	}
}

//
// ga_scrollWindow - The function to actually scroll towards destination
//
function ga_scrollWindow (dest, aName)
{
	var neededSpeed, isAbove;
	
	// get current scroll
	var cypos = ga_getCurrentYPos();
	
	// adjust step size - faster for farther, slower for closer
	if (ga_dir > 0) {
		neededSpeed = ((dest - cypos) + 10) / 11;
	} else {
		neededSpeed = ((cypos - dest) + 10) / 11;
	}
	
	// speed up
	if (ga_init) {
		ga_stepsize += 1.5;
		if (ga_stepsize >= neededSpeed) {
			ga_init = false;
		}
	// slow down
	} else {
		ga_stepsize = neededSpeed;
		// make sure it doesn't get too slow
		if (ga_stepsize < 1) {
			ga_stepsize = 1;
		}
	}
	
	// see if we're above destination
	isAbove = (cypos < dest);
	
	// scroll
	window.scrollTo(0, cypos + (ga_dir * ga_stepsize));
	
	// now see if we're still above destination
	var cypos_after = ga_getCurrentYPos();
	var isAboveNow = (cypos_after < dest);
	
	// if we've just scrolled past the destination, or at top/bottom of page
	if ((isAbove != isAboveNow) || (cypos == cypos_after)) {
		// scroll to destination, and cancel the repeating timer
		window.scrollTo(0, dest);
		clearInterval(ga_INTERVAL);
	}
}

//
// ga_getCurrentYPos - Returns the current y scroll of page
//
function ga_getCurrentYPos ()
{
	if (document.body && document.body.scrollTop) {
		return document.body.scrollTop;
	}
	if (document.documentelement && document.documentelement.scrollTop) {
		return document.documentelement.scrollTop;
	}
	if (window.pageYOffset) {
		return window.pageYOffset;
	}
	return 0;
}

