/*拉伸窗体处理函数区域
此处的命名空间和U.DD.js相同 因为功能是属于一个模块下的
*/

//#region 拉伸窗体
Namespace.register("U.D");

//虚拟框
U.D.Masklayer = function () {
    return $$("div", { "style": { "cssText": "width:100%;height:100%;position:absolute;top:0;left:0;"} }, document.body);
}

/*
* 拖动框
*
* @param  {element} 需要创建虚拟框的元素
* @param  {object} 元素的位置
----------参数为GetElementInfo 返回值
*/
U.D.foundVirtualBox = function (UDOD, UE) {
    UDOD = $(UDOD);
    UE = UE || UDOD.GetElementInfo(); //获取元素的位置
    return $$("div", { "style": { "cssText": "pointer-events:none;border:2px solid Gray;position:absolute;top:" + (UE["OT"] - 2) + "px;left:" + (UE["OL"] - 2) + "px;width:" + (UE["OW"]) + "px;height:" + (UE["OH"]) + "px;z-index:" + (US.ZV + 10)} }, UDOD.Parent());
}

/*
* 拉伸函数处理
*
* @param  {element} 需要拉伸的元素
* @param  {function} 拉伸回调函数
----------参数为GetElementInfo 返回值
*/
U.D.AddOM = function (UDOD, UCB) {
    if (!$(".UD_W_WT", UDOD)[0]) { //拉伸对象不存在
        var i,
        _UTE = U.D.RCDG(UDOD, null, UCB), //创建拉伸处理
        _UTP = "UD_W_WT",
        _UDE = [["H", "up"], ["MR", "right"], ["B", "down"], ["ML", "left"], ["HL", "topright"], ["BR", "bottomright"], ["BL", "bottomleft"], ["HR", "topleft"]], //拉伸所有的监视 和 className 的设置
        _UDTD = $$("div", { "className": "UD_W_WT" }); //新建立一个容器
        for (i = 0; i < _UDE.length; i++) { //添加上下左右处理元素
            $$("div", { "className": _UTP + "_" + _UDE[i][0], "onmousedown": U.M.apply(_UTE, [[_UTE.set, [_UDE[i][1]]]]) }, _UDTD);
        }
        UDOD.appendChild(_UDTD); //添加拉伸元素
    }
}

/*
* 拉伸执行
*
* @param  {element} 需要拉伸的元素
* @param  {string} 需要调用的回调
* @param  {function} 拉伸回调函数
----------参数为GetElementInfo 返回值
*/
U.D.RCDG = function (UDOD, UTF, UCB) {
    return new U.D.RCDG.init(UDOD, UTF, UCB);
}

/*
* 拉伸执行
*
* @param  {element} 需要拉伸的元素
* @param  {string} 需要调用的回调
* @param  {function} 拉伸回调函数
*/
U.D.RCDG.init = function (UDOD, UTF, UCB) { //拖动的拉伸
    /*
    注释:
    -----UCB 回调函数
    -----UDOD 拉伸的原函数
    */
    U.Ut.AddObj(this, { UCB: UCB, UDOD: UDOD });
    (UTF) && (this.set(UTF)); //设置回调
}

U.D.RCDG.init.prototype = {
    /*
    * 拉伸执行
    *
    * @param  {element} 需要拉伸的元素
    * @param  {string} 
    * @param  {function} 拉伸回调函数
    */
    set: function (UTF) {
        var i, _UDMD,
        _UDOD = this.UDOD,
        _UE = U.M.GetElementAllInfo(_UDOD), //当前位置
        _UDE = [["move", UTF], ["up", "mup"]];
        if (UTF && event.button != 2 && (_UE["OH"] != US.Height || _UE["OW"] != US.Width)) { //非右键
            //内部使用变量设置
            U.Ut.AddObj(this, {
                UMD: (_UDMD = U.D.Masklayer()), //拉伸虚拟框
                UTF: UTF, //拖动时回调函数
                UTOD: U.D.foundVirtualBox(_UDOD), //虚拟框
                E: _UE, //当前的位置大小
                H: _UE.H, //原高
                W: _UE.W, //原长
                T: _UE.OT, //原top
                L: _UE.OL//原left
            });
            this.UTOD.setCapture(); //创建使用的变量

            //虚拟框和拖动看置顶
            U.D.SetDTop(this.UTOD);
            U.D.SetDTop(_UDMD); //虚拟框

            //拖动元素监视
            for (i = 0; i < _UDE.length; i++) {
                document["onmouse" + _UDE[i][0]] = U.M.apply(this, [[this[_UDE[i][1].toLocaleLowerCase()]]]);
            }

            //返回拖动对象
            return this; //设置拉伸
        }
    },
    //向上拉伸
    up: function () {
        var _UE = U.M.GetMousep(), //获取鼠标的位置
        _UT = Math.min(Math.max(_UE["Y"], 0), this.T + this.H - 4), //获取拉伸头部的top位置
        _UH = this.H + (this.T - _UT); //计算高度
        $(this.UTOD).addAttrArray({ "style": { "cssText": "top:" + (_UT - 2) + "px;height:" + (_UH) + "px"} }); //设置样式
    },
    //下面拉伸
    down: function () {
        var _UE = U.M.GetMousep(); //鼠标所在的位置
        _UE["Y"] = Math.max(Math.min(_UE["Y"], US.Height), this.T); //获取拉伸的位置
        this.UTOD.style.height = (_UE["Y"] - this.T) + "px"; //拉伸后的大小处理
    },
    //左边拉伸
    left: function () {
        var _UE = U.M.GetMousep(), //鼠标所在的位置
        _UL = Math.min(Math.max(_UE["X"], 0), this.L + this.W - 4), //左边位置处理
        _UW = this.W + this.L - _UL; //长度变化
        $(this.UTOD).addAttrArray({ "style": { "cssText": "left:" + (_UL - 2) + "px;width:" + (_UW) + "px"} });
    },
    //右边
    right: function () {
        var _UE = U.M.GetMousep();  //鼠标所在的位置
        _UE["X"] = Math.max(Math.min(_UE["X"], US.Width), this.L); //右边位置处理
        this.UTOD.style.width = (_UE["X"] - this.L) + "px"; //内容大小处理
    },
    //左上
    topleft: function () {
        this.up(); //向上拉伸
        this.left(); //左边拉伸
    },
    //右上
    topright: function () {
        this.up(); //向上拉伸
        this.right(); //右边拉伸
    },
    //左下
    bottomleft: function () {
        this.down(); //向下拉伸
        this.left(); //向左拉伸
    },
    //右下
    bottomright: function () {
        this.down(); //向下拉伸
        this.right(); //右边拉伸
    },
    //结束拉伸
    mup: function () {
        var i, _UPE = this.E.PD,
        _UBE = this.E.BD,
        _UDTD = $(this.UTOD), //拉伸元素
        _UCE = U.M.GetElementAllInfo(this.UTOD), //拉伸元素的大小
        _UDE = [_UCE["W"], _UCE["H"]];
        //设置得到的位置和大小
        for (i = 0; i < _UPE.length; i++) {
            _UDE[(i + 1) % 2] -= (_UPE[i] + _UBE[i]);
        }
        //设置拉伸元素
        $(this.UDOD).addAttrArray({ "style": { "cssText": "left:" + (_UCE["OL"] + 2) + "px;top:" + (_UCE["OT"] + 2) + "px;width:" + _UDE[0] + "px;height:" + _UDE[1] + "px"} }); //设
        //清空
        $(_UDTD).remove();
        _UDTD[0].releaseCapture();
        document.onmousemove = document.onmouseup = null;
        $(this.UMD).remove();
        //回调设置
        U.D.withf(this.UCB, _UDE);   //清空 //执行回调              
    }
}

/*
* 回调执行
*
* @param  {object} 回调函数或者参数
* @param  {object} 回调的参数
*/
U.D.withf = function (UAE, UDE) {
    if (UAE) {
        var i, j, k, _UCE, _UDOD,
        _UKE = {};
        for (i in UAE) {
            if (UAE.hasOwnProperty(i)) {
                //处理元素共同变化处理 这个会更加最大拖拉的元素 自适应
                if (i == "with") {
                    for (j = 0; j < UAE[i].length; j++) {
                        _UCE = UAE[i][j];
                        _UDOD = _UCE["Element"]; //获取需要变化的元素集
                        for (k in _UCE) {
                            if (_UCE.hasOwnProperty(k) && k != "Element") { //变化的样式处理
                                _UKE[k] = _UCE[k] + UDE[k == "width" ? 0 : 1];
                            }
                        }
                        $(_UDOD).addAttrArray({ style: _UKE });
                    }
                }
                //直接回调自己处理
                else { U.M.apply(null, UAE[i])(UDE); } //执行回调
            }
        }
    }
}


//#endregion