/**
 * example:
 *  fitPopupSize( [doMove] );                  <- ID가 'page_content'인 object의 clientWidth, clientHeight 값에 따라 사이즈 조절
 *  fitPopupSize( object, [doMove] );          <- 주어진 object의 clientWidth, clientHeight값에 따라 사이즈 조절
 *  fitPopupSize( "pageOuter", [doMove] );     <- ID가 'pageOuter'인 object의 clientWidth, clientHeight 값에 따라 사이즈 조절
 *  fitPopupSize( 300, 400, [doMove] );        <- 팝업 크기를 300 x 400으로 조절
 */
function fitPopupSize( arg1, arg2, arg3 ) {
	var toWidth = null;
	var toHeight = null;
	var objOut = null;
	var positionRequired = true;

	var typeArg1 = typeof(arg1);
	var typeArg2 = typeof(arg2);
	var typeArg3 = typeof(arg3);
	
	if( typeArg1 == "undefined" || typeArg1 == "boolean" ) {
		objOut = document.getElementById("page_content");
		
		if( typeArg1 == "boolean" ) {
			positionRequired = arg1;
		}
	}
	if( typeArg1 == "string" ) {
		objOut = document.getElementById(arg1);
		if( objOut == null ) { return; }
				
		if( typeArg2 == "boolean" ) {
			positionRequired = arg2;
		}
	}

	if( typeArg1 == "object" ) {
		objOut = arg1;
				
		if( typeArg2 == "boolean" ) {
			positionRequired = arg2;
		}
	}
	
	if( objOut != null ) {
		var objParent = objOut.parentNode;
		var getStyle = function(obj) {
			if( obj.currentStyle ) {
				return obj.currentStyle;
			} else if( document.defaultView.getComputedStyle ) {
				return document.defaultView.getComputedStyle(obj, null);
			}
			}

		if( objParent != null && objParent.tagName == "BODY" && getStyle(objOut).position == "absolute" ) {
			try {
				var bodyPaddingTop = 0; var outerPaddingTop = 0;
				var bodyPaddingBottom = 0; var outerPaddingBottom = 0;
				var bodyPaddingLeft = 0; var outerPaddingLeft = 0;
				var bodyPaddingRight = 0; var outerPaddingRight = 0;
				
				if( document.body.currentStyle ) {
					var bodyCurrentStyle = document.body.currentStyle;
					bodyPaddingTop = parseInt(bodyCurrentStyle.paddingTop);
					bodyPaddingBottom = parseInt(bodyCurrentStyle.paddingBottom);
					bodyPaddingLeft = parseInt(bodyCurrentStyle.paddingLeft);
					bodyPaddingRight = parseInt(bodyCurrentStyle.paddingRight);
					
					var outerCurrentStyle = objOut.currentStyle;
					outerPaddingTop = parseInt(outerCurrentStyle.paddingTop);
					outerPaddingBottom = parseInt(outerCurrentStyle.paddingBottom);
					outerPaddingLeft = parseInt(outerCurrentStyle.paddingLeft);
					outerPaddingRight = parseInt(outerCurrentStyle.paddingRight);
					
				} else if( document.defaultView.getComputedStyle ) {
					var objBodyStyle = document.defaultView.getComputedStyle(document.body, null);
					bodyPaddingTop = parseInt(objBodyStyle.getPropertyValue("padding-top"));
					bodyPaddingBottom = parseInt(objBodyStyle.getPropertyValue("padding-bottom"));
					bodyPaddingLeft = parseInt(objBodyStyle.getPropertyValue("padding-left"));
					bodyPaddingRight = parseInt(objBodyStyle.getPropertyValue("padding-right"));
					
					var outerCurrentStyle = document.defaultView.getComputedStyle(objOut, null);
					outerPaddingTop = parseInt(outerCurrentStyle.getPropertyValue("padding-top"));
					outerPaddingBottom = parseInt(outerCurrentStyle.getPropertyValue("padding-bottom"));
					outerPaddingLeft = parseInt(outerCurrentStyle.getPropertyValue("padding-left"));
					outerPaddingRight = parseInt(outerCurrentStyle.getPropertyValue("padding-right"));
				}
				
				var adjustHeight = bodyPaddingTop + bodyPaddingBottom;
				var adjustWidth = bodyPaddingLeft + bodyPaddingRight;
				
				var oWidth = objOut.clientWidth;
				var oHeight = objOut.clientHeight;
				
				// objOut.style.height = oHeight + adjustHeight;				
				objOut.style.paddingTop = outerPaddingTop + bodyPaddingTop;
				objOut.style.paddingBottom = outerPaddingBottom + bodyPaddingBottom;

				// objOut.style.width = oWidth + adjustWidth;
				objOut.style.paddingLeft = outerPaddingLeft + bodyPaddingLeft;
				objOut.style.paddingRight = outerPaddingRight + bodyPaddingRight;
			} catch(e) {
				// do nothing
				// alert( e.message );
			}
		}
		toWidth = objOut.clientWidth;
		toHeight = objOut.clientHeight;
	}

	if( typeArg1 == "number" && typeof(arg2) == "number" ) {
		toWidth = arg1;
		toHeight = arg2;
		
		if( typeArg3 == "boolean" ) {
			positionRequired = arg3;
		}
	}

	if( toWidth == null && toHeight == null ) { return; }
	
	if (toWidth > screen.availWidth) toWidth = screen.availWidth;
	if (toHeight > screen.availHeight) toHeight = screen.availHeight;


	if( positionRequired ) {
		fitPopupSize_adjustPosition( toWidth, toHeight );
	}

	fitPopupSize_resize( toWidth, toHeight );
}

function fitPopupSize_adjustPosition( toWidth, toHeight ) {
	var posLeft = (window.screenLeft) ? window.screenLeft : window.screenX;
	var posTop = (window.screenTop) ? window.screenTop : window.screenY;
	var adjustLeft = 0;	var marginWidth = 50;
	var adjustTop = 0;	var marginHeight = 50;
	var movingRequired = false;

	if( posTop + toHeight + marginHeight > screen.availHeight ) {
		adjustTop = -(posTop + toHeight + marginHeight - screen.availHeight);
		movingRequired = true;
	}
	if( posLeft + toWidth + marginWidth > screen.availWidth ) {
		adjustLeft = -(posLeft + toWidth + marginWidth - screen.availWidth);
		movingRequired = true;
	}
	if( movingRequired ) {
		window.moveBy( adjustLeft, adjustTop );
	}
}

function fitPopupSize_resize( toWidth, toHeight ) {
	var oBody = document.body;
	if( oBody == null ) { return; }

	if( typeof(window.innerHeight) != "undefined" && typeof(window.innerWidth) != "undefined" ) {
		window.innerHeight = toHeight;
		window.innerWidth = toWidth;
	} else {
		var clientWidth = Math.max( document.documentElement.clientWidth, oBody.clientWidth );
		var clientHeight = Math.max( document.documentElement.clientHeight, oBody.clientHeight );
	
		var diffX = toWidth - clientWidth;
		var diffY = toHeight - clientHeight;

		window.resizeBy( diffX, diffY );
	}
}
