Namespace.register("U.Word.O.O"); // 大纲视图

//window.onload = function () {
//    U.Word.O.O.BindEle();
//}

U.Word.O.O.Flag = true; // 只运行一次

/* 加载目录 */
U.Word.O.O.Load = function (pos) {

    //window.onload 调用 UW.U.OU.R = U.D.E.GetSelectionRange(window, 需要聚焦的元素, {CB : function(){} ,TF : 'sbzzm' }); 
    //onmousedown 不要用 onclick  UW.U.OU.R.Parent() 当前所在的元素
    //onmousedown UW.U.OU.R.Replace(<span style=>标题一</span>)

    var UW = U.D.Office.WI();
    if (UW.$(".U_Boom_O_O_C_Txt")[0] && !arguments[1]) {
        return
    };
    // $(".U_Boom_O_O_Cat")[0].innerHTML = '';
    var i = 0, // 循环
        j = 0; // 处理相同id的元素
    Udiv = UW.$("#UD_SYQXR div"),
        Child = null, // 选中元素的所有兄弟节点
        pos = pos || UW.$(".U_Boom_CO_Editor_body_main_text")[0]; // 目录生成位置
    if (!UW.$(".U_Boom_Box")[0]) { // 判断目录是否已经生成过
        var U_Boom_Box = $$("div", { "className": "U_Boom_Box", "contenteditable": "false" });
        pos.insertBefore(U_Boom_Box, pos.children[0]);
    }
    UW.$(".U_Boom_Box")[0].innerHTML = "";
    var U_Boom_Box = UW.$(".U_Boom_Box")[0];
    var U_Boom_O_O_C_Txt = $$("div", {
        "class": "U_Boom_O_O_C_Txt",
        "innerText": "目录"
    }, U_Boom_Box);
    var U_Boom_O_O_C_Wrap = $$("div", {
        "class": "U_Boom_O_O_C_Wrap"
    }, U_Boom_Box)
    // 遍历div标签
    for (; i < Udiv.length; i++) {
        if (Udiv[i].className) {
            j = i;
            Child = Udiv[i];
            if (Child.id) {
                // 解决用户回车生成多个同id元素重复循环
                if (i >= 1 && Child.id == Udiv[--j].id && Child.textContent.trim() != "") {
                    Child.id = "T" + Date.parse(new Date());
                };
                if (Child.childNodes[0] !== undefined && Child.childNodes[0].nodeValue != null) { // 解决空标题
                    var U_Boom_O_O_C_Box = $$("div", {
                        "class": "U_Boom_O_O_C_Box"
                    }, U_Boom_O_O_C_Wrap);
                    var a = $$("a", {
                        "class": "U_Boom_O_O_C_Name",
                        "style": {
                            "margin-left": Child.className.match(/\d+$/)[0]
                        },
                        "href": "#" + Child.id
                    }, U_Boom_O_O_C_Box);
                    //                    var span = $$("span", {
                    //                        "innerText": "",
                    //                        "style": {
                    //                            "margin-left": "5px",
                    //                            "float": "left"
                    //                        }
                    //                    }, U_Boom_O_O_C_Box);
                    a.innerHTML = Child.childNodes[0].nodeValue;
                    $$("div", {
                        "class": "U_Boom_O_O_C_Dash",
                        "style": { "width": "80%" }
                    }, U_Boom_O_O_C_Box);
                    $$("div", {
                        "class": "U_Boom_O_O_C_Number",
                        "innerText": U.Word.O.O.UpPageNum(Child),
                        "style": { "width": "auto" }
                    }, U_Boom_O_O_C_Box);
                };
            };
        };
    };
}

/* 按钮点击 */
U.Word.O.O.Click = function (e) {

    //    if (U.Word.O.O.Flag) {
    //        UW.U.OU.R = U.D.E.GetSelectionRange(window, $("#K")[0].contentWindow.$("#UD_SYQXR")[0], {
    //            SO: $("#K")[0].contentWindow.$("#UD_SYQXR")[0],
    //            "TF": "Title1"
    //        })
    //        U.Word.O.O.Flag = false;
    //    };
    //    var UW = U.D.Office.WI($("#K"));
    //    U.OU.R.CreateR();
    switch (e) {
        case "一级目录":
            U.Word.O.O.NewTitle("U_Boom_O_O_25px_0");
            break;
        case "二级目录":
            U.Word.O.O.NewTitle("U_Boom_O_O_22px_40");
            break;
        case "三级目录":
            U.Word.O.O.NewTitle("U_Boom_O_O_20px_60");
            break;
        case "生成目录":
            U.Word.O.O.Load(U.D.Office.WI().$("#UD_SYQXR .U_Boom_CO_Editor_body_main_text")[0]);
            break;
        case "更新目录":
            U.Word.O.O.Load(null, 'update');
            break;
        case "正文":
            U.Word.O.O.NewTitle();
            break;
        default:
            break;
    }
}


/* 绑定事件 */
U.Word.O.O.BindEle = function () {

    // 更新目录
    $(".U_Boom_O_O_F_Update").bind("mousedown", function () {
        U.Word.O.O.Load(null, 'update');
    });
    // 解决失焦问题
    $("#K")[0].contentWindow.$("#UD_SYQXR").bind("click", function () {
        U.OU.R.CreateR();
    })
}

/* 创建标题 */
U.Word.O.O.NewTitle = function (className) {
    var UW = U.D.Office.WI();
    var div = $$("div", {
        "innerText": "请输入正文"
    }),
        inText = UW.U.OU.R.Parent().innerText || UW.U.OU.R.Parent().nodeValue;
    var NowDate = Date.parse(new Date());
    if (className) {
        div = $$("div", {
            "id": "T" + NowDate,
            "contenteditable": true,
            "class": className,
            "innerText": inText
        });
    };
    // 在当前行有文字的基础上运行代码
    if (UW.U.OU.R.R.endContainer.nodeValue !== null) {
        // 若选中文字 设置文字样式
        if (UW.U.OU.R.GetSelectedText().trim() !== "" && className !== undefined) {
            // 解决标题重复嵌套导致布局错乱问题
            if (UW.U.OU.R.R.endContainer.parentNode.id) {
                UW.U.OU.R.R.endContainer.parentNode.className = className;
                UW.U.OU.R.R.endContainer.parentNode.innerText = UW.U.OU.R.GetSelectedText();
                return
            };
            div.innerText = UW.U.OU.R.GetSelectedText();
            UW.U.OU.R.Replace(div);
            return;
        };
        // 解决空文本嵌套标题bug
        if (UW.U.OU.R.Parent().nodeName === "DIV" && className !== undefined && UW.U.OU.R.GetSelectedText().trim() !== "") {
            UW.U.OU.R.Parent().id = "T" + NowDate;
            UW.U.OU.R.Parent().className = className;
            return;
        };
        // 若当前元素是div 则直接在该div上添加样式
        if (UW.U.OU.R.Parent().parentNode.nodeName === "DIV" && className !== undefined && UW.U.OU.R.Parent().parentNode.id !== "UD_SYQXR") {
            UW.U.OU.R.Parent().parentNode.id = "T" + NowDate;
            UW.U.OU.R.Parent().parentNode.className = className;
            return;
        };
        // 若设置为正文 删除当前元素的id class
        if (UW.U.OU.R.Parent().parentNode.id && className === undefined) {
            UW.U.OU.R.Parent().parentNode.removeAttribute("id");
            UW.U.OU.R.Parent().parentNode.removeAttribute("class")
            return;
        };
    };
    // 解决空白行插入正文bug 
    if (UW.U.OU.R.R.endContainer.nodeValue === null && UW.U.OU.R.GetSelectedText().trim() === "" && className === undefined && UW.U.OU.R.Parent().id !== "UD_SYQXR") {
        // 插入文字同时清空样式
        UW.U.OU.R.R.endContainer.className = "";
        UW.U.OU.R.Replace("请输入正文");
        return
    };
    // 当前行内有文字 点击正文清除样式
    if (UW.U.OU.R.R.endContainer.nodeValue !== null && className === undefined) {
        UW.U.OU.R.R.endContainer.parentNode.className = "";
        return
    };
    // 当前行无文字不给予处理
    if (div.innerText.trim() == "" && UW.U.OU.R.R.endContainer.nodeValue === null) {
        UW.U.OU.R.Replace("");
        return
    }
    UW.U.OU.R.Replace(div, UW.U.OU.R.QX());
}


/* 阅览模式 */
U.Word.O.O.Read = function (dis, boolen) {
    var dis = dis || "none",
        boolen = boolen || 'false';
    var Wrap = $(".U_Boom_O_O_Content"),
        i = 0;
    $(".U_Boom_O_O_Features")[0].style.display = dis;
    for (; i < Wrap.length; i++) {
        Wrap[i].setAttribute('contenteditable', boolen)
    };
}

/* 更新页码 */
U.Word.O.O.UpPageNum = function (child) {
    var UW = U.D.Office.WI();
    var Paper = UW.$("#UD_SYQXR"),
        i = 0;
    for (; i < Paper.length; i++) {
        if (U.M.EISE(Paper[i], child)) {
            return ++i
        }
    };

}