Scroller = {

	speed:10,

	// returns the Y position of the div
	gy: function (d) {
		gy = d.offsetTop
		if (d.offsetParent) while (d = d.offsetParent) gy += d.offsetTop
		return gy
	},

	// returns the current scroll position
	scrollTop: function (){
		body=document.body
		d=document.documentElement
		if (body && body.scrollTop) return body.scrollTop
		if (d && d.scrollTop) return d.scrollTop
		if (window.pageYOffset) return window.pageYOffset
		return 0
	},

	// attach an event for an element
	// (element, type, function)
	add: function(event, body, d) {
				if (event.addEventListener) return event.addEventListener(body, d,false)
				if (event.attachEvent) return event.attachEvent('on'+body, d)
	},

	// kill an event of an element
	end: function(e){
		if (window.event) {
			window.event.cancelBubble = true
			window.event.returnValue = false
			return;
		}
		if (e.preventDefault && e.stopPropagation) {
			e.preventDefault()
			e.stopPropagation()
		}
	},
	
	// move the scroll bar to the particular div.
	scroll: function(d){
		// if (document.getElementById('yMenu')) Scroller.speed = 5;
		i = window.innerHeight || document.documentElement.clientHeight;
		h=document.body.scrollHeight;
		a = Scroller.scrollTop()
		if(d>a)
			if(h-d>i)
				a+=Math.ceil((d-a)/Scroller.speed)
			else
				a+=Math.ceil((d-a-(h-d))/Scroller.speed)
		else
			a = a+(d-a)/Scroller.speed;
		window.scrollTo(0,a)
	  	if(a==d || Scroller.offsetTop==a) {
				// ↓ガタツキ防止（END）
				var root = document.getElementsByTagName("body")[0] || document.body;
				root.style.backgroundAttachment = "fixed";
				if (document.getElementById('yMenu')) document.getElementById('yMenu').style.display = "";
				// ↑ガタツキ防止（END）
				clearInterval(Scroller.interval)
			}
	  	Scroller.offsetTop=a
	},
	// initializer that adds the renderer to the onload function of the window
	init: function(){
		Scroller.add(window,'load', Scroller.render)
	},

	// this method extracts all the anchors and validates then as # and attaches the events.
	render: function(){
		a = document.getElementsByTagName('a');
		Scroller.end(this);
		window.onscroll
			for (i=0;i<a.length;i++) {
//				console.log(a[i].href);
				if (a[i].href.substr((a[i].href.length)-1,a[i].href.length) != "#" ) { // リンク先が#だけのものは対象外にする。
					l = a[i];
					if(l.href && l.href.indexOf('#') != -1 && ((l.pathname==location.pathname) || ('/'+l.pathname==location.pathname)) ){
						Scroller.add(l,'click',Scroller.end)
						l.onclick = function(){
						Scroller.end(this);
							l=this.hash.substr(1);
							a = document.getElementsByTagName('a');
								for (i=0;i<a.length;i++) {
									if(a[i].name == l){
										clearInterval(Scroller.interval);
										Scroller.interval=setInterval('Scroller.scroll('+Scroller.gy(a[i])+')',10); 
										// ↓ガタツキ防止（START）
										var root = document.getElementsByTagName("body")[0] || document.body;
										root.style.backgroundAttachment = "scroll";
										if (document.getElementById('yMenu')) document.getElementById('yMenu').style.display = "none";
										// ↑ガタツキ防止（START）
									}
								}
						}
					}
				}
			}
	}
}
// invoke the initializer of the scroller
Scroller.init();

