﻿(function(doc) {

/**
*   Popup resizer ( 2009-05-11 )
*   - resizeTo normalize
*   - extend resize animation
*/

var qMode = !doc.compatMode || doc.compatMode.indexOf('CSS1') == -1, done, _w, _h, obj = {};

function root() {
	return  qMode ? doc.documentElement  || doc.body : doc.documentElement;
};

function cycle(c) {
	return c > 0 ? Math.max( c, 1 ) : c < 0 ? Math.min( c, -1 ) : 0;
};

function ready(fn) {
	var f = function() {
		done = true;
		fn();
	};
	if (doc.attachEvent) window.attachEvent("onload", f);
	else window.addEventListener("load", f, false);
}

// new method : sync to Element size
window.resizeById = function(targetId, speed) {
	ready(function() {
		var context = doc.getElementById(targetId);
		if (context) {
			resizeTo(context.clientWidth, context.clientHeight, speed);
		} else {
			alert( 'resizeById 실행중 오류가 발생하였습니다.\n\n내용 : "' + targetId + '" ID에 해당하는 요소를 찾을 수 없습니다.' );
		}
	});
};

// function override
window.resizeTo = function(x, y, speed) {
	if (!done) return ready(function() {
		resizeTo(x, y, speed);
	});

	if( !x && !y) {
		x = root().scrollWidth;
		y = root().scrollHeight;
	};

	// setting
	_w = _h = null;
	obj.__targetX = x;
	obj.__targetY = y;
	obj.__speed = speed || 20;

	// resize start
	clearInterval(obj.__resizeTimer);
	obj.__resizeTimer = setInterval(function()
	{
		var w = root().clientWidth, h = root().clientHeight;
		obj.__moveX = cycle( (obj.__targetX - w) * (obj.__speed/20) );
		obj.__moveY = cycle( (obj.__targetY - h) * (obj.__speed/20) );

		var up = screen.availHeight - (window.screenTop + y) - 20;
		var left = window.screenLeft > screen.availWidth ? 0 : screen.availWidth - (window.screenLeft + x) - 20;
		moveBy((left < 0 ? left : 0) , (up < 0 ? up : 0));

		if (_w == w && _h == h || !obj.__moveX && !obj.__moveY) {
			clearInterval(obj.__resizeTimer);
		} else {
			resizeBy(obj.__moveX, obj.__moveY);
			_w = w;
			_h = h;
		}
	}, 15);
};

})(document);