
/// <summary>
/// 编辑器设定 
/// </summary>
var ke;
KindEditor.ready(function (K) {
    ke = K.create('textarea[name="cuscode"]', {
        allowFileManager: true, resizeType: 0, filterMode: false
    });
});

/// <summary>
/// 选择图片地址 
/// </summary>
KindEditor.ready(function (K) {
    var editor = K.editor({ allowFileManager: true });
    K('.newsimagesrc').click(function () {
        var self = jQuery(this); editor.loadPlugin('image', function () {
            editor.plugin.imageDialog({ imageUrl: self.attr("value"),
                clickFn: function (url, title, width, height, border, align) {
                    self.attr("value", url);
                    $("#imgdefault").attr("src", url);
                    editor.hideDialog();

                }
            });
        });
    });
});
/// <summary>
/// 设置图片最大宽度 
/// </summary>
function getw(self, maxWidth) {
    //    if ($(self).width() > maxWidth) { alert($(self).width()); $(self).width(maxWidth); } else {
    //        
    //    }
}
//幻灯片删除
function delimg(data) {
    showbox("确定要删除", 2, "删除",function () {
        $.ajax({
            url: "/official/script/imgselide_de.ashx?id=" + data,
            type: "GET",
            error: function (i, j, k) { },
            success: function (jon) {
                showbox("删除已成功")
                window.location.reload();
            }
        });    
    })
    

}
$(document).ready(function () {

    /// <summary>
    /// 设置请求提交地址
    /// </summary>
    var acc_boss = getparaFY("mg");
    $($("form")).each(function (i, d) {
        $(this).attr("action", "/" + acc_boss + "/action.ashx");
    })



    var tid = getparaFY("tid");
    //标题
    $.ajax({
        url: "/official/script/runkey.ashx?tid=" + tid + "&key=websitetitles",
        type: "GET",
        error: function (i, j, k) { },
        success: function (jon) {
            $("#websitetitle").val(jon);
            $.ajax({
                url: "/official/script/runkey.ashx?tid=" + tid + "&key=keywordstitle",
                type: "GET",
                error: function (i, j, k) { },
                success: function (jon) {
                    //alert(jon);
                    $("#keywords").val(jon);
                    $.ajax({
                        url: "/official/script/runkey.ashx?tid=" + tid + "&key=descriptiontitle",
                        type: "GET",
                        error: function (i, j, k) { },
                        success: function (jon) {
                            $("#description").val(jon);
                            $.ajax({
                                url: "/official/script/runkey.ashx?tid=" + tid + "&key=bodycolors",
                                type: "GET",
                                error: function (i, j, k) { },
                                success: function (jon) {
                                    $("#bodycolor").val(jon);
                                }
                            });
                        }
                    });
                }
            });
        }
    });




    AjaxForm({ finder: '#imgform', succEnd: 'msg', refurl: '', message: "设置成功！" });
    AjaxForm({ finder: '#codeform', succEnd: 'msg', refurl: '', message: "设置成功！" });
    AjaxForm({ finder: '#fomslide', succEnd: 'msg', refurl: window.location.href, message: "设置成功！" });
    AjaxForm({ finder: '#webtitle', succEnd: 'msg', refurl: window.location.href, message: "设置成功！" });
    /// <summary>
    /// 点击弹出层 
    /// </summary>
    $(".item").click(function () {
        var self = $(this);
        var type = self.attr("data");
        var datatype = self.attr("data-type");
        var datakey = self.attr("data-key");
        call(type, datatype, datakey, $(this));
    });
    /// <summary>
    /// 点击取消按钮
    /// </summary>
    $(".cn").click(function () { $(".cover").hide(); $(".box").hide(); });

    /// <summary>
    /// 自定义图片确定 
    /// </summary>
    $(".okimg").click(function () {

        var _self = $(this);
        $("#imgform").append(crElement("cmd", "offical_config"));
        $("#imgform").append(crElement("tid", _self.attr("tid")));

        $("#imgform").append(crElement("k", _self.attr("datakey") + "_src"));
        $("#imgform").append(crElement("v", $("#newsimagesrc").val()));

        $("#imgform").append(crElement("k1", _self.attr("datakey") + "_href"));
        $("#imgform").append(crElement("v1", $("#newsimagehref").val()));



        $("#imgform").submit();
        $(".cover").hide();
        $(".box").hide();
        $("#newsimagesrc").val("");
        $("#newsimagehref").val("");
        $("#imgform .waitdelete").remove();

    })
    ///添加官网标题
    $("#subupdata").click(function () {
        //alert("11");
        var serf = $(this);
        var websitetitle = $("#websitetitle").val();
        var keywords = $("#keywords").val();
        var tid = getparaFY("tid");
        var description = $("#description").val();
        var bodycolor = $("#bodycolor").val();

        $("#webtitle").append(crElement("cmd", "official_website"));

        $("#webtitle").append(crElement("tid", tid));

        $("#webtitle").append(crElement("keywordstitle", "keywordstitle"));
        $("#webtitle").append(crElement("keywords", keywords));

        $("#webtitle").append(crElement("websitetitles", "websitetitles"));
        $("#webtitle").append(crElement("websitetitle", websitetitle));

        $("#webtitle").append(crElement("descriptiontitle", "descriptiontitle"));
        $("#webtitle").append(crElement("description", description));

        $("#webtitle").append(crElement("bodycolors", "bodycolors"));
        $("#webtitle").append(crElement("bodycolor", bodycolor));

        $("#webtitle").submit();

    })

    /// <summary>
    /// 自定义代码确定 
    /// </summary>
    $("#okcode").click(function () {
        $("#codeform").remove(".waitdelete");
        var _self = $(this);
        $("#codeform").append(crElement("cmd", "offical_config"));
        $("#codeform").append(crElement("tid", _self.attr("tid")));

        $("#codeform").append(crElement("k", _self.attr("datakey")));
        $("#codeform").append(crElement("v", ke.html()));

        $("#codeform").append(crElement("k1", "none"));
        $("#codeform").append(crElement("v1", "none"));

        $("#codeform").submit();
        $(".cover").hide();
        $(".box").hide();
        ke.html("")
        $("#codeform .waitdelete").remove();
    })
    //添加幻灯片
    $("#okslide").click(function () {
        var title = $("#title").val();
        var src = $("#src").val();
        var href = $("#href").val();
        var tid = getparaFY("tid");

        $("#fomslide").append(crElement("cmd", "offiacl_news_selide"));
        $("#fomslide").append(crElement("tid", tid));
        //        $("#fomslide").append(crElement("title", title));
        //        $("#fomslide").append(crElement("src", title));
        //        $("#fomslide").append(crElement("href", href));

        $("#fomslide").submit();
        $("#codeform .waitdelete").remove();
        //        $(".cover").hide();
        //        $(".box").hide();
        $("#title").val("");
        $("#src").val("");
        $("#href").val("");

        //        var img = document.createElement("img");
        //        img.setAttribute("src", $("#src").val())
        //        $(img).appendTo($("#huandengpian"))
        //alert($("#fomslide").html());
    });
})



/// <summary>
/// 创建隐藏域 
/// </summary>
function crElement(name, value) {
    var input = document.createElement("input");
    input.setAttribute("type", "hidden");
    input.setAttribute("class", "waitdelete");
    input.setAttribute("name", name);

    input.setAttribute("value", value);
    return input;
}


/// <summary>
/// 调取弹出框 
/// </summary>
function call(type, datatype, datakey, self) {
    $(".cover").height($(document.body).height()).show();
    $(".box").css("left", $(document.body).width() / 2 - 320);
    var gid = getparaFY("gid");
    var tid = getparaFY("tid");
    var marg = getparaFY("mg");
    switch (type) {
        case "slide":
            $.ajax({
                url: "/official/script/imgselide.ashx?tid=" + tid,
                dataType: "jsonp",
                type: "GET",
                jsonp: 'callback',
                error: function (i, j, k) { alert(k); },
                success: function (jon) {
                    var _img_html = ""; 
                    $(jon).each(function (i, d) {
                        _img_html += '<img onclick="delimg('+d.ID+')" class="imgslide" src="' + d.Official_src + '" />'; 
                    }) 
                    $("#huandengpian").html(_img_html);
                    $("#inputslide").show();
                  

                }
            });
            return;
        case "news":
            var mo = getparaFY("mo");
            window.location.href = "/" + marg + "/master.aspx?view=offical_news&tid=" + tid + "&gid=" + gid + '&mo=' + mo;
            return;
        case "code":
            $.ajax({
                url: "/official/script/runkey.ashx?tid=" + tid + "&key=" + datakey,
                type: "GET",
                error: function (i, j, k) {  },
                success: function (jon) {
                    $("#okcode").attr("tid", tid).attr("gid", gid).attr("datatype", datatype).attr("datakey", datakey);
                    ke.html(jon);
                    $("#inputcode").show();
                }
            });

            return;
        case "img":
            $.ajax({
                url: "/official/script/runkey.ashx?tid=" + tid + "&key=" + (datakey + "_src"),
                type: "GET",
                error: function (i, j, k) {  },
                success: function (jon) {
                    $("#newsimagesrc").val(jon);
                    if (jon != "Setting Not Found") {
                        $("#imgdefault").attr("src", jon);
                    } else {
                        $("#imgdefault").attr("src", "../" + self.attr("defimg"));
                    }

                    $.ajax({
                        url: "/official/script/runkey.ashx?tid=" + tid + "&key=" + (datakey + "_href"),
                        type: "GET",
                        error: function (i, j, k) { alert(k) },
                        success: function (jon) {
                            $("#newsimagehref").val(jon);
                            $("#inputimg").show();
                            $("#okimg").attr("tid", tid).attr("gid", gid).attr("datatype", datatype).attr("datakey", datakey);
                        }
                    });
                }
            });
            return;

    }

}
