/*
：：：：：：：：：：：：：：：：：：：：：：：：：：：：：：

	■Outline
	title：smooth-scroller.js
	since：2008-01-26
	revision：2008-03-18
	description：ページ内リンクへのスムーズスクローラー

	■Reference
	name：Dezinerfolio Inc. <midart@gmail.com>
	title：Smooth Scroller Script
	revision：1.0.1
	URI：http://dezinerfolio.com
	license：MIT License / X11 License

：：：：：：：：：：：：：：：：：：：：：：：：：：：：：：
*/




Scroller = 
{
	speed:5,

	gy: function (d)
	{
		gy = d.offsetTop
		if (d.offsetParent) while (d = d.offsetParent) gy += d.offsetTop
		return gy
	},

	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
	},

	add: function(event, body, d)
	{
		if (event.addEventListener) return event.addEventListener(body, d,false)
		if (event.attachEvent) return event.attachEvent('on'+body, d)
	},

	end: function(e)
	{
		if (window.event)
		{
			window.event.cancelBubble = true
			window.event.returnValue = false
      		return;
    	}
	    if (e.preventDefault && e.stopPropagation)
		{
			e.preventDefault()
			e.stopPropagation()
	    }
	},

	scroll: function(d)
	{
		i = window.innerHeight || document.documentElement.clientHeight;
		h=document.body.scrollHeight;
		a = Scroller.scrollTop()
		if (d>a)
		{
			if (d>h-i) d = h-i;
			a += Math.ceil((d-a)/Scroller.speed);
		}
		else
		a = a+(d-a)/Scroller.speed;
		window.scrollTo(0,a)
	  	if(a==d || Scroller.offsetTop==a)clearInterval(Scroller.interval)
	  	Scroller.offsetTop=a
	},

	init: function()
	{
		Scroller.add(window,'load', Scroller.render)
	},

	render: function()
	{
		a = document.getElementsByTagName('a');
		Scroller.end(this);
		window.onscroll
		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)) )
			{
				Scroller.add(l,'click',Scroller.end)
				l.onclick = function()
				{
					Scroller.end(this);
					l=this.hash.substr(1);
					a = document.getElementById(l);
					if (a)
					{
						clearInterval(Scroller.interval);
						Scroller.interval=setInterval('Scroller.scroll('+Scroller.gy(a)+')',10);
					}
				}
	      	}
		}
	}
}

Scroller.init();