
var $scroller;
var Scroller = Class.create();

Scroller.prototype.initialize = function() {
	this.motionInterval = 1;
	var self = this;
	var nav = document.getElementById("backto-top");
	var node = nav.getElementsByTagName("a")[0];
	node.ref = node.getAttribute('href', 2).replace(/.*#/, '');
	node.href = 'javascript:void(0);';
	node.onclick = function() { self.slideTo(this.ref); }
	this.setRollOver(node);
}


Scroller.prototype.slideTo  = function(node) {
	var self = this;
	var targetNode = $(node);
	if(!targetNode) { return; }

	var getNodePosition = function() {
		var position = {
			x: Position.cumulativeOffset(targetNode)[0],
			y: Position.cumulativeOffset(targetNode)[1]
		};
		return position;
	}

	var getPageProperty = function() {
		var property = {};

		property.inner = {
			width : window.innerWidth  || document.documentElement.clientWidth  || document.body.clientWidth,
			height: window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
		}

		property.maximum = {
			width : document.body.scrollWidth || document.body.offsetWidth,
			height: window.innerHeight + window.scrollMaxY || document.body.scrollHeight || document.body.offsetHeight
		}

		property.offset = {
			x: window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
			y: window.pageYOffset || document.documentElement.scrollTop  || document.body.scrollTop
		}

		return property;
	}

	var slideY = function() {
		var pageProperty = getPageProperty();
		var nodePosition = getNodePosition();
		var scrollLimitY = pageProperty.maximum.height - pageProperty.inner.height;
		var finishPoint  = Math.min(nodePosition.y, scrollLimitY);
		
		if(!scrollLimitY) {
			location.hash = node;
			return;
		}
		
		
		var distance = scrollLimitY - finishPoint;
		
		var duration = Math.floor(distance / 10);
		var millisec = 10;
		var count    = 0;
		
		if(duration < 10) {
			duration = 10;
		}
		
		self.motionInterval = setInterval(
			function() {
				var prop = getPageProperty();
				var offsetPoint = prop.offset.y;
				var offsetDelta = Math.abs(offsetPoint - finishPoint);
				var offsetRatio = Math.floor( Math.max(offsetDelta / duration) * count++ ) + 1;
				
				var direction   = ((offsetPoint - finishPoint) > 0) ? -1 : 1;
				var variation   = Math.min(offsetRatio, offsetDelta) * direction;

				window.scrollBy(0, variation);
				if(offsetDelta <= 1) {
					clearInterval(self.motionInterval);
					location.hash = node;
					self.unsetForceStop();
				}
			},
			millisec
		);
		
		/*
		setTimeout(
			function() { clearInterval(motion); },
			duration * millisec * 3
		);
		*/
		self.setForceStop();
	}
	
	slideY();
}


Scroller.prototype.setForceStop = function() {
	Event.observe(
		window,
		'mousewheel',
		$scroller.forceStop,
		false
	);
}

Scroller.prototype.unsetForceStop = function() {
	Event.stopObserving(
		window,
		'mousewheel',
		$scroller.forceStop,
		false
	);
}


Scroller.prototype.forceStop = function() {
	clearInterval($scroller.motionInterval);
	$scroller.unsetForceStop();
}




Scroller.prototype.setRollOver = function(node) {
	var img = node.getElementsByTagName("img")[0];
	node.onmouseover = function() {
		img.src = img.src.replace(".gif", "_active.gif");
	}
	node.onmouseout = function() {
		img.src = img.src.replace("_active.gif", ".gif");
	}
}




Event.observe(
    window,
    'load',
    function() {
        $scroller = new Scroller();
    },
    false
);


