﻿var nowPopupDivID = "div_MakePopup";
var nowTempDivID = "div_MakeTemp";
var nowPopupTitleID = "span_title";
var nowPopupIframeID = "iframe_Popup";
var nowPopupNumber = 0;
var nowParentPage = "";

function MakePopup()
{
    nowPopupNumber = nowPopupNumber + 1;
    
    if (document.getElementById(nowPopupDivID + nowPopupNumber.toString()) != "[object]")
    {
        var makePopup = "<div id='" + nowPopupDivID + nowPopupNumber.toString() + "' name='" + nowPopupDivID + "' style='z-index:2; position:absolute; width:0px; height:0px; display:none; border:solid 1px #2756A0; ' ><table border='0' cellpadding='0' cellspacing='0' ><tr style='background-color:#2756A0' height='30px' ><td onmousedown='MouseDown()' style='position: relative; cursor:move;' class='drag'><span id='" + nowPopupTitleID + nowPopupNumber.toString() + "' style='color:White; font-size:9pt; padding:0 0 0 5;'></span></td><td align='right' style='padding: 0 5 0 0' onmousedown='MouseDown()' class='drag' ><img src='http://image.jump2000.net/j2/button/popupbtn_close.gif' style='cursor:hand' align='absmiddle' onclick='return ClosePopup()' /></td></tr><tr><td colspan='2' style='padding:0px 0px 0px 0px; background-color:White;' ><iframe frameborder='0' id='" + nowPopupIframeID + nowPopupNumber.toString() + "' onload='AutoSizeDialog();' scrolling='auto' style='overflow-x: hidden'></iframe></td></tr></table></div><div id='" + nowTempDivID + nowPopupNumber.toString() + "'></div>"; 
        
        if (nowPopupNumber > 1)
        {
            document.getElementById(nowTempDivID + (nowPopupNumber - 1).toString()).innerHTML += makePopup;
        }
        else
        {
            document.getElementById("div_popup").innerHTML += makePopup;
        }
    }
}

function OpenPopup(page, title, parentLocation)
{   
    MakePopup();
    var divPopup = nowPopupDivID + nowPopupNumber.toString();
    var spanTitle = nowPopupTitleID + nowPopupNumber.toString();
    var iframePopup = nowPopupIframeID + nowPopupNumber.toString();
    
    document.getElementById(spanTitle).innerHTML = "";
    
    if (CheckNull(title))
    {
        document.getElementById(spanTitle).innerHTML = title;
    }
    
    document.getElementById(iframePopup).src = page;
    
    document.getElementById("div_background").style.display = "inline";
    document.getElementById("div_background").style.left = "0";
    document.getElementById("div_background").style.top = "0";
    document.getElementById("div_background").style.height = document.body.scrollHeight;
    document.getElementById("div_background").style.width = document.body.scrollWidth;

    document.getElementById(divPopup).style.display = "inline";

    if (nowPopupNumber > 1) 
    {
        var prevDivPopup = nowPopupDivID + (nowPopupNumber - 1).toString();
        document.getElementById(prevDivPopup).style.zIndex = "0";
    }
    
    AutoMoveProcess();
    
    if (CheckNull(parentLocation))
    {
        nowParentPage = parentLocation;
    }
    
    //return false;
}

function ClosePopup()
{
    var divPopup = nowPopupDivID + nowPopupNumber.toString();
    var spanTitle = nowPopupTitleID + nowPopupNumber.toString();
    var iframePopup = nowPopupIframeID + nowPopupNumber.toString();

    if (CheckNull(document.getElementById(divPopup)))
    {
        document.getElementById(divPopup).style.display = "none";
        document.getElementById(divPopup).style.left = document.body.clientWidth;
        document.getElementById(divPopup).style.top = document.body.clientHeight;

        document.getElementById(iframePopup).src = null;
        
        if (nowPopupNumber > 1) 
        {
            var prevDivPopup = nowPopupDivID + (nowPopupNumber - 1).toString();
            document.getElementById(prevDivPopup).style.zIndex = "2";
        }

        nowPopupNumber = nowPopupNumber - 1;
    
        if (nowPopupNumber == 0)
        {
            document.getElementById("div_background").style.display = "none";
        }
    }
    
    //return false;
}

function PopupOpener(control_id)
{
    var iFrameMain = top.Main;

    var popupOpener = iFrameMain.document.getElementById(control_id);

    var sIFrameMainSrc = iFrameMain.location.toString();
    
    if (sIFrameMainSrc.indexOf(nowParentPage) == -1)
    {
        var ArrIFrames = iFrameMain.document.getElementsByTagName("iframe");

        for (i = 0; i < ArrIFrames.length; i++)
        {
            var sSrc = ArrIFrames[i].src.toString();
            nowParentPage = nowParentPage.toString();
        
            if (sSrc.indexOf(nowParentPage) > -1 || nowParentPage.indexOf(sSrc) > -1)
            {
                popupOpener = iFrameMain.eval(ArrIFrames[i].id).document.getElementById(control_id);
                break;
            }
        }
    }
    
    return popupOpener;
}

function CheckNull(arg)
{
    var fin = true;
    
    switch (arg)
    {
        case undefined : fin = false; break;
        case "" : fin = false; break;
        case null : fin = false; break;
    }
    
    return fin;
}

function AutoSizeDialog()
{
    var divPopup = nowPopupDivID + nowPopupNumber.toString();
    var spanTitle = nowPopupTitleID + nowPopupNumber.toString();
    var iframePopup = nowPopupIframeID + nowPopupNumber.toString();

    if (nowPopupNumber > 0 && CheckNull(eval(iframePopup).dialogTb))
    {   
        var height = eval(iframePopup).dialogTb.clientHeight;
        var width = eval(iframePopup).dialogTb.clientWidth;

        var x = (document.body.clientWidth / 2) - (width / 2);
        var y = (document.body.clientHeight / 2) - (height / 2) - 32;
        
        if (parseInt(y) < 0)
        {
            y = 0;
        }
        
        if (navigator.appVersion.indexOf("MSIE") > -1) {
            document.getElementById(iframePopup).height = height;
            document.getElementById(iframePopup).width = width;
        }
        else {
            document.getElementById(iframePopup).height = height + 20;
            document.getElementById(iframePopup).width = width + 20;
        }
        
        document.getElementById(divPopup).style.left = x;
        document.getElementById(divPopup).style.top = y;
        document.getElementById(divPopup).style.display = "inline";
    }
    
    //return false;
}

var bDown = false;
var x, y;

function MouseDown() 
{
    bDown = true;
    x = event.clientX;
    y = event.clientY;
}

function MouseUp() 
{
    bDown = false;
}

function PupupMoveing() {

    if (bDown) 
    {
        var distX = event.clientX - x;
        var distY = event.clientY - y;

        var divPopup = nowPopupDivID + nowPopupNumber.toString();

        document.getElementById(divPopup).style.pixelLeft += distX;
        document.getElementById(divPopup).style.pixelTop += distY;
        x = event.clientX;
        y = event.clientY;
        return false;
    }
}

document.onmouseup = MouseUp;
document.onmousemove = PupupMoveing;


function AutoMoveProcess() 
{
    var height = document.getElementById("div_background").clientHeight;
    var width = document.getElementById("div_background").clientWidth;

    var x = (document.body.clientWidth / 2) - (width / 2);
    var y = (document.body.clientHeight / 2) - (height / 2);

    if (parseInt(y) < 0) {
        y = 0;
    }

    document.getElementById("div_background").style.left = x;
    document.getElementById("div_background").style.top = y;
}

function addEvent(obj, type, fn) {
    if (obj.addEventListener) {   //표준   
        obj.addEventListener(type, fn, false);
    } else if (obj.attachEvent) {    //IE
        obj.attachEvent("on" + type, fn);
    }
}
