(function() {

//	@ Ntreevsoft Inner Popup JS v1.01 (2009.03.23)
//   script by Su-hyung, park

// 이미지파일 경로 및 타이틀 이미지 명 정의
var nSetting = {
	path : "http://public.gametree.co.kr/Global/img/nPopup/",
	title: 'npop_title_gametree.gif'
}

var nRoot, nDoc = document, nMask, nIng = false;
var nPopup = window.nPopup = function(url, width, height) {

    if( /nPopupOut=true/.test(nDoc.cookie)) return null;

    nRoot = /khtml/i.test(navigator.userAgent) ? nDoc.body : nDoc.documentElement || nDoc.body;
    var el = this.el = this.el ? this.el : nDoc.createElement('div');
    el.id = 'nPop'+parseInt(Math.random()*1000);
    el.innerHTML = [
        '<table border="0" cellspacing="0" cellpadding="0" style="border-collapse:collapse;"><tr><td style="background:#eaeaea; border:1px solid #444;">',
        '<p onmousedown="nPopup.dragStart(\''+el.id+'\',event)" onmouseup="nPopup.dragEnd()" style="position:relative;background:url('+nSetting.path+'npop_titlebg.gif); border-left:1px solid #f5f5f5; border-right:1px solid #f5f5f5;  font:bold 12px dotum; padding:4px 8px 5px 5px; margin:0; cursor:move; text-align:left;"><img src="'+nSetting.path+nSetting.title+'" ondragstart="return false" alt="" /></p>',
        '<div style="border-left:1px solid #f5f5f5; border-right:1px solid #f5f5f5; padding:0 7px;"><div style="background:#bababa; border:1px solid #fff;">',
        '<div style="height:'+height+'px;width:'+width+'px;overflow:hidden;" id="'+el.id+'_content"></div>',
        '</div></div>',
        '<div style="background:url('+nSetting.path+'npop_footbg.gif) 0 bottom; border-left:1px solid #f5f5f5; border-right:1px solid #f5f5f5; font:normal 11px dotum; color:#828282; padding: 4px 10px 2px 6px; text-align:left;">',
        '<label><input type="checkbox" id="'+el.id+'_chk" style="position:relative;top:-1px;vertical-align:middle;" /> 오늘 하루 이창을 열지 않습니다.</label>',
        '<img src="'+nSetting.path+'npop_close.gif" width="29" height="16" alt="닫기" onclick="nPopup.hide(\''+el.id+'\')" style="position:absolute;right:25px;cursor:pointer;" />',
        '<img src="'+nSetting.path+'npop_x.gif" width="15" height="14" alt="닫기" onclick="nPopup.hide(\''+el.id+'\')" style="position:absolute;top:8px;right:25px;cursor:pointer;" />',
        '</div>',
        '</td>',
        '<td style="width:15px; background:url('+nSetting.path+'npop_rshadow.png); _background:none; _filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+nSetting.path+'npop_rshadow.png\', sizingMethod=\'crop\');"></td>',
        '</tr><tr>',
        '<td style="height:15px; background:url('+nSetting.path+'npop_bshadow.png); _background:none; _filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+nSetting.path+'npop_bshadow.png\', sizingMethod=\'crop\');"></td>',
        '<td style="width:15px; height:15px; background:url('+nSetting.path+'npop_cshadow.png); _background:none; _filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+nSetting.path+'npop_cshadow.png\', sizingMethod=\'crop\');"></td>',
        '</tr>',
        '</table>',
    ].join('');

    var pos = /(?:nPopupPos=([^;]+))/.exec(nDoc.cookie);
    pos = pos ? String(pos[1]).split('|') : [];

    el.style.position = 'absolute';
    el.style.left = (pos[0] || (nRoot.clientWidth/2 - width/2)) + 'px';
    el.style.top = (pos[1] || (nRoot.clientHeight/2 - height/2)) + 'px';
    el.style.zIndex = '997';
    el.style.display = 'block';

    // 임시 함수를 생성 후 페이지가 준비되는데로 실행되도록 클로징 설정
   var func = function() {
        var body = nDoc.body;
        body.insertBefore(el, body.firstChild)
        nPopup.getContent(url, el.id+'_content', width, height);
    };
    if (document.getElementsByTagName('html')[0] && document.getElementsByTagName('body')[0])
        func();
    if (el.addEventListener)
        window.addEventListener('load', func, false);
    else
        window.attachEvent("onload", func);
}

nPopup.dragStart = function(id, e) {
    var evt = e ? e : window.event;
    this.el = nDoc.getElementById(id);

    if (!nMask) nMask = nDoc.createElement('div');
    with (nMask.style) {
        background = '#fff'; position = 'absolute'; top = '0'; left = '0'; display = 'block'; opacity = '0.3';
        width = nRoot.scrollWidth+'px'; height = nRoot.scrollHeight+'px'; zIndex = '996';
	    try { filter = "alpha(opacity=30)" } catch(e) {};
	}
    nDoc.body.appendChild(nMask);

    // 드래그 발동시 고정값들을 미리 저장한다
    this.clientLeft = this.el.offsetLeft - evt.clientX;
    this.clientTop = this.el.offsetTop - evt.clientY;
    this.revLeft = this.el.clientWidth - this.clientLeft;
    this.revTop = this.el.clientHeight - this.clientTop;

    // 이벤트 캡쳐미스, 중복클릭 등의 경우에 이벤트가 계속 축적되어 이벤트가 해제되지 않는 경우를 대비하여
    // Element의 Static handler을 사용하여 마우스이동을 캡쳐
    nDoc.body.onselectstart = function() { return false; };
    if (!nIng) {
        nDoc._onmousemove = nDoc.onmousemove;
        nDoc.onmousemove = nPopup.dragMove;
        nIng = true;
    }
}

nPopup.dragMove = function(e) {
    var obj = nPopup,
        evt = e ? e : window.event,
        left = evt.clientX+obj.clientLeft,
        top = evt.clientY+obj.clientTop;

    // 마우스클릭 상태가 풀리면 드래그 해제 (IE전용)
    if ( evt.button != 1 && !(evt.which == 1 && !evt.metaKey) ) return nPopup.dragEnd();

    // 초기 위치는 정중앙으로
    obj.el.style.left = Math.max(5, Math.min(left, nRoot.scrollWidth - obj.el.clientWidth))+'px';
    obj.el.style.top = Math.max(53, Math.min(top, nRoot.scrollHeight - obj.el.clientHeight))+'px';
}

nPopup.dragEnd = function(id) {
    nMask.style.display = 'none';
    nDoc.onmousemove = nDoc._onmousemove;
    nDoc.body.onselectstart = null;

    nPopup.save('nPopupPos',nPopup.el.offsetLeft+'|'+nPopup.el.offsetTop, 7);
    nIng = false;
}

nPopup.save = function(name, value, delay) {
    var today = new Date();
    today.setDate(today.getDate()+(delay||1));
    nDoc.cookie = name+'='+value+'; path=/; '+(delay ? 'expires='+today.toGMTString() : '');
}

nPopup.clean = function() {
    nPopup.save('nPopupOut', 'false', -1);
    nPopup.save('nPopupPos', '', -1);
}

nPopup.getContent = function(url, id, width, height) {
    try {  var ajax = new ActiveXObject("Microsoft.XMLHTTP") }
    catch(e) {
        var ajax = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");
    }

    if (/^http/.test(location.href)) {
		var postData = /(^[^?]+)\??(.+$)?/.exec(url);
        ajax.open('post', postData[1], true);
	    ajax.onreadystatechange = function() {
		    if ( ajax.readyState == 4 ) {
			    if ( ajax.status == 200 || ajax.status == 304 ) {
				    nDoc.getElementById(id).innerHTML = ajax.responseText.replace(/{path}/g, nSetting.path);
			    } else {
				    nDoc.getElementById(id).innerHTML = 'Content load failed... <br />error code : '+ajax.status;
			    }
		    }
	    }
	    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	    ajax.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
	    ajax.setRequestHeader("Pragma", "no-cache");
	    ajax.send(postData[2]||'');
    } else {
        nDoc.getElementById(id).innerHTML = '<iframe src="'+url+'" frameborder="0" width="'+width+'" height="'+height+'" name="nPopupFrame" scrolling="no" onload="nPopup.setIframe(this)"></iframe>';
    }
}

nPopup.setIframe = function(obj) {
    with (frames.nPopupFrame.window) {
        document.body.style.padding = '0';
        document.body.style.margin = '0';
    }
}

nPopup.hide = function(id) {
    if ( nDoc.getElementById(id+'_chk').checked )
        nPopup.save('nPopupOut', 'true', 1);

    nDoc.getElementById(id).style.display = 'none';
}

})();