//---------------------------------------------------------------------------------------------------
//get url file
// ¼öÁ¤ : ÆË¾÷ ³ëÃâ½Ã°£ ¼³Á¤ ±â´É Ãß°¡..(2009/02/26 wjk)
//---------------------------------------------------------------------------------------------------


var virtual;
virtual = false;

if(window.XMLHttpRequest && !(window.ActiveXObject)) {
	try {
		virtual = new XMLHttpRequest();
	} catch(e) {
		virtual = false;
	}
} else if(window.ActiveXObject) {
	try {
		virtual = new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e) {
		try {
			virtual = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(e) {
			virtual = false;
		}
	}
}
//alert(virtual);

//var virtual = new ActiveXObject("Microsoft.XMLHTTP");
var _httpMethod = "GET";  // POST ·Î ÇÏ¸é, .js ÆÄÀÏÀº ¸ø ÀÐ´Â´Ù.

var _syncFlag = false;

function xmlGetDataLayer (remoteURL)
{
	try {
		virtual.open(_httpMethod,remoteURL, _syncFlag); // post/get/put/etc optional
		virtual.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
		//virtual.setRequestHeader("Content-type:", "text/html; charset=euc-kr");
		//virtual.setRequestHeader("Referer:", remoteURL);
		virtual.send(null);
		return virtual.responseText;
	} catch(e) {
		alert(e);
		//alert((e.number&0xFFFF)+"||"+e.description);
	}
}


function getURLFile(remoteURL)
{
	return xmlGetDataLayer(remoteURL);
}


//---------------------------------------------------------------------------------------------------
//ÆË¾÷ ·¹ÀÌ¾î·Î ¶ç¿ì±â Ãß°¡
//---------------------------------------------------------------------------------------------------
var LayerPopupTop=0;
var LayerPopupLeft=0;

var expIdMainPopup = false;

function popupOpenDiv(pCategory){
	strhostname = location.host;
	currentDomain = strhostname.substring(strhostname.indexOf(".")+1);
		
	//if(currentDomain != "mgame.com"){
	//	return;
	//}

	//return alert(pCategory);
	try{
	if (pCategory == "www_guest")
	{
		expIdMainPopup = true;
	}
	
	// ¿¥°ÔÀÓ ¸ÞÀÎ°ú ÅëÇÕ.. : wjk(20090210) #######
	if(pCategory == "myinfo") {
		var pFilePath = "http://"+location.hostname+"/popup/mainPopup/split/"+pCategory+".xml";
	} else if(pCategory == "www" || pCategory == "www_guest") {
		var pFilePath = "http://"+location.hostname+"/popup/allPopup/split/"+pCategory+".xml";
	} else {
		var pFilePath = "http://"+location.hostname+"/common_popup/"+pCategory+"/"+pCategory+".xml";
	}
	//var pFilePath = "http://"+location.hostname+"/common_popup/"+pCategory+"/"+pCategory+".xml";
	
	// ############################################

	//alert(pFilePath);

	var pStrFile  = unescape(getURLFile(pFilePath));
	
	var pPopupVar = pStrFile.split("||");
	var pPopupNum = parseInt(pPopupVar[0]);
	
	if(pPopupNum != 0){
		var pAge = parseInt(getCookie("MGAME", "AGE"));
		var pUpper	= pPopupNum;
		var pLower	= 1;
		var pRnd	= Math.random();
		var pRok	= Math.floor((pUpper - pLower + 1) * pRnd + pLower);
		var tmpLoop;
		var pCookieCheck;
		var pCookieName,pURL,pHeight,pWidth,pLogin,pAdult;
		var pPopupView = 0;
		var pPopupCheck = 0;
		for(tmpLoop = 1; tmpLoop <= pPopupNum; tmpLoop++){
			pCookieName		= pPopupVar[1 + 9 * (tmpLoop - 1)];
			pURL			= pPopupVar[2 + 9 * (tmpLoop - 1)];
			pHeight			= pPopupVar[3 + 9 * (tmpLoop - 1)];
			pWidth			= pPopupVar[4 + 9 * (tmpLoop - 1)];
			pAdult			= parseInt(pPopupVar[5 +9 * (tmpLoop - 1)]);
			pCookieCheck	= getCookie_1DIM(pCookieName);
			LayerPopupTop	= pPopupVar[6 + 9 * (tmpLoop - 1)];
			LayerPopupLeft	= pPopupVar[7 + 9 * (tmpLoop - 1)];
			pStartDate		= pPopupVar[8 + 9 * (tmpLoop - 1)];		// ÆË¾÷ ½ÃÀÛ½Ã°£..
			pEndDate		= pPopupVar[9 + 9 * (tmpLoop - 1)];		// ÆË¾÷ Á¾·á½Ã°£..
			pURL			= pURL+"?timestamp="+new Date().getTime();
			
			//### ÇöÀç ½Ã°£ ½ºÆ®¸µº¯¼ö Á¤ÀÇ.. ###############################
			var date = new Date();

			var year  = date.getFullYear();
			var month = date.getMonth() + 1; // 1¿ù=0,12¿ù=11ÀÌ¹Ç·Î 1 ´õÇÔ
			var day   = date.getDate();
			var hour  = date.getHours();
			var min   = date.getMinutes();

			if (("" + month).length == 1) { month = "0" + month; }
			if (("" + day).length   == 1) { day   = "0" + day;   }
			if (("" + hour).length  == 1) { hour  = "0" + hour;  }
			if (("" + min).length   == 1) { min   = "0" + min;   }
			
			//var curDate = year+month+day+hour+min;
			var curDate = String(year)+String(month)+String(day)+String(hour)+String(min);
			
		
			//####################################################

			var arrVerStr = navigator['userAgent'].split('; ');
			if( arrVerStr.length >= 4 ) {
				var arrIEVer = arrVerStr[1].split(' ');
				if( arrIEVer[1] >= 7.0){
					pHeight		= pHeight -10;
					pWidth		= pWidth -5;
				}
			}
			if(pAdult == 0){
				pPopupCheck = tmpLoop;
			}
			
			//### ÇöÀç½Ã°£ÀÌ ½ÃÀÛ½Ã°£ ~ Á¾·á½Ã°£ »çÀÌ¿¡ ÀÖÀ»°æ¿ì¿¡¸¸ ³ëÃâ..##########

			if(pRok == tmpLoop){
				if((pAdult > 0 && getCookie("MGAME", "") != null) || pAdult == 0){
					if((pAdult == 2 && pAge >= 19) || pAdult == 1 || pAdult == 0){
						if (pCookieCheck == null || pCookieCheck == "undefined"){

							if(pStartDate != "" && pEndDate != "") {
								if(curDate >= pStartDate && curDate < pEndDate) {
									popupDialogDiv(pURL,pHeight,pWidth);
									pPopupView = 1;
									break;
								}
							} else {
								popupDialogDiv(pURL,pHeight,pWidth);
								pPopupView = 1;
								break;
							}

						}
					}
				}
			}

			if(tmpLoop == pPopupNum && pPopupView == 0 && pPopupCheck !=0){
				pCookieName		= pPopupVar[1 + 9 * (pPopupCheck - 1)];
				pURL			= pPopupVar[2 + 9 * (pPopupCheck - 1)];
				pHeight			= pPopupVar[3 + 9 * (pPopupCheck - 1)];
				pWidth			= pPopupVar[4 + 9 * (pPopupCheck - 1)];
				pCookieCheck	= getCookie_1DIM(pCookieName);
				LayerPopupTop	= pPopupVar[6 + 9 * (pPopupCheck - 1)];
				LayerPopupLeft	= pPopupVar[7 + 9 * (pPopupCheck - 1)];
				pStartDate		= pPopupVar[8 + 9 * (tmpLoop - 1)];
				pEndDate		= pPopupVar[9 + 9 * (tmpLoop - 1)];

				if (pCookieCheck == null || pCookieCheck == "undefined"){
					if(pStartDate != "" && pEndDate != "") {
						if(curDate >= pStartDate && curDate < pEndDate) {
							popupDialogDiv(pURL,pHeight,pWidth);
							pPopupView = 1;
							break;
						}
					} else {
						popupDialogDiv(pURL,pHeight,pWidth);
						pPopupView = 1;
						break;
					}
				}
			}
			// #########################################################################
		}
	}
	return;
	}catch(e){
		alert(e);
	}
}

function popupDialogDiv(pURL,pHeight,pWidth){

	try{
		var x = 10;
		var y = 10;
		if (isOverXPSP2() == 0){var xx = pWidth; var yy = pHeight;}else{var xx = pWidth; var yy = parseInt(pHeight)+15;}
		openPopupDiv(pURL,pHeight,pWidth);
		return;
	}catch(e){
	}
}

//-----------------------------------------------------------------------------------
//ÆË¾÷ ³»¿ë¾ÈÀÇ ÇÔ¼ö ------------------------------------------------------------------

//¼³Á¤ÇÑ ³¯Â¥¸¸Å­ ÄíÅ°°¡ À¯ÁöµÇ°Ô. expiredays°¡ 1ÀÌ¸é ÇÏ·çµ¿¾È À¯Áö
function setCookiePop(name, value, expiredays){
	var expire_date = new Date();
	expire_date.setDate(expire_date.getDate() + expiredays );
	if ( location.host.indexOf("buddybuddy.co.kr") > 0 ){  //BUDDYBUDDY
		document.cookie = name + "=" + escape( value ) + "; expires=" + expire_date.toGMTString() + "; domain=.buddybuddy.co.kr; path=/";	
	}else if ( location.host.indexOf("gameon.nate.com") > 0 ){  //³×ÀÌÆ®¿Â Ã¤³Î¸µ
		document.cookie = name + "=" + escape( value ) + "; expires=" + expire_date.toGMTString() + "; domain=.gameon.nate.com; path=/";
	}else if ( location.host.indexOf("gameon.nate.com") > 0 ){  //´ÙÀ½ Ã¤³Î¸µ
		document.cookie = name + "=" + escape( value ) + "; expires=" + expire_date.toGMTString() + "; domain=.game.daum.net; path=/";
	} else {
		document.cookie = name + "=" + escape( value ) + "; expires=" + expire_date.toGMTString() + "; domain=.mgame.com; path=/";
	}

}
//ÄíÅ° ¼Ò¸ê ÇÔ¼ö
function clearCookiePop(name) {
	var expire_date = new Date();
	//¾îÁ¦ ³¯Â¥¸¦ ÄíÅ° ¼Ò¸ê ³¯Â¥·Î ¼³Á¤ÇÑ´Ù.
	expire_date.setDate(expire_date.getDate() - 1);
	if ( location.host.indexOf("buddybuddy.co.kr") > 0 ){  //BUDDYBUDDY
		document.cookie = name + "= " + "; 	expires=" + expire_date.toGMTString() + ";	domain=.buddybuddy.co.kr; path=/";	
	}else if ( location.host.indexOf("gameon.nate.com") > 0 ){  //³×ÀÌÆ®¿Â Ã¤³Î¸µ
		document.cookie = name + "= " + "; 	expires=" + expire_date.toGMTString() + ";	domain=.gameon.nate.com; path=/";
	}else if ( location.host.indexOf("gameon.nate.com") > 0 ){  //´ÙÀ½ Ã¤³Î¸µ
		document.cookie = name + "= " + "; 	expires=" + expire_date.toGMTString() + ";	domain=.game.daum.net; path=/";	
	} else {
		document.cookie = name + "= " + "; 	expires=" + expire_date.toGMTString() + ";	domain=.mgame.com; path=/";
	}
}
//¿À´ÃÀÌÃ¢
function controlCookiePop(elemnt,cookname) {
	if (elemnt.checked) {
		//Ã¼Å© ¹Ú½º¸¦ ¼±ÅÃÇßÀ» °æ¿ì ÄíÅ° »ý¼º ÇÔ¼ö È£Ãâ
		setCookiePop(cookname,"true", 1);
		openPopupHidden();
		}else{
		clearCookiePop(cookname);
	}
	return;
}
//ÆË¾÷¾ÈÀÇ ¸µÅ©
function lhref(url) {
	top.location.href=url;
}

//ÆË¾÷ ´Ý±â
function openPopupHidden(){
	document. getElementById("divPopup").style.display='none';
	document. getElementById("divPopup").innerHTML='';
}
//-----------------------------------------------------------------------------------
var pophtml;
function createXMLHttpRequest() {
	var xmlHttp;
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
	return xmlHttp;
}

function startRequestPopup(pURL) {
		var xmlHttp = createXMLHttpRequest();
		xmlHttp.open("GET", pURL, true);
		xmlHttp.send(null);
		//xmlHttp.onreadystatechange = handleStateChangePopup;
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4){
				if(xmlHttp.status == 200){
					var html = PopupXmlShow(xmlHttp);
				}
			}
		}

}

function PopupXmlShow(xmlHttp){
	try{
		var xmlData = xmlHttp.responseXML;
		var Popupdata = xmlData.getElementsByTagName("data");
		var PopupdataLength = Popupdata.length;
		 pophtml = Popupdata[0].childNodes[0].nodeValue;
	}catch(e){
	}
}

//ÇØ´õ ¾ø´Â ÆË¾÷
function openPopupDiv(pURL,pHeight,pWidth) {
	try{
		var showDiv = "";
//		var pFilePath = "http://"+location.hostname+"/popup/allPopup/070327_wffm.xml";l
		startRequestPopup(pURL);
		setTimeout('openPopupDivShow()', 500);
	}catch(e){
		//alert(e.discription);
	}
}
function openPopupDivShow(){
	try{
		if (expIdMainPopup == true)
		{
			LayerPopupTop = 0;
			LayerPopupLeft = 0;
			//LayerPopupLeft = window.screen.width-440;
		}

		if(pophtml && pophtml != "" && pophtml != "undefined"){
			if (document.getElementById("divPopup")){
				document.getElementById("divPopup").innerHTML = pophtml ;
				document.getElementById("divPopup").style.top = LayerPopupTop + "px";
				document.getElementById("divPopup").style.left = LayerPopupLeft + "px";
				document.getElementById("divPopup").style.display = "block";
			}
		}
	}catch(e){

	}
}



//·¹ÀÌ¾î µå·¹±×

isIE=document.all;
isNN=!document.all&&document.getElementById;
isN4=document.layers;
isHot=false;

function ddInit(e){
	try{
		topDog=isIE ? "BODY" : "HTML";
		whichDog=isIE ? document.all.divPopup : document.getElementById("divPopup");
		hotDog=isIE ? event.srcElement : e.target;

		while (hotDog.id!="divPopup"&&hotDog.tagName!=topDog){
			hotDog=isIE ? hotDog.parentElement : hotDog.parentNode;
		}
		if (hotDog.id=="divPopup"){
			offsetx=isIE ? event.clientX : e.clientX;
			offsety=isIE ? event.clientY : e.clientY;
			nowX=parseInt(whichDog.style.left);
			nowY=parseInt(whichDog.style.top);
			ddEnabled=true;
			document.onmousemove=dd;
		}

	}catch(e){
		//alert(e.descrption);
	}
}

function dd(e){
	if (!ddEnabled) return;
	whichDog.style.left=isIE ? nowX+event.clientX-offsetx : nowX+e.clientX-offsetx + "px";
	whichDog.style.top=isIE ? nowY+event.clientY-offsety : nowY+e.clientY-offsety + "px";
	return false;
}

document.onmousedown=ddInit;
document.onmouseup=Function("ddEnabled=false");