IntoViewScroller = function (objectID) {

	this.subject = document.getElementById(objectID);
	if (!this.subject) return;

	this.STATIC  = 0; //In position
	this.WAITING = 1; //Scroll event detected, eager to start moving
	this.MOVING  = 2; //Being animated to new position
	this.state = this.STATIC;
	
	this.minimumTop = calcY(this.subject) + 5;
	this.lastTop = (document.all) ? calcTop() : 0;
	setInterval(this.createContextFunction("poll"), 200);

	this.animator = new Animator(0, 100, this.createContextFunction("doMove"), this.createContextFunction("stop"));
	this.animator.setType(this.animator.EASEOUT);
	this.animator.setPower(4);
}
IntoViewScroller.prototype.wait = function () {
	if (this.state == this.MOVING) return;
	this.state = this.WAITING;
	if (this.waiting) clearTimeout(this.waiting);
	this.waiting = setTimeout(this.createContextFunction("move"), 1000);
}
IntoViewScroller.prototype.move = function () {
	this.waiting = null;
	this.state = this.MOVING;
	var scrollTop = calcTop();
	this.from = calcY(this.subject);
	this.to = (scrollTop <= this.minimumTop) ? this.minimumTop : (scrollTop+20);
	this.to = (isNaN(this.to)) ? this.minimumTop : this.to;
	this.animator.start();
}
IntoViewScroller.prototype.doMove = function (x) {
	var factor = x/100;
	var y = this.from + factor*(this.to-this.from);
	if (y) this.subject.style.top = parseInt(y)+"px"
}
IntoViewScroller.prototype.stop = function () {
	this.state = this.STATIC;
}
IntoViewScroller.prototype.poll = function () {
	var scrollTop = calcTop();
	if (this.lastTop != scrollTop) {
		this.lastTop = scrollTop;
		this.wait();
	}
}
IntoViewScroller.prototype.createContextFunction = function (method) {
	var context = this;
	return (function(x){
		eval("context."+method+"(x)");
		return false;
    });
}
function calcTop() {
	var result = 0;
	if (document.documentElement) result = document.documentElement.scrollTop;
	if (document.body) result = document.body.scrollTop;
	if (document.documentElement && document.body) result = Math.max(document.documentElement.scrollTop,document.body.scrollTop);
	if (!result) result = window.pageYOffset;
	return result;
}
function calcY(o) {if (o) return o.offsetTop + calcY(o.offsetParent); else return 0;}
