/*--------------------------------------------------------------------------
 *  Smooth Scroller Script, version 1.0.1
 *  (c) 2007 Dezinerfolio Inc.
 *
 *  For details, please check the website : http://dezinerfolio.com/
 *
/*--------------------------------------------------------------------------*/

Scroller = {
	// control the speed of the scroller.
	// dont change it here directly, please use Scroller.speed=50;
	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.
	// d: どこまでスクロールするか
	scroll: function(d){
		bH = document.body.scrollHeight;
		wH = window.innerHeight || document.documentElement.clientHeight;
		a = Scroller.scrollTop();	// 現在位置
		if(d > bH - wH){
			d = bH - wH;
		}
		a += Math.ceil((d - a)/Scroller.speed);
		window.scrollTo(0, a);	// スクロール実行
	  	if(a == d || Scroller.offsetTop == a){
	  		// スクロール終了
	  		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(){
		var a = document.getElementsByTagName('a');
		Scroller.end(this);
	    for(var i = 0; i < a.length; i++){
			var l = a[i];
			if(l.href
			&& l.href.indexOf('#') != -1
			&& ((l.pathname == location.pathname) || ('/' + l.pathname == location.pathname))){
				// ページ内リンク発見
	        	var hash = l.hash.substr(1);	// hrefの # を抜いた部分
	        	if(hash == ""){
	        		continue;
	        	}
	        	var b = document.getElementsByTagName('a');
	        	l.toscroll = null;
			    for(var j = 0; j < b.length; j++){
			    	if(b[j].name != hash) continue;
			    	l.toscroll = b[j];
				}
				Scroller.add(l, 'click', Scroller.end);
				l.onclick = function(){
					if(this.toscroll){
			    		clearInterval(Scroller.interval);	// インターバル解除
			    		Scroller.interval = setInterval('Scroller.scroll(' + Scroller.gy(this.toscroll) + ')', 10);
			    	}
				};
	      	}
		}
	}
}
// invoke the initializer of the scroller
Scroller.init();


/*------------------------------------------------------------
 *						END OF CODE
/*-----------------------------------------------------------*/
