var FloatLayer = Class.create();
FloatLayer.prototype = {
	layer : null,
	distanceY : 0,
	initY : 0,
	currentY : 0,
	beatTimer : 0,
	scrollTimer : 0,
	direction : 0,
	initialize : function(layer, top) {
		this.layer = $(layer);
		this.top = $parseInt(top);
		GlobalEvent.addEvent('scroll', 'FloatLayer', this.scroll.bind(this));
	},

	scroll : function() {
		if(this.beatTimer) {
			clearTimeout(this.beatTimer);
			this.beatTimer = 0;
		}
		if(this.scrollTimer) {
			clearTimeout(this.scrollTimer);
			this.scrollTimer = 0;
		}
		this.scrollTimer = setTimeout(this.start.bind(this), 500);
	},
	
	start : function() {
		if(!this.beatTimer) {
			this.currentY =  this.layer.offsetTop + this.top;
			this.direction = DocumentBody.getScrollTop() - this.currentY;
			this.beatTimer = setInterval(this.beat.bind(this), 20);
		}
	},
	
	stop : function() {
		if(this.beatTimer) {
			clearInterval(this.beatTimer);
			this.beatTimer = 0;
		}
	},

	beat : function() {
		var percent;
		var distance;

		if(this.direction > 0) { //down
			distance = DocumentBody.getScrollTop() + DocumentBody.getHeightExcludeScroll() - (this.currentY + this.layer.getHeight());
			if(distance > 0) {
				percent	= .2 * distance;
				if(percent > 0)	percent	= Math.ceil(percent);
				else percent = Math.floor(percent);

				this.layer.style.top = this.layer.offsetTop + percent + 'px';
				this.currentY += percent;
			}else {
				this.stop();
			}
		}else { //up
			var scrollY = (DocumentBody.getScrollTop() <= this.top)?this.top:DocumentBody.getScrollTop();
			distance = scrollY - this.currentY;
			if(distance < 0) {
				percent	= .2 * distance;
				if(percent > 0)	percent	= Math.ceil(percent);
				else percent = Math.floor(percent);

				this.layer.style.top = this.layer.offsetTop + percent + 'px';
				this.currentY += percent;
			}else {
				this.stop();
			}
		}
	
	}
}
