﻿
/// Cookie 부분
function goToReg()
{   
    if (escape(getCookie("IMBCMAIN")) == "") 
    {
        ClearCookie('catchRobberCnt');
        ClearCookie('catchRobber');
        ClearCookie('catchRobberFirstTime');
        
        
        SetUrl();
        window.location.href="http://login.imbc.com/imbc/Login/Login.asp";
    }
}


if (escape(getCookie("IMBCMAIN")) == "") 
{
    ClearCookie('catchRobberCnt');
    ClearCookie('catchRobber');
    ClearCookie('catchRobberFirstTime');
    
    setCookie("catchRobberCnt", -1 ,null,"/","imbc.com", false)
    setCookie("catchRobber", -1 ,null,"/","imbc.com", false)
    setCookie("catchRobberFirstTime", -1 ,null,"/","imbc.com", false)
    
}

if (!isSetCookies)
{
    goToReg();
}

function SetUrl() 
{
    setCookie("IMBCURL", location.href ,null,"/","imbc.com", false)

}

function SetCookies()
{
    if (escape(getCookie("catchRobberCnt")) != '-1') 
    {
        alert('이미 이벤트에 참여중이십니다.');
    }
    setCookie("catchRobberCnt", 0 ,null,"/","imbc.com", false)
    setCookie("catchRobber", '0000000000' ,null,"/","imbc.com", false)
}

function ClearCookie(name)	
{
   var expireDate = new Date();
    expireDate.setDate( expireDate.getDate() - 1 );
    document.cookie = name + "= " + "; expires=" + expireDate.toGMTString() + "; path=/";
 }


function setCookie(strName, strValue, dateExpires, strPath, strDomain, isSecure)
{
    var strCookie;
    if(strName == "") return ;

    strCookie = strName + "=" + escape(strValue) + 
		           ((dateExpires) ? "; expires=" + dateExpires.toGMTString() : "")  + 
		           ((strPath)     ? "; path="    + strPath : "") + 
  	           ((strDomain)   ? "; domain="  + strDomain : "") + 
		           ((isSecure)    ? "; secure" : "");

    document.cookie = strCookie; 
}

// 쿠키정보 가져오기
function getCookie(strName)
{
    var strArg = new String(strName + "=");	
    var nArgLen, nCookieLen, nEnd;
    var i = 0, j;

    nArgLen    = strArg.length;
    nCookieLen = document.cookie.length;

    if(nCookieLen > 0) {
        while(i < nCookieLen) {
	        j = i + nArgLen;
	        if(document.cookie.substring(i, j) == strArg) {
		        nEnd = document.cookie.indexOf (";", j);
		        if(nEnd == -1) nEnd = document.cookie.length;
		        return unescape(document.cookie.substring(j, nEnd));
	        }
	        i = document.cookie.indexOf(" ", i) + 1;
	        if (i == 0) break;
        }
    }
    return("");
}



/// 도굴꾼 잡기
function catchRobber(num)
{
    goToReg();
    
    if (getCookie('catchRobber').substr(eval(num-1),1) == 0)
    {
        try{
            if (getCookie('catchRobberCnt') == 0)
            {
                var today =  new Date();
                var dateString = today.getFullYear() + '-' + eval(today.getMonth() +1 ) + '-' + today.getDate() + 'd' + today.getHours() +'-' + today.getMinutes()+'-' + today.getSeconds();
                setCookie('catchRobberFirstTime', dateString , null, '/' , 'imbc.com', false)
                //alert('이벤트 참여가 시작되었습니다.');
            }
            setCookie('catchRobberCnt', eval(getCookie('catchRobberCnt')) +1  , null, '/' , 'imbc.com', false)
            setCookie('catchRobber', getCookie('catchRobber').substr(0, eval(num-1)) + '1' + getCookie('catchRobber').substr(num,10) , null ,'/', 'imbc.com', false)
            
            var win = window.open('http://eventi.imbc.com/catchrobber/catchRobberJoin.aspx', 'catchRobber', 'width=430px, height=265px');
            win.location.href = 'http://eventi.imbc.com/catchrobber/catchRobberJoin.aspx';
            win.focus();
         
        }
        catch(e)
        {};
        
    }
    else if (getCookie('catchRobberCnt')  == 10)
    {
         var win = window.open('http://eventi.imbc.com/catchrobber/catchRobberJoin.aspx', 'catchRobber', 'width=430px, height=265px');
         win.location.href = 'http://eventi.imbc.com/catchrobber/catchRobberJoin.aspx';
         win.focus();
    }
    else
    {
        var win = window.open('http://eventi.imbc.com/catchrobber/catchRobberJoin.aspx?isCatched=yes', 'catchRobber', 'width=430px, height=265px');
        win.location.href = 'http://eventi.imbc.com/catchrobber/catchRobberJoin.aspx?isCatched=yes';
        win.focus();
        //alert('이미 잡은 도굴꾼입니다.');
    }
}


function isSetCookies()
{
    if (escape(getCookie("IMBCMAIN")) == "")
    {
        return false;
    }
    
    if (getCookie('catchRobberCnt') !='' && getCookie('catchRobberCnt') != "-1")
    {
        return true;
    }
    
    return false;
}

/// 잡은 도굴꾼 보여주기
function modal()
{
    document.body.style.backgroundColor = 'Gray';
    document.body.style.filter = 'alpha(opacity=70)';
    document.body.style.opacity = '0.7';
    
    
    document.getElementById('popupDiv').style.visibility = 'visible';
    document.getElementById('popupDiv').style.height = '300px';
    document.getElementById('popupDiv').style.width = '250px';
    document.getElementById('popupDiv').style.left = eval(document.body.clientWidth/2 -  popupDiv.style.posWidth/2);
    document.getElementById('popupDiv').style.top = eval(document.documentElement.scrollTop);
}


// 잡은 도굴꾼 보여주는 페이지 닫기
function close()
{
     document.body.style.backgroundColor = 'Transparent';
     document.getElementById('popupDiv').style.visibility = 'hidden';
}


function createRobber(num)
{
    if (isSetCookies())
    {
        var time =  new Date();
        
        document.write('<div style="position:absolute;left:' + eval(50+ (Math.floor(Math.random()*500))) + 'px; top:' + 
            eval(100+ (Math.floor(Math.random() * 700))) + 'px"><img src="http://eventi.imbc.com/catchrobber/image/cha_05.gif" onclick="javascript:catchRobber(' + num + ');" /></div>');
        
    }
}