// Smooth Scrolling (based on: http://www.itnewb.com/v/Creating-the-Smooth-Scroll-Effect-with-JavaScript)
smoothScroll = {
	duration: 1500,
	init: function (duration) {
		this.duration = duration ? duration : this.duration;
		a = document.getElementsByTagName("a");
		for (i = 0; i < a.length; i++) {
			l = a[i];
			d = location.pathname;
			if (l.href && l.href.indexOf("#") != -1 && (l.pathname == d || "/" + l.pathname == d)) {
				var goto = l.hash.substring(1);
				l.onclick = this.scroll;
			}
		}
	},
	scroll: function () {
		// find our start position
		var startY = 0;
		if (self.pageYOffset) startY = self.pageYOffset; // Firefox, Chrome, Opera, Safari
		if (document.documentElement && document.documentElement.scrollTop) startY = document.documentElement.scrollTop; // Internet Explorer 6 - standards mode
		if (document.body.scrollTop) startY = document.body.scrollTop; // Internet Explorer 6, 7 and 8

		// get our go-to position
		eID = this.hash.substring(1);
		var e = document.getElementById(eID);
		var stopY = e.offsetTop;
		while (e.offsetParent && e.offsetParent != document.body) {
			e = e.offsetParent;
			stopY += e.offsetTop;
		}

		// build the geometric progression (start fast, end slow)
		if (stopY < startY) {
			stopY = stopY == 0 ? stopY : stopY - 40;
		}
		var distance = stopY > startY ? stopY - startY : startY - stopY;
		var gate = new Array();
		gate[0] = 1;
		for (i = 1; i < 150; i++) {
			gate[i] = (gate[i-1] * 1.05);
		}
		for (var a in gate) {
			gate[a] = Math.round(gate[a] * (distance / gate[149]) - (distance / gate[149]));
		}
		gate[149] = distance;
		gate.reverse();

		// perform the actual scrolling
		var leapY = startY;
		for (i = 0; i < 148; i++) {
			leapY = (stopY > startY) ? leapY + (gate[i] - gate[i+1]) : leapY - (gate[i] - gate[i+1]);
			setTimeout("window.scrollTo(0, "+leapY+")", i * (smoothScroll.duration / 150));
		}

		// prevent click event
		return false;
	}
};
