// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// - - - - - - ( smooth scrolling between internal links ) - - - - - - - - - - - 
// http://www.sitepoint.com/article/scroll-smoothly-javascript
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// set vars
var ss_INTERVAL;
var ss_STEPS = 5;

function ss_fixAllLinks() {
	$('a[@href*=#]').click(smoothScroll);
	$('.blind > a:first-child').unbind('click', smoothScroll);
//	$('.info a[@href*=#]').click(smoothScroll);
//	$('a[@href$=#top]').unbind('click', smoothScroll).click(smoothScrollTop);
}

function smoothScrollTop() {
	// clear the interval timer (why here?)
	clearInterval(ss_INTERVAL);
	
	// get current y-position
	var cypos = ss_getCurrentYPos();
	
	// calculate the step sizes
	var ss_stepsize = parseInt((0-cypos)/ss_STEPS);
	
	// run the scroll
	ss_INTERVAL = setInterval('ss_scrollWindow('+ss_stepsize+',0,"")',10);
	
	// stop the browser handling the event as normal and opening the link
	// kills the bubble
	return false;
	if (window.event)
		{
		window.event.cancelBubble = true;
		window.event.returnValue = false;
		}
	if (e && e.preventDefault && e.stopPropagation)
		{
		// hmmm, safari gets inside here, but then fails to stop the event, and so a 'jump' rather than a 'scroll' takes place. I can't figure it out...
		e.preventDefault();
		e.stopPropagation();
		}
	return true;	
}

function smoothScroll(e) {
	var target = this;

	// strip off the hash
	var anchor = target.hash.substr(1);
	// grab all the anchors on the page
	var destinationLink = document.getElementById(anchor);
	// if none found, exit
	if (!destinationLink) return true;

	// find position of destination link
	var destx = destinationLink.offsetLeft; 
	var desty = destinationLink.offsetTop;
	var thisNode = destinationLink;
	// loop up through offsetParent until we hit the document body, as IE requires
	while (thisNode.offsetParent && (thisNode.offsetParent != document.body))
		{
		thisNode = thisNode.offsetParent;
		destx += thisNode.offsetLeft;
		desty += thisNode.offsetTop;
		}
	
	// clear the interval timer (why here?)
	clearInterval(ss_INTERVAL);
	
	// get current y-position
	var cypos = ss_getCurrentYPos();
	
	// calculate the step sizes
	var ss_stepsize = parseInt((desty-cypos)/ss_STEPS);
	
	// run the scroll
	ss_INTERVAL = setInterval('ss_scrollWindow('+ss_stepsize+','+desty+',"'+anchor+'")',10);
	
	// stop the browser handling the event as normal and opening the link
	// kills the bubble
	return false;
	if (window.event)
		{
		window.event.cancelBubble = true;
		window.event.returnValue = false;
		}
	if (e && e.preventDefault && e.stopPropagation)
		{
		// hmmm, safari gets inside here, but then fails to stop the event, and so a 'jump' rather than a 'scroll' takes place. I can't figure it out...
		e.preventDefault();
		e.stopPropagation();
		}
	return true;
	}

function ss_scrollWindow(scramount, dest, anchor) {
	var wascypos = ss_getCurrentYPos();
	var isAbove = (wascypos < dest);
	window.scrollTo(0,wascypos + scramount);
	var iscypos = ss_getCurrentYPos();
	var isAboveNow = (iscypos < dest);
	// if finished cancel timer and jump anchor so url is correct
	if ((isAbove != isAboveNow) || (wascypos == iscypos)) {
		window.scrollTo(0,dest);
		clearInterval(ss_INTERVAL);
//		location.hash = anchor;
	}
}

function ss_getCurrentYPos() {
	// ie5 and ie5.5
	if (document.body && document.body.scrollTop)
	return document.body.scrollTop;
	// ie6
	if (document.documentElement && document.documentElement.scrollTop)
	return document.documentElement.scrollTop;
	// netscape etc
	if (window.pageYOffset)
	return window.pageYOffset;
	return 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

$(document).ready(ss_fixAllLinks);
