
var Floating = function (FloatingObj, MarginX, MarginY, Percentage, setTime) {
	this.FloatingObj = FloatingObj;
	this.MarginX = (MarginX) ? MarginX : 0;
	this.MarginY = (MarginY) ? MarginY : 0;
	this.Percentage = (Percentage) ? Percentage : 20;
	this.setTime = (setTime) ? setTime : 10;
	this.FloatingObj.style.position = "absolute";
	this.isStrict	= (document.compatMode == 'CSS1Compat');
	this.Body = null;
	this.setTimeOut = null;
	this.Init();
}

Floating.prototype.Init = function () {
	this.Body = ( this.isStrict ? document.documentElement : document.body );
	var goPosition = this.GetToGoPosition();
	this.FloatingObj.style.left = goPosition.x + "px";
	this.FloatingObj.style.top = goPosition.y + "px";
	if (window.attachEvent){
		window.attachEvent('onscroll', this.Run.bind(this));
		window.attachEvent('onresize', this.Run.bind(this));
	}else{
		window.addEventListener('scroll', this.Run.bind(this), false);
		window.addEventListener('resize', this.Run.bind(this), false);
	}
	this.FloatingObj.style.visibility = 'visible';
}

Floating.prototype.Run = function () {
	this.Body = ( this.isStrict ? document.documentElement : document.body );
	var FloatingObjLeft	= (this.FloatingObj.style.left) ? parseInt(this.FloatingObj.style.left,10) : this.FloatingObj.offsetLeft;
	var FloatingObjTop	= (this.FloatingObj.style.top) ? parseInt(this.FloatingObj.style.top,10) : this.FloatingObj.offsetTop;
	var goPosition = this.GetToGoPosition();

	var MoveX	= Math.abs(FloatingObjLeft - goPosition.x);
	MoveX	= Math.ceil(MoveX / this.Percentage);
	var MoveY	= Math.abs(FloatingObjTop - goPosition.y);
	MoveY	= Math.ceil(MoveY / this.Percentage);

	if ((MoveX != 0) || (MoveY != 0)) {
		if (FloatingObjLeft < goPosition.x) {
			this.FloatingObj.style.left = FloatingObjLeft + MoveX + "px";
		} else {
			this.FloatingObj.style.left = FloatingObjLeft - MoveX + "px";
		}
		if (FloatingObjTop < goPosition.y) {
			this.FloatingObj.style.top = FloatingObjTop + MoveY + "px";
		} else {
			this.FloatingObj.style.top = FloatingObjTop - MoveY + "px";
		}
		window.clearTimeout(this.setTimeOut);
		this.setTimeOut = window.setTimeout(this.Run.bind(this), this.setTime);
	}
}

Floating.prototype.GetToGoPosition = function () {
	this.Body = ( this.isStrict ? document.documentElement : document.body );
	var DocLeft	= parseInt(this.Body.clientWidth/2) + this.MarginX;
	var DocTop	= this.Body.scrollTop + this.Body.clientHeight - this.MarginY;
	return { x:DocLeft, y:DocTop };
}
