var ss = {
	fixAllLinks: function () {
		var a = document.getElementsByTagName('a');
		for ( var i = 0, n = a.length, b; i < n; ++i ) {
			var b = a[i];
			if ( ( b.href && b.href.indexOf('#') !== -1 ) && ( ( b.pathname == location.pathname ) || ( '/' + b.pathname == location.pathname ) ) && ( b.search == location.search ) && !b.onclick ) {
				ss.addEvent( b, 'click', ss.smoothScroll );
			}
		}
		var a = document.getElementsByTagName('area');
		for ( var i = 0, n = a.length, b; i < n; ++i ) {
			var b = a[i];
			if ( ( b.href && b.href.indexOf('#') !== -1 ) && ( ( b.pathname == location.pathname ) || ( '/' + b.pathname == location.pathname ) ) && ( b.search == location.search ) && !b.onclick ) {
				ss.addEvent( b, 'click', ss.smoothScroll );
			}
		}
	},
	smoothScroll: function (e) {
		if ( window.event ) {
			target = window.event.srcElement;
		} else if (e) {
			target = e.target;
		} else return;
		if ( target.nodeName.toLowerCase() != 'a' ) {
			target = target.parentNode;
		}
		if ( target.nodeName.toLowerCase() != 'a' ) return;
		var anchor = target.hash.substr(1);
		var allLinks = document.getElementsByTagName('a');
		var destinationLink = null;
		for ( var i = 0, a = document.getElementsByTagName('a'), n = a.length, b; i < n; ++i ) {
			var b = a[i];
			if ( b.name && ( b.name == anchor ) ) {
				destinationLink = b;
				break;
			}
		}
		if ( !destinationLink ) return true;
		ss.runScroll( destinationLink, anchor );
		if ( window.event ) {
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if ( e && e.preventDefault && e.stopPropagation ) {
			e.preventDefault();
			e.stopPropagation();
		}
	},
	runScroll: function ( destinationLink, anchor ) {
		var destx = destinationLink.offsetLeft, desty = destinationLink.offsetTop, thisNode = destinationLink;
		while ( thisNode.offsetParent && ( thisNode.offsetParent != document.body ) ) {
			thisNode = thisNode.offsetParent;
			destx += thisNode.offsetLeft;
			desty += thisNode.offsetTop;
		}
		clearInterval( ss.INTERVAL );
		cypos = ss.getCurrentYPos();
		ss_stepsize = parseInt( ( desty - cypos ) / ss.STEPS );
		ss.INTERVAL = setInterval( 'ss.scrollWindow(' + ss_stepsize + ',' + desty + ',"' + anchor + '")', 10 );
	},
	scrollWindow: function ( scramount, dest, anchor ) {
		wascypos = ss.getCurrentYPos();
		isAbove = ( wascypos < dest );
		window.scrollTo( 0, wascypos + scramount );
		iscypos = ss.getCurrentYPos();
		isAboveNow = ( iscypos < dest );
		if ( ( isAbove != isAboveNow ) || ( wascypos == iscypos ) ) {
			window.scrollTo( 0, dest );
			clearInterval( ss.INTERVAL );
			location.hash = anchor;
		}
	},
	getCurrentYPos: function () {
		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;
	},
	addEvent: function ( elm, evType, fn, useCapture ) {
		if ( elm.addEventListener ) {
			elm.addEventListener( evType, fn, useCapture );
			return true;
		} else if ( elm.attachEvent ) {
			var r = elm.attachEvent( "on" + evType, fn );
			return r;
		} else {
			alert("Handler could not be removed");
		}
	}
}
ss.STEPS = 25;
ss.addEvent( window, "load", ss.fixAllLinks );
