/*
此处为1473跨域文件夹 利用此文件函数实现1473跨域
*/
Namespace.register("U.CD");

//#region 跨域1473文件加载

//#region 跨域变量区域

//引入www.1473.cn的时候,需要图形界面,即引用ufromd.js的时候,要把首页全部下载下来。

//完全跨域的全局变量。例如a.com引入b.com,需要此文件
U.CD.dom = "/domain.htm"; //外域引入1473跨域文件

//引入www.1473.cn的时候,需要图形界面,即引用ufromd.js的时候,要把首页全部下载下来。

//引入www.1473.cn的时候,%.14573.cn引入www.1473.cn的时候,需要使用到下面配置
//二级跨域的全局变量,暂时没有用
U.CD.cdom = "/Crossdomain.htm"; //跨域获取1473桌面文件


U.CD.Len = 1; //主加载域名长度
U.CD.ST = ["U_Main"]; //主加载域
//跨域1473获取的变量
U.CD.UTF = [{ "U_Main": "MA" }, { "U_Disk": "Adisk" }, { "U_BG": "UB" }, { "U_PB": "PB" }, { "U_Pay": "PY" }, { "U_ME": "MA" }, { "reply": "RY"}]; //ajax对应变量

//所有跨域以前是指向crossdomain.htm 现在指向了index.htm。
U.CD.UKE = [
    ["U_Main", "http://main.1473.cn/Crossdomain.htm", "A"], //主域名跨域地址
//["U_Pay", "http://pay.1473.cn/Crossdomain.htm", "PY"], //交易系统
//["U_Office", "http://office.1473.cn/Crossdomain.htm", "Of"]
];

//#endregion

//#region 跨域解决方案

/**
* 加载跨域文件  异步加载iframe跨域请求1473项目加载请求
* @param  {array} 跨域集
----------[["iframename", "iframeurl", "ajaxname"]]
----------[iframename] iframename iframe对应的name
----------[iframeurl] iframeurl iframe加载的url
----------[ajaxname] ajaxname //原u.a替换的命名
* @param  {function} 加载成功回调函数
* @param  {element} 用户使用U.A.Request时需要时间,则做loading效果
*/
U.CD.LIFA = function (_ajaxArr, _cb, _el) {
    //UBE = UBE || []; //传参默认值
    var _bool, _iframeId, i;
    // i = UBE.length,_UKE
    _ajaxName = U.CD.UTF; //ajax跨域加载域名对应
    _crossObj = U.CD.UKE; //添加用户合一 需要跨域的obj对象
    _objLength = _crossObj.length;
    _frag = $$("frag");
    U.CD.Len = _crossObj.length; //变量
    //for (j = 0; j < i; j++) { U.CD.ST.push(UBE[j][0]); } //添加预加载处理方式
    if (document.domain.indexOf("1473.cn") > -1) { //1473非跨域形式
        //while (i--) { //设置外跨域文件 
        //_UKE = {};
        //_UKE[UBE[i][0]] = UBE[i][2];
        //_UCE = [_UKE].concat(_UCE);
        //}
        for (i = 0; i < _objLength; i++) { //创建跨域
            try {
                //得到形如"U_Main"的iframe的id或者名字
                _iframeId = _crossObj[i][0];
                //如果iframe已经存在,表示已经加载了跨域iframe,该跨域已经加载处理
                if (window[_iframeId] && window[_iframeId].location) { //判断跨域是否已经加载
                    //如果iframe没有加载完,则找不到location.href。
                    if (window[_iframeId].location.href == _crossObj[i][1]) {
                        if (U.CD.AsynLIFA.apply($("#" + _iframeId)[0], [_ajaxName[i], _cb, []])) { //设置域名加载成功函数
                            return;
                        };
                        break;
                    } else { //移除加载
                        $("#" + _iframeId).remove();
                    }
                }
                //创建跨域iframe的函数。
                //跨域未加载处理
                _bool = true;
                _UIFR = $$("iframe", { "id": _iframeId, "name": _iframeId, "width": 0, "height": 0, "style": { "display": "none" }, "src": _crossObj[i][1] }, _frag);
                //跨域回调设置
                U.MD.IframeLoad(_UIFR, [
                    [U.CD.AsynLIFA, [_ajaxName[i], _cb, U.CD.ST]]
                ]);
            } catch (e) {
                return;
            }
        }
    } else { //1473跨域形式
        if (window["U_Domain"]) {
            if (window["U_Domain"].location.href == U.CD.dom) {
                U.CD.AsynDomain(_cb);
                return;
            }
        } //跨域已经加载成功
        else {
            U.CD.Introduce(_cb); //初始化1473
        }
    }
    //跨域加载需要时间,可以传递一个loading图标。
    if (_el && _bool) {
        U.MD.loading(_el[0] || _el, _el[1]); //设置loading加载
    }
    ($("#usestudio-server")[0] || $("body")[0]).appendChild(_frag); //添加到页面
}

/**
* iframe加载成功后,异步加载跨域
*
* @param  {object} 当前跨域集
* @param  {function} 成功回调
* @param  {array} 所有的跨域集
* @return {boolean} 是否已经成功过回调
*/
U.CD.AsynLIFA = function (iframe, _cb, _crossArr) { //加载跨域文件
    var i, j, _iframe = this.contentWindow;
    _iframe.name = this.id; //设置当前iframe name
    U.CD["Len"]--; //总加载-1
    try {
        for (i in iframe) {
            //window.U[iframe[i]] = _iframe.U.A; //设置此域U.A的别称,让他们指向一致。
            //加载成功后,则删除跨域变量。
            j = _crossArr.indexOf(i);
            if (j > -1) { _crossArr.splice(j, 1) }
        }
    } catch (e) { } //加载Iframe
    if (!_crossArr.length) { //判断是否已经加载成功过
        _crossArr.push(false);
        if (_cb) {
            (_cb()); //回调函数
        }
        return true;
    }
}

/**
* 非1473引入1473...
*
* @param  {function} 加载回调
* @param  {element} loading元素
*/
U.CD.Introduce = function (_cb, _el) {
    var _el = $$("iframe", {
        "id": "U_Domain",
        "name": "U_Domain",
        "width": 0,
        "height": 0,
        "style": { "display": "none" },
        "src": U.CD.dom
    });
    U.MD.IframeLoad(_el, $("#usestudio-server")[0] || $("body")[0]), [
        [U.CD.AsynDomain, [_cb]]
    ]; //初始化1473
    if (_el) {
        U.MD.loading(_el[0] || _el, _el[1]); //加载1473loading
    }
    // (UDOD) && (U.MD.loading(UDOD[0] || UDOD, UDOD[1])); //加载1473loading
}

/**
* 非1473加载跨域回调
*
* @param  {element} 元素加载的位置
*/
U.CD.AsynDomain = function (_cb) {
    (U.Ut.isFunction(_cb)) && (_cb());
}




//针对图形界面。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

/**
* 跨域引用1473。。。。。。。。。。。。。。,示例在c.147.cn和d.1473.cn。
*
* @param  {element} 加载1473时给其他网站的loanding元素
* @param  {function} 加载1473成功的回调函数
* @param  {bool} 是否加载1473桌面,如果不是,则只加载窗体。任务管理系统需要我们的桌面
*/
U.CD.GQB = function (loading, _cb, _bool) {
    if (!$("#usestudio-server")[0]) {
        var _el = $$("div", { id: "usestudio-server", className: "usestudio-server", "__top__": "false" }, document.body); //生成跨域加载
        //1473域加载1473平台
        if (document.domain == "1473.cn") {
            _el = $$("iframe", { "width": 0, "height": 0, "style": { "display": "none" }, "src": "/index8.aspx" }, $("body")[0]); //获取1473主站点
            window.onload = null;
            U.MD.IframeLoad(_el, [
                [U.CD.AsynGQB, [_el, loading, _cb, _bool]]
            ]); //加载1473内容
        }
        //非1473加载
        else { U.CD.CGQB(_cb, _bool); }
    }
}

/*
* 加载1473整体
*
* @param  {element} 当前加载的iframe
* @param  {element} 需要loading的元素
* @param  {element} 加载的位置
* @param  {bool} 是否加载1473桌面,如果不是,则只加载窗体。任务管理系统需要我们的桌面
*/
U.CD.AsynGQB = function (iframe, loading, cb, b) {
    var i, _document, _html;
    //_UNE = undefined,
    //_UTF = U.Ut.isString(UDOD), //判断位置是否为元素
    var _windows = iframe.contentWindow; //1473cookie是
    document.wincookie = iframe.contentWindow.document.cookie; //document.wincookie 自己做一个全局变量,记录1473的cookie.
    var _content = $("#usestudio-server")[0]; //获取加载的位置
    //if (_UTF) {
    //    UDOD = $($$("div", { "innerHTML": UDOD })).Child()[0];
    //    _UST = UDOD.innerHTML;
    //}
    //else {
    _html = $(b === true ? "body" : "#UD_SY", _windows.document)[0].innerHTML;
    //}
    //if (!_UTF) {
    try {
        _document = _windows.document.open();
    }
    catch (e) {
        _document = _windows.document;
    }
    _document.write = "";
    _document.close();
    $(iframe).remove()
    //}
    _content.innerHTML = _html; //添加值
    if (U.D.Load) { U.D.Load(cb) }
    else if (cb) { cb(); } //调用1473预加载
}