﻿/* derived from http://www.sitepoint.com/print/scroll-smoothly-javascript */

var ss_INTERVAL = null;
var ss_STEPS = 25;

function ss_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;
}

function smoothScroll( e )
{
	offsetTop = 0;
	
	if (isNaN(e))
		$(e).offsetTop - 12;		
	else
		offsetTop = e;
	
	destY = offsetTop - 12;
	if (destY < 0)
		destY = 0;
		
	// Stop any current scrolling
	clearInterval(ss_INTERVAL);
	currY = ss_getCurrentYPos();
	stepsize = parseInt((destY-currY)/ss_STEPS);
	ss_INTERVAL = setInterval('ss_scrollWindow('+stepsize+','+destY+')', 10);
}

function ss_scrollWindow(scramount,dest) {
	wascypos = ss_getCurrentYPos();
	window.scrollTo(0, parseInt(wascypos + (dest-wascypos)/10 + 4));
	//window.scrollTo(0,wascypos + scramount);
	
	currY = ss_getCurrentYPos();
	//window.alert('currY: ' + currY + '\ndest: ' + dest);
	isNear = currY > dest;
	
	if (isNear || currY == wascypos)
	{
		// cancel the repeating timer
		clearInterval(ss_INTERVAL);
	}
}
