var loadurl = "";//
var skinPath = "/Css/";
var isBackGround = true;
var zIndex = 500;
var autoHeight = true;
var did = "";
function showWindow(a, b, c, d, w, h, e, func, f) {
    did = e;
    if (f) {
        return window.parent.XDrag.create(a, b, c, d, w, h, e, func)
    } else {
        return XDrag.create(a, b, c, d, w, h, e, func, f)
    }

};
function closeWindow() {
    if (window.frameElement) {
        window.parent.document.body.focus();
        XDrag.Close(window.frameElement)
    } else {
        if (arguments[0]) {
            XDrag.Close(arguments[0])
        }
    }
};
function DragBox() {
    this.backGround = isBackGround ? true: false;
    var m;
    var n;
    this.Show = function() {};
    this.Close = function() {
        close(arguments[0])
    };
    this.create = function(a, b, c, d, w, h, e, func) {
//        alert(document.body.clientWidth+", "+document.body.scrollWidth+", "+document.documentElement.offsetWidth+", "+document.documentElement.clientWidth);
//        alert(document.body.clientHeight+", "+document.body.scrollHeight+", "+document.documentElement.offsetHeight+", "+document.documentElement.clientHeight);
        m = document.documentElement.clientWidth;//Math.max(document.body.clientWidth, document.body.scrollWidth, document.documentElement.offsetWidth, )
        n = document.documentElement.clientHeight;//Math.min(document.body.clientHeight, document.body.scrollHeight, document.documentElement.offsetHeight, )
        return create(a, b, c, d, w, h, e, func)
    };
    this.moveFilm = function(a, b, c, l, t) {
        if (b < 0) b = 0;
        if (c < 0) c = 0;
        if (typeof(window["ct" + a]) != "undefined") clearTimeout(window["ct" + a]);
        var o = $(a);
        if (!o) return;
        o.style.top = c + "px";
        o.style.left = b + "px";
        return
    };
    var p;
    function create(a, b, c, d, w, h, e, func) {
        e = e || "winx";
        zIndex = d || zIndex;
        h = h || 400;
        w = w || 600;
        c = c || "新窗口...";
        b = b || "数据加载中...";
        a = a || "";
        if (autoHeight) {
            if (h > document.documentElement.clientHeight) h = document.documentElement.clientHeight
        }
        p = document.createElement("DIV");
        p.setAttribute("id", e);
        p.setAttribute("Max", 0);
        p.setAttribute("Min", 0);
        p.setAttribute("drag", "true");
        p.style.cssText = "width:" + w + "px;height:" + h + "px;left:0;top:0";
        p.className = "box";
        p.style.position = "absolute";
        p.style.zIndex = zIndex++;
        document.getElementsByTagName('body')[0].appendChild(p);
        var f = appendElement(p, "", "box_con_center");
        var g = appendElement(f, "", "title");
        g.style.cssText = "width:" + w + "px;height:28px;border:solid 1px #4685bd;border-bottom:none;padding:4px 5px 0 0";
        var i = document.createElement("b");
        i.innerHTML = c;
        g.appendChild(i);
        var j = document.createElement("a");
        j.href = "javascript:void(0);";
        j.className = 'close';
        j.onclick = function(){close(e, func);}
        j.innerHTML = "X";
        j.title = '关闭';
        g.appendChild(j);
        var k = appendElement(f, "", "content");
        k.setAttribute("id", "xdrag_content");
        k.style.cssText = "border:solid 1px #4685bd";
        k.style.width = (parseInt(w) + 5) + "px";
        k.style.height = h + "px";
        if (a != "") {
            k.innerHTML = '<iframe id="IFrame1" name="IFrame" style="width:100%;height:100%" frameborder="0" scrolling="auto" src="' + (loadurl == "" ? a: loadurl + '?url=' + a) + '" fdrag="true"></iframe>'
        } else {
            k.innerHTML = b
        }
        f.style.width = w - 12 + "px";
        f.style.height = h - 12 + "px";
        position(p.id);
        if (XDrag.backGround) {
            appendBG()
        }
        p.focus();
        return p.id
    };
    function clearChilds(a) {
        for (var i = a.childNodes; i > -1; i--) {
            if (a.childNodes[i].childNodes.length > 0) clearChilds(a.childNodes[i]);
            else a.removeChild(a.childNodes[i])
        }
    };
    function position(a) {
        var o = $(a);
        if (o.style.display == 'none') return;
        var b = Math.max(document.body.scrollTop, parent.document.documentElement.scrollTop);
        var c = [];
//        alert(m+'   '+n+'   '+o.offsetHeight+'   '+b+'   '+(n - o.offsetHeight) / 2 + b);
        c[0] = (m > o.offsetWidth ? (m - o.offsetWidth) / 2 : 0);
        c[1] = (n > o.offsetHeight ? (n - o.offsetHeight) / 2 + b: b);
        XDrag.moveFilm(a, c[0], c[1], 0, 0)
    };
    function $() {
        return "string" == typeof arguments[0] ? document.getElementById(arguments[0]) : arguments[0]
    };
    function close() {
        if(typeof arguments[1] == "function"){
             setTimeout(arguments[1], 0);
        }
        var o = null;
        if (arguments[0] && typeof(arguments[0].tagName) != "undefined") {
            window.parent.document.body.focus();
            var o = arguments[0];
            if (typeof(o) == "string") {
                o = document.getElementById(o)
            }
            if (!o) return;
            if (o.tagName == "IFRAME" && o.getAttribute("fdrag") == "true") {
                if (window.parent.XDrag.backGround && window.parent.XDrag.fullBox) {
                    window.parent.XDrag.fullBox.style.display = "none"
                }
                o.src = "about:blank";
                o = getBox(o)
            } else o = getBox(this)
        } else {
            o = getBox(this)
        }
        if (!o || !o.parentNode) {
            document.body.removeChild($(arguments[0]))
        } else {
            o.parentNode.removeChild(o)
        }
        if (XDrag && XDrag.backGround && XDrag.fullBox) {
            XDrag.fullBox.style.display = "none"
        }
        o = null;
        realase();
        mystop()
    };
    function mystop() {
        if (document.all) {
            document.execCommand("stop")
        }
    };
    function realase() {
        height = null;
        width = null;
        p = null
    };
    function appendBG() {
        if (XDrag.fullBox) {
            XDrag.fullBox.style.display = ""
        } else if (document.getElementById("fullDragBox_Div")) {
            XDrag.fullBox = document.getElementById("fullDragBox_Div");
            XDrag.fullBox.style.display = ""
        } else {
            XDrag.fullBox = document.createElement("div");
            document.getElementsByTagName('body')[0].appendChild(XDrag.fullBox);
            XDrag.fullBox.id = "fullDragBox_Div";
            XDrag.fullBox.oncontextmenu = function() {
                return false
            };
            var a = XDrag.fullBox.style;
            a.zIndex = zIndex - 2;
            a.position = "absolute";
            a.left = '0';
            a.top = '0';
            a.width = Math.max(document.getElementsByTagName('body')[0].scrollWidth, document.documentElement.offsetWidth) + 'px';
            a.height = Math.max(document.getElementsByTagName('body')[0].scrollHeight, document.documentElement.offsetHeight) + 'px';
            a.background = '#333';
            if (navigator.appMinorVersion != '0') {
                XDrag.fullBox.innerHTML = '<iframe src="about:blank" class="dragiframe"></iframe>'
            }
            setStyle(XDrag.fullBox, 'opacity', 0.2)
        }
    };
    function appendElement(a, b, c) {
        var d = document.createElement("DIV");
        if (b != "") d.setAttribute("id", b);
        if (c != "") d.className = c;
        a.appendChild(d);
        return d
    };
    function setStyle(a, b, c) {
        if (typeof a != 'object') a = $(a);
        switch (b) {
        case 'opacity':
            if (a.filters) {
                a.style.filter = 'alpha(opacity=' + c * 100 + ')';
                if (!a.currentStyle.hasLayout) {
                    a.style.zoom = 1
                }
            } else {
                a.style.opacity = c;
                a.style['-moz-opacity'] = c;
                a.style['-khtml-opacity'] = c
            }
            break;
        default:
            a.style[b] = c
        }
        a = null
    };
    function getBox(a) {
        if (a.getAttribute) {
            while (a.getAttribute("drag") != "true") {
                if (a.parentNode.getAttribute) {
                    a = a.parentNode
                } else {
                    break
                }
            }
        }
        return a
    }
    
};
var XDrag = new DragBox();
function vauto() {
    
    if(did !=''){
        var myn = document.documentElement.clientHeight;//Math.min(document.body.clientHeight, document.body.scrollHeight, document.documentElement.offsetHeight, )
        var myb = Math.max(document.body.scrollTop, parent.document.documentElement.scrollTop); // $(window).scrollTop();
//        document.getElementById(did).style.top=(myb + (myn - document.getElementById(did).offsetHeight) / 2) + "px";
        $('#' + did).css("top", (myb + (myn - $('#' + did).attr("offsetHeight")) / 2) + "px");
       
       
    }
}
window.onscroll=vauto;
function hauto(){
    if(did !=''){
        var myn = document.documentElement.clientWidth;//Math.max(document.body.clientWidth, document.body.scrollWidth, document.documentElement.offsetWidth, )
        var myb = Math.max(document.body.scrollLeft, parent.document.documentElement.scrollLeft);
//        document.getElementById(did).style.left=(myb + (myn - document.getElementById(did).offsetWidth) / 2) + "px";
        $('#'+did).css("left",(myb + (myn - $('#'+did).attr("offsetWidth")) / 2) + "px");
    }
}
window.onresize=hauto;

