﻿/* ========================= 팝업 제어 관련 공통변수(수정불가) =================== */

var isPopup = false;            //팝업창 존재여부
var interval = "";              //setInterval 해제를 위한 레퍼런스 변수
var cx = 0;                     //드래그 x축
var cy = 0;                     //드래그 y축
var dragObj = null;             //드래그 대상 팝업창

/* =============================================================================== */

///////////////DIV 팝업창 출력
function ShowPopup()
{
    //레이어 팝업창 생성
    for(var i=0; i<popupLen; i++) {
        var popBody = CreatePopBody(i);         //팝업몸체
        with(popBody) {
            appendChild(CreatePopTitle(i));     //팝업제목
            appendChild(CreatePopContent(i));   //팝업내용
            appendChild(CreatePopBottom(i));    //팝업하단
        }
        var popDiv = document.getElementById("popDiv");
        popDiv.appendChild(popBody);
        
        var popTitle_Body = document.getElementById("popTitle_" + i);
        //쿠키 설정된 팝업일 경우(오늘하루 창 보이지않기)
        if(GetCookie(popupName+"_"+i) == "done")    popBody.style.display = "none";
        //팝업 미리보기 일 경우
        if(isPreview)   popBody.style.display = "block";
    }
    //팝업 스크롤 기능이 설정된 경우
    if(isScroll) SetScroll();
    //팝업 드래그 기능이 설정된 경우
    if(isDrag) SetDrag();
}

/////////////////팝업 제목 생성
function CreatePopTitle(num)
{
    var popTitle = document.createElement("div");
    with(popTitle.style) {
        width = p_width[num];
        height = 23;
        background = titleImg;
    }
    
    var oTable = document.createElement("table");
    var newCell = oTable.insertRow().insertCell();
    newCell.innerHTML = "<table border=\"0\" width=\"100%\"><tr><td width=\"45\"></td><td><span style=\"color:" + fColorTop + "; font-weight:" + fweightTop +"; font-size:" + fSizeTop + "; font-family:" + fFamilyTop + "\">"
        + title[num]
        + "</span></td><td width=\"45px\"><div style=\"width:100%; z-index:1;\"><a href=\"javascript:CloseWin('"+num+"')\"><img src=\"/images/common/btn/btn_closed5.gif\"></a></div></td></tr></table>";
    newCell.align = "center";
    
    with(oTable) {
        width = "100%";
        height = "100%";
    }
    
    var popTitle_Body = "<div id=\"popTitle_" + num + "\" class=\"move\" style=\"position:absolute; cursor:move; width:" + (parseInt(p_width[num]) - 45) + "; height:30px; background:#ffffff; filter:alpha(opacity=1);\"></div>";
    
    popTitle.innerHTML = popTitle_Body + oTable.outerHTML;
    return popTitle;
}

///////////////팝업 하단부 생성
function CreatePopBottom(num)
{
    var popBottom = document.createElement("div");
    with(popBottom) {
        id = "popBottom_"+num;
    }
    
    with(popBottom.style) {
        background = bottomColor;
        width = p_width[num];
    }
    
    var oInput = document.createElement("input");
    with(oInput) {
        type = "checkbox";
        id = "chk_close_"+num;
        onclick = function() {
            CloseWin(num);
        }
    }
	with(oInput.style) {
		border = "none";
	}

    
    var oA = document.createElement("a");
    with(oA) {
        href = "javascript:CloseWin('"+num+"')";
        innerHTML = "<img src=\"/images/common/btn/btn_closed5_.gif\">";
    }
    
    var oTable = document.createElement("table");
    oTable.width = "100%";
    oTable.insertRow();
    with(oTable.rows[0]) {
        insertCell().width = 12;
        
        var newCell = insertCell();
        newCell.appendChild(oInput);
        newCell.style.width = 10;
        
        var newCell2 = insertCell();
        newCell2.align = "left";
        newCell2.innerHTML = "<span style=\"color:" + fColorBottom + "; font-size:" + fSizeBottom + "; font-family:" + fFamilyBottom + "\">"
        + "오늘 하루 열지 않기"
        + "</span>";
        
        var newCell3 = insertCell();
        newCell3.appendChild(oA);
        newCell3.align = "right";
        
        insertCell().width = 10;
    }
    
    var oTable2 = document.createElement("table");
    with(oTable2) {
        insertRow().insertCell().appendChild(oTable);
        border = "0";
        height = "100%";
        width = "100%";
    }
    with(oTable2.rows[0].cells[0]) {
        id = num;
    }
    
    popBottom.appendChild(oTable2);
    
    //마우스 이벤트시 정확한 아이디 값 인식을 위한 레이어
    var topDiv = document.createElement("div");
    topDiv.id = "";
    popBottom.appendChild(topDiv);
    return popBottom;
}

////////////팝업 내용 생성
function CreatePopContent(num)
{
    var popContent = document.createElement("div");
    with(popContent) {
        id = "popContent_"+num;
    }
    with(popContent.style) {
        background = contentColor;
        width = parseInt(p_width[num]);
        height = parseInt(p_height[num]);
    }
   
    var html = ""; 
    //내용타입 설정(이미지)
    if(content_type[num] == "1") {
        if(img_url[num] != "") {
            html = "<a href=\"" + img_url[num] + "\" target=\"_blank\">"
                + "<img src=\"" + img_file[num] + "\">"
                + "</a>";
        } else {
            html = "<img src=\"" + img_file[num] + "\">";
        }
    }
    //내용타입 설정(직접편집)
    if(content_type[num] == "2") {
        html = content[num];
    }
    popContent.innerHTML = html;
    return popContent;
}

/////////////팝업 몸체 생성
function CreatePopBody(num)
{
    var popBody = document.createElement("div");
    with(popBody) {
        id = popupName+"_"+num;
    }
    with(popBody.style) {
        position = "absolute";
        border = "solid 2px "+bdColor;
        zIndex = 50+num;
        
        //노출위치(직접지정)
        if(pos_type[num] == "1") {
            left = pos_left[num];
            top = pos_top[num];
        }
        
        //노출위치(간단히지정) - ※참고:스크롤바 두께:20px
        if(pos_type[num] == "2") {
            //팝업 크기가 클라이언트 웹 브라우저 높이 해상도보다 클 경우
            if(document.body.clientHeight < parseInt(p_height[num])) {
                top = 0;
            }
            else {
                top = (document.body.clientHeight/2) - (p_height[num]/2) - 40;
            }
            
            switch(pos_fix[num]) {
            case "L" :  //왼쪽정렬
                left = 0; break;
            case "M" :  //가운데정렬
                left = (document.body.clientWidth/2) - (p_width[num]/2); break;
            case "R" :  //오른쪽정렬(4:border pixel 값에 따른 차감수치 [ex]1px=2)
                left = (document.body.clientWidth-p_width[num]) - 4; break;
            default :   //기본 왼쪽정렬
                left = 0; break;
            }
        }
    }
    eval(
    "popBody.onmousedown = function() {"
        +"PopupChSort("+num+");"
    +"};"
    );
    return popBody;
}

//////////스크롤 기능 설정
function SetScroll()
{
    //팝업 존재여부 체크
    CookieChk();
    //팝업이 존재한다면 스크롤바 위치에 따라 움직이게 함
    if(isPopup) {
        var dElement = document.getElementById("dElement");
        //스크롤바 이전 위치값 저장을 위한 hidden 개체 생성
        for(var i=0; i<popupLen; i++) {
            var preTop = document.createElement("input");
            preTop.type = "hidden";
            preTop.id = "prevScrollTop_" + i;
            preTop.value = 0;
            //dElement.innerHTML += pre.outerHTML;
            dElement.appendChild(preTop);
            
            var preLeft = document.createElement("input");
            preLeft.type = "hidden";
            preLeft.id = "prevScrollLeft_" + i;
            preLeft.value = 0;
            dElement.appendChild(preLeft);
        }
        interval = window.setInterval("PopupMove()", 100);
    }
}

///////////팝업 드래그 기능 설정
function SetDrag()
{
    //팝업 존재여부 체크
    CookieChk();
    //팝업이 존재한다면 드래그 기능 설정
    if(isPopup) {
        document.body.onmousedown = function() { PopupDrag(); };
        document.body.onmouseup = function() { PopupDrag(); };
        document.body.onmousemove = function() { PopupDrag(); };
    }
}

//////////팝업창 존재여부 체크
function CheckPopup()
{
    var cnt = popupLen;
    for(var i=0; i<popupLen; i++) {
        var popupDisplay = document.getElementById(popupName+"_"+i).style.display;
        if(popupDisplay == "none") {
            cnt--;
        }
    }
    
    //팝업이 없을경우 setInterval 설정해제
    if(cnt == 0)    return false;
    else            return true;
}

//////////선택된 팝업창 최상위로 이동
function PopupChSort(num)
{
    var selPopup = document.getElementById(popupName+"_"+num);  //선택된 팝업창
    
    //최상위의 팝업창 찾기
    var maxIdx = 0;
    var maxPopup = "";
    for(var i=0; i<popupLen; i++) {
        var targetPopup = document.getElementById(popupName+"_"+i);
        if(maxIdx < targetPopup.style.zIndex) {
            maxIdx = targetPopup.style.zIndex;
            maxPopup = targetPopup;
        }
    }
    
    //최상위 팝업창을 선택했을 경우
    if(selPopup.style.zIndex == maxPopup.style.zIndex)  return;

    //팝업창 재정렬
    var selIdx = selPopup.style.zIndex;
    for(var i=0; i<popupLen; i++) {
        var popupDiv = document.getElementById(popupName+"_"+i);
        if(popupDiv.style.zIndex > selIdx) {
            popupDiv.style.zIndex -= 1;
        }
    }
    selPopup.style.zIndex = maxIdx;
}

///////////팝업창 위치변경(드래그)
function PopupDrag()
{
    var type = event.type;
    if(type == "mousedown") {           //mousedown 이벤트
        //팝업창이 아닌경우
        if(event.srcElement.className != "move")  return;
        
        var arr = event.srcElement.id.split("_");
        var num = arr[1];
        dragObj = document.getElementById(popupName+"_"+num);
        
        cx = event.x - parseInt(dragObj.style.left);
        cy = event.y - parseInt(dragObj.style.top);
        isMouseup = false;
    } else if(type == "mouseup") {      //mouseup 이벤트
        dragObj = null;
    } else if(type == "mousemove") {    //mousemove 이벤트
        if(dragObj != null) {
            dragObj.style.left = event.x - cx;
            dragObj.style.top = event.y - cy;
            
            if(event.y - cy < 0) {
                dragObj.style.top = 0;
            }
        }
    }
}

///////////팝업창 위치변경(스크롤바)
function PopupMove()
{
    //팝업창 존재여부 체크(실패시 setInterval 설정해제)
    if(!CheckPopup()) { creaInterval(interval); return; }
    for(var i=0; i<popupLen; i++) {
        var popupDiv = document.getElementById(popupName+"_"+i);
        var prevScrollTop = document.getElementById("prevScrollTop_"+i);
        var prevScrollLeft = document.getElementById("prevScrollLeft_"+i);
        
        var popupTop = parseInt(popupDiv.style.top.replace(/[^0-9]/g, ""));
        var popupLeft = parseInt(popupDiv.style.left.replace(/[^0-9]/g, ""));
        var scrollTop = parseInt(document.documentElement.scrollTop);
        var scrollLeft = parseInt(document.documentElement.scrollLeft);
        
        //top 이동
        if(scrollTop != prevScrollTop.value) {
            var topGap = scrollTop - parseInt(prevScrollTop.value);
            popupDiv.style.top = popupTop + topGap;
            prevScrollTop.value = scrollTop;
        }
        
        //left 이동
        if(scrollLeft != prevScrollLeft.value) {
            var leftGap = scrollLeft - parseInt(prevScrollLeft.value);
            popupDiv.style.left = popupLeft + leftGap;
            prevScrollLeft.value = scrollLeft;
        }
    }
}

/////////////////쿠키 설정여부 체크(오늘 표시하지 않음)
function CookieChk()
{
    for(var i=0; i<popupLen; i++) {
        if(GetCookie(popupName+"_" + i) != "done") {
            document.getElementById(popupName+"_" + i).style.display ="block";
            isPopup = true;
            break;
        } else {
            document.getElementById(popupName+"_" + i).style.display ="none";
        }
    }
}

///////////////팝업창을 닫음
function CloseWin(num)
{
    var objChk = document.getElementById("chk_close_" + num);
    var objPop = document.getElementById(popupName+"_" + num);
    if(objChk.checked) {
        setCookie(popupName+"_" + num, "done", 1);
    }
    objPop.style.display ="none";
}
