﻿function popupWindow(name, title, url, divWidth, divHeight, needCoverDiv) {
    //添加Div
    var fullDiv = document.createElement("div");
    var content="<table cellpadding='0' cellspacing='0'>";
    content+="<thead><td class='lt'></td><td class='ct'></td><td class='rt'></td></thead>";
    content+="<tbody><td class='lt'></td>";
    content += "<td class='ct'>";
    content += "<div class='popupTitle' id='popupTitle" + name + "'>" + title + "<a href=\"javascript:closeWindow('"+name+"')\" class='popupClose' id='popupClose" + name + "'>关闭</a></div>";
    content += "<div class='popupBody' id='popupBody" + name + "'>";
    content += "<iframe src='"+url+"' class='popupIframe' id='popupIframe" + name + "' allowTransparency='true' frameborder='0'></iframe>";
    content+="</div>";
    content+="</td>";
    content+="<td class='rt'></td></tbody>";
    content+="<tfoot><td class='lt'></td><td class='ct'></td><td class='rt'></td></tfoot>";
    content+= "</table>";
    with (fullDiv) {
        id = "popup" + name;
        className = "popup";
        innerHTML = content;
    }
    document.body.appendChild(fullDiv);
    //窗口变化
    if (document.all) {
        window.attachEvent('onresize', PositionDiv(name,divWidth, divHeight));
    }
    else {
        window.addEventListener('resize', PositionDiv(name, divWidth, divHeight), true);
    }
    //遮照
    if (needCoverDiv) {
        var screenWidth = (document.documentElement.scrollWidth || document.body.scrollWidth);
        if (screenWidth < GetScreenWidth()) {
            screenWidth = GetScreenWidth();
        }
        var screenHeight = (document.documentElement.scrollHeight || document.body.scrollHeight);
        if (screenHeight < GetScreenHeight()) {
            screenHeight = GetScreenHeight();
        }
        var coverDiv = document.createElement("div");
        with (coverDiv) {
            id = "coverDiv"+name ;
            className = "coverDiv";
            style.width = screenWidth + "px";
            style.height = screenHeight + "px";
        }
        document.body.appendChild(coverDiv);
    }
    //拖动
    var titleDiv = document.getElementById("popupTitle" + name);
    var moveX = 0;
    var moveY = 0;
    var moveTop = 0;
    var moveLeft = 0;
    var moveable = false;
    var docMouseMoveEvent = document.onmousemove;
    var docMouseUpEvent = document.onmouseup;
    var iWidth = document.documentElement.clientWidth;
    var iHeight = document.documentElement.clientHeight;
    titleDiv.onmousedown = function() {
        var evt = getEvent();
        moveable = true;
        moveX = evt.clientX;
        moveY = evt.clientY;
        moveTop = parseInt(fullDiv.style.top);
        moveLeft = parseInt(fullDiv.style.left);
        document.onmousemove = function() {
            if (moveable) {
                var evt = getEvent();
                var x = moveLeft + evt.clientX - moveX;
                var y = moveTop + evt.clientY - moveY;
                if (x > 0 && (x + divWidth < iWidth) && y > 0 && (y + divHeight < iHeight)) {
                    fullDiv.style.left = x + "px";
                    fullDiv.style.top = y + "px";
                }
            }
        };
        document.onmouseup = function() {
            if (moveable) {
                document.onmousemove = docMouseMoveEvent;
                document.onmouseup = docMouseUpEvent;
                moveable = false;
                moveX = 0;
                moveY = 0;
                moveTop = 0;
                moveLeft = 0;
            }
        };
    }
    //Div定位
   position(name, divWidth, divHeight);
}
function $(id) {
    return document.getElementById(id);
}
function position(name, divWidth, divHeight) {
    $("popup" + name).style.top =GetDivTop(divHeight)+"px";
    $("popup" + name).style.left = GetDivLeft(divWidth)+"px";   
    $("popupIframe" + name).style.width = (divWidth-100)+"px";
    $("popupIframe" + name).style.height =(divHeight-100)+"px";
    $("popupBody" + name).style.width = $("popupIframe" + name).clientWidth + "px";
    $("popupBody" + name).style.height = $("popupIframe" + name).clientHeight + "px";
    $("popup" + name).style.width = $("popupIframe" + name).offsetWidth + 100 + "px";
    $("popup" + name).style.height = $("popupIframe" + name).offsetHeight + 100 + "px";
    $("popupTitle" + name).style.width = $("popup" + name).offsetWidth + "px";
}
function getEvent() {
    return window.event || arguments.callee.caller.arguments[0];
}
function closeWindow(name) {
    var coverDiv = document.getElementById("coverDiv" + name);
    if (coverDiv != null) {
        document.body.removeChild(coverDiv);
    }
    var fullDiv = document.getElementById("popup"+name);
    if (fullDiv != null) {
        document.body.removeChild(fullDiv);
    }
}
function GetDivLeft(divWidth) {
    var scrollLeft = (document.documentElement.scrollLeft || document.body.scrollLeft);
    return (GetScreenWidth() - divWidth) / 2 + scrollLeft;
}
function GetDivTop(divHeight) {
    var scrollTop = (document.documentElement.scrollTop || document.body.scrollTop);
    var top = (GetScreenHeight() - divHeight) / 2 + scrollTop;
    if (top > 0) {
        return top;
    }
    else {
        return 0;
    }
}
function GetScreenWidth() {
    return (window.innerWidth || document.documentElement && document.documentElement.clientWidth || document.body.clientWidth);
}
function GetScreenHeight() {
    return (window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight);
}
function PositionDiv(name, divWidth, divHeight) {
    return function() {
        if ($("popup" + name) != null) {
            with ($("popup" + name).style) {
                left = GetDivLeft(divWidth) + "px";
                top = GetDivTop(divHeight) + "px";
            }
        }
    }
}