window.SCROLLTO = {
	
	callback : null,
	timer    : null,
	locked   : false,
   steps    : null,
	step     : null,
   stepsize : null,
	from     : null,
	to       : null,

	scroller : function(obj) {
   	obj.step += 1;
		if(obj.step > obj.steps)
		{
			window.scrollTo(0, obj.to);
      	obj.endScroll();
		}
		else
		{
      	window.scrollTo(0, Math.round(obj.from + (obj.step * obj.stepsize)));
		}
	},

	getYOffsetByNode : function( node ) {
   	var offset = 0;
		do
		{
      	offset = offset + node.offsetTop;
		}
		while(node = node.offsetParent);

		return offset;
	},

	endScroll : function() {
      window.clearInterval(this.timer);
   	this.locked = false;
		if(this.callback != null)
			callback();
	},

	scroll : function( node, duration, callback ) {
		if(this.locked)
			return;
      this.locked = true;

		if(typeof(callback) == 'undefined')
			this.callback = null;
	
		this.to   = this.getYOffsetByNode(node);
      
		if(duration <= 0)
		{
			// Instant Scroll
      	window.scrollTo(0, this.to);
			if(this.callback != null)
				callback();

			return;
		}

		this.step = 0;
		this.from = $.browserInfos().yOffset;
		this.steps = Math.ceil(duration/1000*$.infos.animfps);
		this.stepsize = (this.to - this.from) / this.steps;

		var obj = this;
		this.timer = window.setInterval(function(){ SCROLLTO.scroller(obj); }, 1000/$.infos.animfps, this);
	}
}

