﻿window.onload = function() {
    if (!document.all) {
        document.readyState = "complete";
    }
}
function popupWindow(name, title, url, divWidth, divHeight, needCoverDiv, scroll, needTitle) {
    if (document.readyState == "complete") {
        var titleHeight = 28;
        if (needTitle == 0) {
            titleHeight = 0;
        }
        var fullDivName = name;
        var coverDivName = name + "_Cover";
        var titleDivName = name + "_Title";
        var tempDiv = document.getElementById(fullDivName);
        if (tempDiv == null) {
            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 = coverDivName;
                    className = "coverDiv";
                    style.width = screenWidth + "px";
                    style.height = screenHeight + "px";
                }
                document.body.appendChild(coverDiv);
            }
            var fullDiv = document.createElement("div");
            with (fullDiv) {
                id = fullDivName;
                className = "popup";
                style.left = GetDivLeft(divWidth) + "px";
                style.top = GetDivTop(divHeight) + "px";
                style.width = (divWidth + 7) + "px";
                style.height = (divHeight + 9) + "px";
            }
            document.body.appendChild(fullDiv);

            var groundDiv = document.createElement("div");
            with (groundDiv) {
                className = "popupSubcoat";
                style.width = (divWidth + 10) + "px";
                style.height = (divHeight + 12) + "px";
            }
            fullDiv.appendChild(groundDiv);

            var mainDiv = document.createElement("div");
            with (mainDiv) {
                className = "popupMain";
                style.width = (divWidth - 2) + "px";
            }
            fullDiv.appendChild(mainDiv);

            if (needTitle == 1) {
                var titleDiv = document.createElement("div");
                with (titleDiv) {
                    id = titleDivName;
                    className = "popupTitle";
                    style.height = titleHeight + "px";
                }
                titleDiv.innerHTML = "<a href=\"javascript:closeWindow('" + name + "');\" class='popupClose'>关闭</a>" + title;
                mainDiv.appendChild(titleDiv);
                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;
                        }
                    };
                }
            }
            var contentDiv = document.createElement("div");
            with (contentDiv) {
                className = "popupContent";
                style.width = (divWidth - 2) + "px";
                style.height = (divHeight - titleHeight) + "px";
                innerHTML = "<iframe class='popupIframe' src='" + url + "' width='" + (divWidth - 2) + "px' height='" + (divHeight - titleHeight) + "px' scrolling='" + scroll + "' transparent='true' frameborder='0'></iframe>";
            }
            mainDiv.appendChild(contentDiv);
            function PositionDiv() {
                if (document.getElementById(fullDivName) != null) {
                    with (fullDiv.style) {
                        left = GetDivLeft(divWidth) + "px";
                        top = GetDivTop(divHeight) + "px";
                    }
                }
            }
            if (document.all) {
                window.attachEvent('onresize', PositionDiv);
            }
            else {
                window.addEventListener('resize', PositionDiv, true);
            }
        }
    }
    else {
        AlertMessage("页面正在加载中，请稍等..");
        return;
    }
}
function getEvent() {
    return window.event || arguments.callee.caller.arguments[0];
}
function closeWindow(name) {
    var coverDivName = name + "_Cover";
    var coverDiv = document.getElementById(coverDivName);
    if (coverDiv != null) {
        document.body.removeChild(coverDiv);
    }
    var fullDiv = document.getElementById(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);
    return (GetScreenHeight() - divHeight) / 2.5 + scrollTop;
}
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);
}