/* (C)kaizu */


function SmoothScroll() {
	var ua = navigator.userAgent;
	if (ua.match(/MSIE 4\./) || document.layers || (ua.match(/MSIE/) && ua.match(/Mac/))) return false;
	this.isOldIE = ua.match(/MSIE (5|6)\./);
	this.anchorInterval = null;
	this.easing = 0.1;
	if (window.addEventListener) {
		window.addEventListener('load', this.setupAnchor, true);
		document.addEventListener('click', this.stopScroll, true);
		document.addEventListener('DOMMouseScroll', this.stopScroll, true);
	} else {
		window.attachEvent('onload', this.setupAnchor);
		document.attachEvent('onmousedown', this.stopScroll);
		document.attachEvent('onmousewheel', this.stopScroll);
	}
}


SmoothScroll.prototype = {

	setupAnchor : function() {
		var anchors = document.getElementsByTagName('a');
		var url = location.href.replace(/\#.*/, '');
		for (var i = 0, length = anchors.length; i < length; i++) {
			if (anchors[i].href.indexOf(url) > -1 && anchors[i].href.replace(url, '').indexOf('#') == 0) {
				var rel = anchors[i].href.replace(/.*\#/, '');
				if (!document.getElementById(rel)) continue;
				anchors[i].rel = rel;
				anchors[i].href = "javascript:void(0)";
				anchors[i].onclick = function() {
					SS.anchorInterval = setInterval('SS.smoothScroll("' + this.rel + '")', 10);
				}
			}
		}
	},
	
	getStyle : function(obj) {
		return obj.currentStyle || document.defaultView.getComputedStyle(obj, '');
	},
	
	smoothScroll : function(id) {
		clearInterval(SS.anchorInterval);
		SS.anchorInterval = null;

		var getOffsetLeft = function(obj) {
			return obj.offsetLeft + (obj.offsetParent ? getOffsetLeft(obj.offsetParent) : 0);
		};
		var getOffsetTop = function(obj) {
			return obj.offsetTop + (obj.offsetParent ? getOffsetTop(obj.offsetParent) : 0);
		};
		var getPosition = function() {
			var x, y;
			if (document.all) {
				if (document.compatMode == "BackCompat") {
					x = document.body.scrollLeft;
					y = document.body.scrollTop;
				} else {
					x = document.documentElement.scrollLeft;
					y = document.documentElement.scrollTop;
				}
			} else if (document.layers || window.opera) {
				x = window.pageXOffset;
				y = window.pageYOffset;
			} else if (navigator.userAgent.indexOf('Gecko') != -1) {
				x = window.scrollX;
				y = window.scrollY;
			} else {
				x = y = 0;
			}
			return {X:x, Y:y};
		}

		var pos = getPosition();
		var w = window;
		var b = document.body;
		var pageLeft   = b.offsetLeft;
		var pageTop    = b.offsetTop;
		var pageWidth  = b.offsetWidth;
		var pageHeight = (SS.isOldIE) ? b.scrollHeight  : b.offsetHeight;
	
		if (!SS.isOldIE) {
			var style = SS.getStyle(b);			
			pageWidth += parseInt(style.marginLeft) + parseInt(style.marginRight) + parseInt(style.paddingLeft) + parseInt(style.paddingRight);
			pageHeight += parseInt(style.marginTop) + parseInt(style.marginBottom) + parseInt(style.paddingTop) + parseInt(style.paddingBottom);	
		}

		var windowWidth  = (w.innerWidth)  ? w.innerWidth  : document.documentElement.offsetWidth;
		var windowHeight = (w.innerHeight) ? w.innerHeight : document.documentElement.offsetHeight;		
		var scrollableWidth  = pageWidth - windowWidth;
		var scrollableHeight = pageHeight - windowHeight;

		var o = document.getElementById(id);
		var targetX = getOffsetLeft(o);
		var targetY = getOffsetTop(o);
		if (scrollableWidth  < targetX) targetX = scrollableWidth;
		if (scrollableHeight < targetY) targetY = scrollableHeight;
		if (targetX < 0) targetX = 0;
		if (targetY < 0) targetY = 0;
		var distanceX = targetX - pos.X;
		var distanceY = targetY - pos.Y;

		if (Math.abs(distanceX) > 1 || Math.abs(distanceY) > 1) {
			var moveX = (distanceX == 0) ? 0 : Math.abs(distanceX * this.easing) > 1 ? distanceX * this.easing : (distanceX > 0) ? 1 : -1;
			var moveY = (distanceY == 0) ? 0 : Math.abs(distanceY * this.easing) > 1 ? distanceY * this.easing : (distanceY > 0) ? 1 : -1;
			var newX = pos.X + moveX;
			var newY = pos.Y + moveY;
			var duration = (moveX + moveY > 2) ? 10 : 20;
			window.scrollTo(newX, newY);
			SS.anchorInterval = setInterval('SS.smoothScroll("' + id + '")', duration);			
		} else {
			window.scrollTo(targetX, targetY);
			//alert('stop');//debug
		}
		
	},

	stopScroll : function() {
		if (SS.anchorInterval != null) {
			clearInterval(SS.anchorInterval);
			SS.anchorInterval = null;
		}
	}

}

var SS = new SmoothScroll();
