$(function () {

    //编辑器自定义
    $("#pageContent_txt").xheditor({
        skin: 'nostyle',
        width: 680,
        height: 320,
        tools: '|,Bold,Italic,Underline,Strikethrough,|,Separator,Blocktag,Fontface,FontSize,FontColor,BackColor,|,Align,List,Outdent,Indent,Separator,|,SelectAll,Removeformat,Print,Preview,Fullscreen,/,|,Cut,Copy,Paste,Pastetext,|,Link,Unlink,Img,Flash,Media,Flv,Table,Emot,Code,|,Quote,map,Page,Source,About',
        //图片上传
        upBtnText: '上传',
        upImgUrl: "Ajax/EditUpload.ashx?com=pic&immediate=1",
        upImgExt: "jpg,jpeg,gif,png,bmp",

        //Flash上传
        upFlashUrl: "Ajax/EditUpload.ashx?com=flash&immediate=1",
        upFlashExt: "swf",

        //视频上传
        upMediaUrl: "Ajax/EditUpload.ashx?com=media&immediate=1",
        upMediaExt: "flv"
    });
   
    //扩展编辑器
    function updateEdit(id) {
        $("#" + id + "").xheditor({
            width: 680,
            height: 200,
            skin: 'nostyle',
            tools: '|,Bold,Italic,Underline,Strikethrough,|,Separator,Blocktag,Fontface,FontSize,FontColor,BackColor,|,Align,List,Outdent,Indent,Separator,|,SelectAll,Removeformat,Print,Preview,Fullscreen,/,|,Cut,Copy,Paste,Pastetext,|,Link,Unlink,Img,Flash,Media,Flv,Table,Emot,Code,|,Quote,map,Page,Source,About',
            //图片上传
            upBtnText: '上传',
            upImgUrl: "Ajax/EditUpload.ashx?com=pic&immediate=1",
            upImgExt: "jpg,jpeg,gif,png,bmp",

            //Flash上传
            upFlashUrl: "Ajax/EditUpload.ashx?com=flash&immediate=1",
            upFlashExt: "swf",

            //视频上传
            upMediaUrl: "Ajax/EditUpload.ashx?com=media&immediate=1",
            upMediaExt: "flv"
        });
    }
    //已存在的扩展项初始化编辑器
    $("textarea[exist='1']").each(function () {
        updateEdit($(this).attr("id"));
    });

    //上传单页缩略图
    $("#uploadImgBtn").uploadify({
        swf: 'Plugin/Uploadify/uploadify.swf',
        uploader: 'Ajax/SwfUpload.ashx',
        postData: { com: 'uploadpagepic', pastpic: $("#pagePic_hide").val(), 'naviname': $("#pageNaviName_hide").val(), 'naviid': $("#pageNaviid_hide").val() },
        transparent: true,
        debug: false,
        buttonText: '选择图片上传',
        width: '100',
        height: '28',
        auto: true,
        cancelImage: 'Plugin/Uploadify/uploadify-cancel.png',
        fileTypeDesc: '格式支持',
        fileTypeExts: '*.gif;*.jpg;*.jgeg;*.bmp;*.png;',
        fileSizeLimit: 10 * 1024, // 10MB
        multi: false, //是否允许多文件上传
        queueID: 'fileQueue',
        queueSizeLimit: 999,
        removeCompleted: false,
        onSelect: function (file) { },
        onUploadStart: function (file) {
            $("#uploadMsg").attr("class", "point ico-remind F B");
            $("#uploadMsg").html("正在上传…");
        },
        onUploadProgress: function (file, fileBytesLoaded, fileTotalBytes, queueBytesLoaded, uploadSize) {
            $("#uploadMsg").html("正在上传…<span class=\"Q\">完成" + Math.round(fileBytesLoaded / fileTotalBytes * 100) + "% <span class=\"R NoB\">(" + Capacity(queueBytesLoaded) + ")</span></span><span class=\"X NoB\"> | (大小:" + Capacity(uploadSize) + ")</span>");
        },
        onUploadSuccess: function (event, response, status) {
            $("#uploadMsg").attr("class", "point ico-via Q B");
            $("#uploadMsg").html("图片上传成功!");
            $("#pageImg_txt").attr("src", "../Upload/Insets/" + response + "");
            $("#pagePic_hide").val(response);
        }, //上传单个文件成功触发
        onSelectError: function (file, errorCode, errorMsg) {
            $("#uploadMsg").attr("class", "point ico-error R");
            if (errorCode == -110) {
                $("#uploadMsg").html("上传失败!文件超出上传限制大小");
            } else {
                $("#uploadMsg").html("上传失败!");
            }
        }, //选择文件有误触发事件
        onUploadComplete: function (file) { }, //批量上传成功触发事件
        onUploadError: function (file, errorCode, errorMsg) {
            alert(errorMsg);
        }
    });



    //判断扩展项是否存在
    function isPageItem() {
        if ($("#pageItemBox:has(div)").length == 0) {
            $("#pageItemMsgBox").show();
            $("#pageItemMsgBox").css("margin", "8px 0px 32px 0px");
        } else {
            $("#pageItemMsgBox").hide();
        }
    }

    //创建添加扩展
    var pageItemNumber = 1;

    //添加扩展项
    $("#pageItemAddBtn").click(function () {
        var ShowHtml = "";

        ShowHtml += "<div>"
        ShowHtml += "<div class=\"title-tab\">";
        ShowHtml += "<span class=\"f_right\">";
        ShowHtml += "<a class=\"R\" rel=\"delPageItemAddBox\" href=\"#\"> [删除] </a>  ";
        ShowHtml += "</span>";

        ShowHtml += "<div class=\"title\"><span class=\"title-ico titico7 F B\">未命名" + pageItemNumber + "</span></div>";
        ShowHtml += "</div>";

        ShowHtml += "<div class=\"clear-6\"></div>";
        ShowHtml += "<p>";
        ShowHtml += "<label class=\"name\"><span class=\"J\">*</span>扩展项别名:</label> <input tabindex=\"1\" value=\"未命名" + pageItemNumber + "\" class=\"input200 isNull\" name=\"pageItemName_txt\" type=\"text\" />";
        ShowHtml += "<span class=\"point ico-remind V\">扩展项名称</span>";
        ShowHtml += "</p>";

        ShowHtml += "<p>";
        ShowHtml += "<label class=\"name\">扩展内容:</label> <span class=\"interval\"><textarea id=\"pageItem_txt" + pageItemNumber + "\" name=\"pageItemContent_txt\" class=\"input400\" tabindex=\"1\" cols=\"\" rows=\"\"></textarea></span>";
        ShowHtml += "</p>";
        ShowHtml += "</div>";

        $("#pageItemBox").append(ShowHtml);

        //初始化编辑器
        updateEdit("pageItem_txt" + pageItemNumber + "");

        //捕获焦点
        $("#pageItem_txt" + pageItemNumber + "").focus();

        isPageItem();

        pageItemNumber++;
        $(this).text("继续再添加");
        return false;
    });
    //删除已存在的扩展项
    $("a[rel='delPageItem']").click(function () {
        var $delObject = $(this);
        if (confirm("确定要删除扩展项 [ " + $delObject.attr("rev") + " ] 吗？")) {
            $.ajax({
                type: "POST",
                url: "Ajax/PageInfo.ashx",
                data: { 'com': 'delpageitem', 'pageitemid': $delObject.attr("href"), 'pagename': $delObject.attr("rev") },
                datatype: "json",
                beforeSend: function (data, textStatus) {
                    showMsg("[ " + $delObject.attr("rev") + " ] 正在删除中 ", true);
                },
                success: function (data, textStatus) {
                    if (data == "1") {
                        $delObject.parent().parent().parent("div").remove();
                    } else {
                        alert("删除失败!");
                    }
                    showMsg("", false);
                },
                error: function (e) { alert("连接失败,请向Eoyoo官方技术人员反馈!"); }
            })
        }
        return false;
    });
    //删除新添加扩展项
    $("a[rel='delPageItemAddBox']").live('click', function () {
        if (confirm("确定要删除该扩展项吗？")) {
            $(this).parent().parent().parent("div").remove();
            isPageItem();
        }
        return false;
    });

    //移动扩展项位置
    $("a[rel='shift']").click(function () {
        //当前对象
        var $nowObject = $(this);
        $.ajax({
            type: "POST",
            url: "Ajax/PageInfo.ashx",
            data: { 'com': $nowObject.attr("href"), 'pageid': $nowObject.attr("pageid"), 'objectid': $nowObject.attr("name"), 'orderid': $nowObject.attr("rev"), 'pagename': $nowObject.attr("pagename") },
            datatype: "json",
            beforeSend: function (data, textStatus) {
                showMsg("[ " + $nowObject.attr("pagename") + " ] 移动中 ", true);
            },
            success: function (data, textStatus) {
                switch (data) {
                    case "0":
                        alert("更新失败");
                        break;
                    case "1":
                        location.href = $("#pageContent_From").attr("action") + "&tab=2";
                        $("#pageInfo_Tab li a[href='2']").click();
                        break;
                    case "2":
                        if ($nowObject.attr("href") == "upward") {
                            alert("已经是最上了!");
                        } else {
                            alert("已经是最下了!");
                        }
                        break;
                }
                showMsg("", false);
            },
            error: function (e) { alert("连接失败,请向Eoyoo官方技术人员反馈!"); }
        })
        return false;
    });
    showTab($("#pageInfo_Tab"));

    //提交判断
    $("form").submit(function () {
        $("input[type='text'],input[type='password'],textarea").trigger("blur");
        $("select").trigger("change");

        ///新增方法填写在此///

        /////

        if ($("form span").hasClass("ico-error")) {
            $("input:submit").attr("disabled", false);
            $("input:submit").next("span").attr("class", "point ico-error R");
            $("input:submit").next("span").text("请按照提示正确操作，检查各项填写");
            setInterval(delSubmitBtnMsg, 3000);
            return false;
        } else {
            $("input:submit").attr("disabled", true);
            $("input:submit").next("span").attr("class", "Q");
            $("input:submit").next("span").html("<img src=\"Images/Loading.gif\" align=\"middle\" /> 正在保存…");
        }
    });

})