//----------------------------------------------
//Floating v1.1 Source By Bermann
//dobermann75@gmail.com
//----------------------------------------------

//new Floating(Àû¿ëÇÒ°³Ã¼ , XÃà¿©¹é , YÃà¿©¹é , ¹Ì²ô·¯Áö´Â¼Óµµ:ÀÛÀ»¼ö·Ïºü¸§..±âº»20 , ºü¸£±â:ÀÛÀ»¼ö·ÏºÎµå·¯¿ò..±âº»10);

function Floating(FloatingObj,MarginX,MarginY, position, Percentage,setTime) {
	this.FloatingObj = FloatingObj;
	this.MarginX = (MarginX) ? MarginX : 0;
	this.MarginY = (MarginY) ? MarginY : 0;
	this.Percentage = (Percentage) ? Percentage : 20;
	this.setTime = (setTime) ? setTime : 20;
	this.FloatingObj.style.position = (position) ? position : "absolute";
	this.Body = null;
	this.setTimeOut = null;

	this.cnt = 0;
	this.Run();

	
}



Floating.prototype.Run = function () {
	if ((document.documentElement.scrollLeft + document.documentElement.scrollTop) > (document.body.scrollLeft + document.body.scrollTop)) {
		this.Body = document.documentElement;
	} else {
		this.Body = document.body;
	}

	var This = this;
	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 DocLeft = this.Body.scrollLeft + this.MarginX;
	var DocTop = this.Body.scrollTop + this.MarginY;

	var MoveX = Math.abs(FloatingObjLeft - DocLeft);
	MoveX = Math.ceil(MoveX / this.Percentage);
	var MoveY = Math.abs(FloatingObjTop - DocTop);
	MoveY = Math.ceil(MoveY / this.Percentage);
/*
	if (FloatingObjLeft < DocLeft) {
		this.FloatingObj.style.left = FloatingObjLeft + MoveX + "px";
	} else {
		this.FloatingObj.style.left = FloatingObjLeft - MoveX + "px";
	}
*/

	try{
		var top = 0;
		
		if (FloatingObjTop < DocTop) {
			top = FloatingObjTop + MoveY;
		} else {
			top = FloatingObjTop - MoveY;
		}

		var bodyHeight = this.Body.offsetHeight + this.Body.scrollTop;

		var max = (screen.availHeight > bodyHeight) ? screen.availHeight : bodyHeight;

		max -= 50;
		if(this.FloatingObj.getHeight()+FloatingObjTop > max){
			top = top  - (this.FloatingObj.getHeight()+FloatingObjTop - max);
		}
	}catch(ex){}

	this.FloatingObj.style.top = top + 'px';

	window.clearTimeout(this.setTimeOut);
	this.setTimeOut = window.setTimeout(function () { This.Run(); },this.setTime);
}
