// Trim ÇÔ¼ö ##################################################
// Ex) str = "    Å× ½ºÆ®   ".trim(); => str = "Å× ½ºÆ®";
String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

// ¹®ÀÚ¿­ °ø¹éÁ¦°Å ÇÔ¼ö ##################################################
// Ex) str = "    Å× ½º   Æ®   ".stripspace(); => str = "Å×½ºÆ®";
String.prototype.stripspace = function() {
	return this.replace(/ /g, "");
}

// ÀüÃ¼ ¹®ÀÚ¿­ ¹Ù²Ù±â ÇÔ¼ö ##################################################
// Ex) str = "aÅ×½ºÆ®bcdÅ×½ºÆ®efg".replaceAll("Å×½ºÆ®", ""); => str = "abcdefg";
String.prototype.replaceAll = function(a, b) {
	var s = this;
	var n1, n2, s1, s2;

	while (true) {
		if ( s=="" || a=="" ) break;
		n1 = s.indexOf(a);
		if ( n1 < 0 ) break;
		n2 = n1 + a.length;
		if ( n1==0 ) {
			s1 = b;
		}
		else {
			s1 = s.substring(0, n1) + b;
		}
		if ( n2 >= s.length ) {
			s2 = "";
		}
		else {
			s2 = s.substring(n2, s.length);
		}
		s = s1 + s2;
	}
	return s;
}

// Event Ãß°¡ ##################################################
function addEvent(obj, evt, exec) {
	if (window.attachEvent) obj.attachEvent('on'+evt, exec);
	else if (window.addEventListener) obj.addEventListener(evt, exec, false);
	else obj['on'+evt] = exec;
}

function floating() {
	this.isIE = (navigator.userAgent.toLowerCase().indexOf("msie") != -1);

	this.objBasis = null;									// ÇÃ·ÎÆÃ ±âÁØ°´Ã¼
	this.objFloating = null;								// ÇÃ·ÎÆÃ °´Ã¼
	this.timer = null;										// Å¸ÀÓ °´Ã¼

	this.speed = 5;										// ¹Ì²ô·¯Áö´Â¼Óµµ : ÀÛÀ»¼ö·Ï ºü¸§
	this.time = 10;											// ºü¸£±â : ÀÛÀ»¼ö·Ï ºÎµå·¯¿ò
	this.isMove = true;									// ÀÌµ¿¿©ºÎ

	this.initTop = 0;												// ÃÊ±â TOP À§Ä¡ : ¼³Á¤½Ã ÇØ´çÀ§Ä¡¿¡¼­ marginTop À§Ä¡·Î ¹Ì²ô·¯Á® ¿Â´Ù. ¿¹) -1000

	this.bodyMargin = { left : 0, top : 0 };		// BODY MARGIN

	this.left;
	this.top;

	this.marginLeft;
	this.marginTop = 80;

	this.init = function() {
		var self = this;

		if (!this.isIE) this.speed *= 1.2;

		if (this.objFloating) {
			this.marginLeft = (typeof this.marginLeft == "undefined") ? 0 : parseInt(this.marginLeft, 10);

			if (this.objBasis) {
				this.marginTop = (typeof this.marginTop == "undefined") ? this.getOffset(this.objBasis).top : parseInt(this.marginTop, 10);

				this.left = parseInt(this.objBasis.clientWidth, 10) + this.marginLeft;
				this.top = this.marginTop;

				this.objFloating.style.left = (this.getOffset(this.objBasis).left + this.left) + "px";
				this.objFloating.style.top = ((typeof this.initTop == "undefined") ? this.top : this.initTop) + "px";
				this.objFloating.style.display = "";

				this.addEvent(window, 'resize', function() { self.resize(); });
			}
			else {
				this.marginTop = (typeof this.marginTop == "undefined") ? 0 : parseInt(this.marginTop, 10);

				this.top = this.getOffset(this.objFloating).top + this.marginTop;

				this.objFloating.style.marginLeft = this.marginLeft + "px";
				this.objFloating.style.top = this.top + "px";

				this.objFloating.style.display = "";
			}
		}
	}

	this.run = function() {
		var self = this;

		if (!this.objFloating) return;

		var floatingTop = (this.objFloating.style.top) ? parseInt(this.objFloating.style.top, 10) : this.objFloating.offsetTop;
		var docTop = (document.documentElement.scrollTop || document.body.scrollTop) + this.top;
		var moveTop = Math.ceil(Math.abs(floatingTop - docTop) / this.speed);

		if (floatingTop < docTop) {
			this.objFloating.style.top = (floatingTop + moveTop) + "px";
		}
		else {
			this.objFloating.style.top = (floatingTop - moveTop) + "px";
		}

		this.timer = setTimeout(function () { self.run() }, this.time);
	}

	this.move = function() {
		if (this.isMove) {
			this.isMove = false;
			clearTimeout(this.timer);
			this.objFloating.style.top = this.top + "px";
		}
		else {
			this.isMove = true;
			this.run();
		}
	}

	this.resize = function() {
		if (this.objFloating) this.objFloating.style.left = (this.getOffset(this.objBasis).left + this.left) + "px";
	}

	this.getOffset = function(obj) {
		var objOffset = { left : 0, top : 0 };
		var objOffsetParent = obj.offsetParent;

		objOffset.left = parseInt(obj.offsetLeft, 10);
		objOffset.top = parseInt(obj.offsetTop, 10);

		while (objOffsetParent) {
			objOffset.left += parseInt(objOffsetParent.offsetLeft, 10);
			objOffset.top += parseInt(objOffsetParent.offsetTop, 10);

			objOffsetParent = objOffsetParent.offsetParent;
		}

		return objOffset;
	}

	this.addEvent = function(obj, evt, exec) {
		if (window.attachEvent) obj.attachEvent('on'+evt, exec);
		else if (window.addEventListener) obj.addEventListener(evt, exec, false);
		else obj['on'+evt] = exec;
	}
}
