/*--------------------------------------------------------------------------
 *  Techniques stolen from: 
 *    Smooth Scroller Script, version 1.0.1 (c) 2007 Dezinerfolio Inc. <midart@gmail.com>
 *
 *  Totally rewritten fixing a lot of errors
 *
/*--------------------------------------------------------------------------*/
SoftScroll = {
	// defaults
	speed:10,
	offset:-15,

	gety: function(tag) {
		// returns the Y position of tag

		for (y = 0; tag; y += tag.offsetTop, tag = tag.offsetParent)
			;
		return (y + SoftScroll.offset);
	},
	scrollTop: function() {
		// returns the current scroll position

		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;
	},

	attach: function(elem, evnt, func) {
		// attach an event for element

		if (elem.addEventListener) return elem.addEventListener(evnt, func, false);
		if (elem.attachEvent) return elem.attachEvent('on'+evnt, func);
	},

	capture: function(evnt) {
		// capture the event and stop propagation

		if (window.event) {
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		} else if (evnt.preventDefault && evnt.stopPropagation) {
			evnt.preventDefault();
			evnt.stopPropagation();
		}
	},
	
	scroll: function(dest) {
		// move the scroll bar to a div

		try {
			winheight = window.innerHeight || document.documentElement.clientHeight;
			docheight = document.body.scrollHeight;
			y = SoftScroll.scrollTop();
			if (SoftScroll.lasty != undefined && SoftScroll.lasty != y) {
				// Someone touched the scrolling from last round trip, pop to destination and stop scrolling
				y = dest;
			} else {
				y += (Math.min(dest, docheight - winheight) - y) / SoftScroll.speed;
				y = (dest > y ? Math.ceil(y) : Math.floor(y));
			}
	
			window.scrollTo(0, y);
			if (y == dest || SoftScroll.lasty == y) clearInterval(SoftScroll.interval);
			SoftScroll.lasty = y;
		} catch(err) {
			clearInterval(SoftScroll.interval);
		}
	},

	start: function(evnt) {
		// Start scrolling

		try {
			var e = (evnt.srcElement) ? evnt.srcElement : this;
			while (e.tagName != "A") { e = e.parentNode; }
			d = e.hash.substr(1);
			if (d != "") {
				a = document.getElementsByTagName('a');
				for (i = 0; i < a.length; i++) {
					if(a[i].name == d){
						clearInterval(SoftScroll.interval);
						SoftScroll.lasty = undefined;
						SoftScroll.capture(evnt);
						SoftScroll.interval = setInterval('SoftScroll.scroll(' + SoftScroll.gety(a[i]) + ')', 10);
					}
				}
			}
		} catch(err) { 
        	}
	},

	init: function() {
		// initializer that adds the hook to the onload function

		SoftScroll.attach(window, 'load', SoftScroll.hook)
	},

	
	hook: function() {
		// this method add events to all # anchors

		a = document.getElementsByTagName('a');
		for (i = 0; i < a.length; i++) {
			l = a[i]; 
			if(l.href && l.href.indexOf('#') != -1 && ((l.pathname == location.pathname) || ('/' + l.pathname == location.pathname))) {
				SoftScroll.attach(l, 'click', SoftScroll.start);
			}
		}
	}
}

SoftScroll.init();
SoftScroll.speed = 6;
