$(function () {
    //编辑器自定义
    $("#productContent_txt").xheditor({
        width: 680,
        height: 180,
        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"
    });

    //批量上传缩略图
    $("#uploadImgBtn").uploadify({
        swf: 'Plugin/Uploadify/uploadify.swf',
        uploader: 'Ajax/SwfUpload.ashx',
        postData: { com: 'uploadproductpic', 'proid': $("#productID_hide").val(), 'naviname': $("#naviname_hide").val(), 'naviid': $("#naviid_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: true, //是否允许多文件上传
        queueID: 'fileQueue',
        queueSizeLimit: 999,
        removeCompleted: false,
        onSelect: function (file) {
            var ShowHtml = "";
            ShowHtml += "<li id=\"" + file.id + "\">";
            ShowHtml += "<img class=\"small-images\" src=\"images/time-large-pic.gif\" />";
            ShowHtml += "<div class=\"clear-6\"></div>";
            ShowHtml += "<span class=\"V line16\">" + file.name + "</span>";
            ShowHtml += "</li>";
            $("#uploadPicSetBox").append(ShowHtml);
            
        },
        onUploadStart: function (file) {
            $("#noneData").hide();
            $("#uploadMsg").attr("class", "point ico-remind F B");
            $("#uploadMsg").html("正在上传…<span class=\"Q NoB\">[" + file.name + "]</span>");
        },
        onUploadProgress: function (file, fileBytesLoaded, fileTotalBytes, queueBytesLoaded, uploadSize) {
            $("#uploadMsg").html("上传进度 :<span class=\"M NoB\"> [ " + cutStr(file.name, 20) + " ] </span> <span class=\"Q\">完成" + Math.round(fileBytesLoaded / fileTotalBytes * 100) + "% <span class=\"R NoB\">(" + Capacity(queueBytesLoaded) + ")</span></span><span class=\"X NoB\">(总大小:" + Capacity(uploadSize) + ")</span>");

            var ShowHtml = "";
            ShowHtml += "<p class=\"uploading\"><span style=\"width:" + Math.round(fileBytesLoaded / fileTotalBytes * 100) + "%\">" + Math.round(fileBytesLoaded / fileTotalBytes * 100) + "%</span></p>";
            ShowHtml += "<img class=\"small-images\" src=\"images/upload-large-pic.gif\" />";
            ShowHtml += "<div class=\"clear-6\"></div>";
            ShowHtml += "<span class=\"X line16\">" + file.name + "</span>";
            $("#" + file.id + "").html(ShowHtml);
        },
        onUploadSuccess: function (event, response, status) {
            $("#uploadMsg").attr("class", "point ico-via Q B");
            $("#uploadMsg").html("图片上传成功!");

            var ShowHtml = "";
            ShowHtml += "<a class=\"del\" rel=\"delPicSet\" href=\"" + response.split("|")[1] + "-" + response.split("|")[0] + "\">删</a>";
            ShowHtml += "<a class=\"fm\" rel=\"fengPicSet\" href=\"" + response.split("|")[0] + "\">封</a>";
            ShowHtml += "<img class=\"small-border-images\" src=\"/Upload/Insets/" + response.split("|")[0] + "\" />";
            ShowHtml += "<div class=\"clear-6\"></div>";
            ShowHtml += "<span class=\"E line16\">" + event.name + "</span>";
            $("#" + event.id + "").html(ShowHtml);

            //放入隐藏ID
            $("#productPicSet_hide").val($("#productPicSet_hide").val() + response.split("|")[1] + ",");
        }, //上传单个文件成功触发
        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);
        }
    });

    //删除图片
    $("a[rel='delPicSet']").live("click", function () {
        var $delObject = $(this);
        if (confirm("确定要删除该图片吗？")) {
            var isFengmian = "0"
            if ($("#productPic_hide").val() == $delObject.attr("href").split('-')[1]) {
                isFengmian = "1";
                $("#productPic_hide").val("");
                $("#productImg_txt").attr("src", "images/none-large-pic.gif");
            }
            $.ajax({
                type: "POST",
                url: "Ajax/Product.ashx",
                data: { 'com': 'delpicset', 'picid': $delObject.attr("href").split('-')[0], 'picfile': $delObject.attr("href").split('-')[1], 'proid': $("#productID_hide").val(), 'fengmian': isFengmian },
                datatype: "json",
                beforeSend: function (data, textStatus) { },
                success: function (data, textStatus) {
                    if (data == "1") {
                        $delObject.parent().remove();
                        if ($("#uploadPicSetBox li").length == 0) $("#noneData").show();
                    }
                },
                error: function (e) { alert("连接失败,请向Eoyoo官方技术人员反馈!"); }
            });

        }
        return false;
    });

    //设置为封面
    $("a[rel='fengPicSet']").live("click", function () {
        var $object = $(this);
        if ($("#productID_hide").val() != "0") {
            $.ajax({
                type: "POST",
                url: "Ajax/Product.ashx",
                data: { 'com': 'updatepic', 'proid': $("#productID_hide").val(), 'picfile': $object.attr("href") },
                datatype: "json",
                beforeSend: function (data, textStatus) { },
                success: function (data, textStatus) {
                    if (data == "1") {
                        $("#productPic_hide").val($object.attr("href"));
                        $("#productImg_txt").attr("src", "../Upload/Insets/" + $object.attr("href"));
                    }
                },
                error: function (e) { alert("连接失败,请向Eoyoo官方技术人员反馈!"); }
            });
        } else {
            $("#productPic_hide").val($object.attr("href"));
            $("#productImg_txt").attr("src", "../Upload/Insets/" + $object.attr("href"));
        }
        return false;
    });

    //标题颜色
    $("#colorBox a").click(function () {
        if ($("#proicleTitle_text").val() != "") {
            $("#proicleTitle_text").css("color", $(this).attr("href"));
            $("#productTitleColor_hide").val($(this).attr("href"));
        } else {
            alert("请先输入产品名称!");
        }
        return false;
    });
    //标题加粗
    $("#productTitleisbold_chk").click(function () {
        if ($("#proicleTitle_text").val() != "") {
            if ($(this).attr("checked") == true) {
                $("#proicleTitle_text").css("font-weight", "bold");
            } else {
                $("#proicleTitle_text").css("font-weight", "100");
            }
        } else {
            alert("请先输入产品名称!");
        }
    });

    showTab($("#productAdd_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\" /> 正在保存…");
        }
    });

});
