﻿function Xwindow() {
    this.mouseState = document.all ? 1 : 0;
    this.x;
    this.y;
    this.dragId = null;
    this.index = 8100;
    //document.onselectstart = function() { return false; };
    this.moveId = null;
    this.fgLayer = null;
}

Xwindow.prototype.dragStar = function(moveId, title) {//拖动入口函数

    var dragId = moveId + "_0000000000001";
    var fgLayer = "fgLayer";

    var _this = this;

    _this.moveId = moveId;
    _this.dragId = dragId;
    _this.fgLayer = fgLayer;

    var win1 = document.createElement("div");

    var h = "0px";
    if (document.getElementById(moveId).style.height == "")
        h = document.getElementById(moveId).height;
    else
        h = document.getElementById(moveId).style.height;

    win1.setAttribute("id", dragId);
    win1.style.cssText = "background-position: #6484B7; height: 28px;width:100%; overflow: hidden; background: #6484B7; font-weight: bold; padding-left: 0px;padding-right: 0px; padding-top: 0px; color: #FFFFFF;"
    win1.innerText = title;
    win1.innerHTML = "<table cellpadding='0' cellspacing='0' style='width:100%;height:28px;font-weight: bold; color: #FFFFFF;'><tr><td>&nbsp;" + title + "&nbsp;</td><td align='right'><img align='absmiddle' alt='关闭' src='del.gif' style='width: 22px; height: 22px;cursor:hand;' onclick=\"CloseDrag('" + moveId + "')\" /></td></tr></table>";
    _this.$(moveId).insertBefore(win1, _this.$(moveId).firstChild);


    var win2 = document.createElement("div");
    win2.setAttribute("id", fgLayer);
    win2.style.cssText = "background-color:#FFFFFF;position:absolute;z-index:8100; Filter: Alpha(Opacity=40); display:none;";
    win2.style.width = Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth) + "px";
    win2.style.height = (Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight) + 30) + "px";
    document.body.insertBefore(win2, document.body.firstChild);



    if (_this.$(moveId).style.background == "")
        _this.$(moveId).style.background = "#ffffff";



    _this.$(moveId).style.position = "absolute";
    _this.$(moveId).style.display = "none";



    _this.$(dragId).style.cursor = "move";
    _this.$(dragId).onmousedown = function(e) {
        var e = e ? e : event;
        //_this.$(moveId).style.zIndex = _this.index++;
        if (e.button == _this.mouseState) {
            _this.setDragInfo(e, moveId, moveId);
            _this.dragPro(moveId);
        }
    },
          _this.$(moveId).onmousedown = function(e) {
              _this.$(moveId).style.zIndex = _this.index++;
          },
          _this.$(dragId).onmouseup = function() {
              _this.clearDragId();
          }
    document.onmouseup = function() {
        _this.clearDragId();
    }
}


Xwindow.prototype.setDragInfo = function(e, dragId, moveId) {//拖动初始化
    this.x = e.clientX;
    this.y = e.clientY;
    this.dragId = dragId;
    if (this.$(moveId).style.position != "absolute") {
        this.$(moveId).style.width = this.$(moveId).offsetWidth;
        this.$(moveId).style.height = this.$(moveId).offsetHeight;
        this.$(moveId).style.position = "absolute";
        this.$(moveId).style.left = this.$(moveId).offsetLeft;
        this.$(moveId).style.top = this.$(moveId).offsetTop;
    }
}

Xwindow.prototype.clearDragId = function() { //清除拖动ID
    this.dragId = null;
}

Xwindow.prototype.dragPro = function(moveId) {
    var _this = this;
    document.onmousemove = function(e) {
        var e = e ? e : event;
        if (e.button == _this.mouseState && _this.dragId != null) {
            var x = e.clientX;
            var y = e.clientY;
            _this.$(moveId).style.left = (_this.$(moveId).offsetLeft + (x - _this.x)) + "px";
            _this.$(moveId).style.top = (_this.$(moveId).offsetTop + (y - _this.y)) + "px";
            _this.x = x;
            _this.y = y;
            //alert(_this.$(dragId).style.left);
        }
    }
}

Xwindow.prototype.$ = function(o) {//获取对象
    if (typeof (o) == "string") {
        if (document.getElementById(o)) {
            return document.getElementById(o);
        }
        else {
            alert("errId \"" + o + "\"!");
            return false;
        }
    }
    else {
        return o;
    }

}

Xwindow.prototype.show = function() {
    //if (this.flag_ == false) {
    this.$(this.moveId).style.top = (Math.abs(document.body.offsetHeight - parseFloat(this.$(this.moveId).style.height)) / 2) + document.body.scrollTop;

    this.$(this.moveId).style.left = (Math.abs(document.body.offsetWidth - parseFloat(this.$(this.moveId).style.width)) / 2) + document.body.scrollLeft;
    //}

    this.$(this.moveId).style.zIndex = 8880;
    //this.$(this.dragId).style.display = "";
    this.$(this.moveId).style.display = "";
    this.$(this.fgLayer).style.display = "";
}

Xwindow.prototype.close = function() {
    this.$(this.moveId).style.display = "none";
    this.$(this.fgLayer).style.display = "none";
}
function CloseDrag(p) {
    document.getElementById("fgLayer").style.display = "none";
    document.getElementById(p).style.display = "none";
}
