
// modal window control

var _browser = navigator.userAgent;
var isIE = document.defaultView ? false : true;
var IEvers = isIE ? Number(_browser.substring(_browser.indexOf('MSIE ')+4,_browser.indexOf('MSIE ')+8)) : null;
var IE7 = isIE ? IEvers > 6 ? true : false : false;

var currentModalContent = null;
var modalInterval;

function ShowModal(elemID) {
	var modalW = document.getElementById('ModalOverPage');
	var divShimBG = document.getElementById('ModalFrame');
	var elemC = document.getElementById(elemID);
	currentModalContent = elemID;

	modalW.onClick = void(0);
	
	divShimBG.style.display='block';
	divShimBG.style.left = '0px';
	divShimBG.style.top = '0px';
	divShimBG.style.width = '99%';
	divShimBG.style.height = '99%';
	
	if(!document.defaultView && IEvers <= 6) {
		// IE6...
		//modalW.style.height = $clientHeight();
		//divShimBG.style.height = $clientHeight();
		modalW.style.height = document.body.offsetHeight;
		divShimBG.style.height = document.body.offsetHeight;
	}
	//alert(document.body.offsetHeight + " : " + $clientHeight());
	
	elemC.style.display = 'block';
	modalW.style.display = 'block';
	
	var marginGapTop = Math.floor(($clientHeight() - elemC.offsetHeight) / 2);
	var marginGapLeft = Math.floor(($clientWidth() - elemC.offsetWidth) / 2);
	elemC.style.marginTop = marginGapTop + 'px';
	elemC.style.marginLeft = marginGapLeft + 'px';
	
	modalInterval = setInterval(function(){CheckPosition();},20);
}

function CloseModal() {
	clearInterval(modalInterval);
	var modalW = document.getElementById('ModalOverPage');
	var divShimBG = document.getElementById('ModalFrame');
	var elemC = document.getElementById(currentModalContent);
	modalW.style.display = 'none';
	divShimBG.style.display = 'none';
	elemC.style.display = 'none';
}

function CheckPosition() {
	
	var modalW = document.getElementById('ModalOverPage');
	var divShimBG = document.getElementById('ModalFrame');
	var elemC = document.getElementById(currentModalContent);
	
	if(!document.defaultView && IEvers <= 6) {
		// IE6...
		/*
		modalW.style.height = $clientHeight();
		modalW.style.top = $scrollTop() + 'px';
		modalW.style.left = $scrollLeft() + 'px';
		
		divShimBG.style.top = $scrollTop() + 'px';
		divShimBG.style.left = $scrollLeft() + 'px';
		*/
		elemC.style.top = $scrollTop() + 'px';
		elemC.style.left = $scrollLeft() + 'px';
	}
	var marginGapTop = Math.floor(($clientHeight() - elemC.offsetHeight) / 2);
	var marginGapLeft = Math.floor(($clientWidth() - elemC.offsetWidth) / 2);
	elemC.style.marginTop = marginGapTop + 'px';
	elemC.style.marginLeft = marginGapLeft + 'px';
}

function $clientWidth() {
	return _browserSafe (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function $clientHeight() {
	return _browserSafe (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function $scrollLeft() {
	return _browserSafe (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function $scrollTop() {
	return _browserSafe (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function _browserSafe(n_win, n_docEl, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docEl && (!n_result || (n_result > n_docEl))) {
		n_result = n_docEl;
	}
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
