var editor2;
function init_des_editor(params){
    // var minHeight;  //最小高度
    var paramsHeight = params.height; //设定的高度
    var paramsWidth = params.width == undefined ? "100%" : params.width;

    var editor = params.kindutil.create(params.textarea, {
        resizeType : 1,minWidth:"1px",width:paramsWidth,
        height:"30px",// paramsHeight == undefined ? "30px":paramsHeight+"px",
        minHeight:"30px",// paramsHeight == undefined ? "30px":paramsHeight+"px",
        items : ['code','emoticons','fontname',
            'forecolor', 'hilitecolor', 'bold', '|', 'justifyleft', 'justifycenter', 'insertorderedlist','insertunorderedlist', '|',
            'formatblock', 'fontsize', '|','indent', 'outdent',
            '|','imagedirectupload','table',   'media', 'preview',"more"
        ],
        afterChange:function(){//按键事件
            //nh_check_field({content:this,contentmsg:params.contentmsg,textarea:params.textarea});
            var edit = this.edit;
            var body = edit.doc.body;
            edit.iframe.height(paramsHeight);
            this.resize(null, Math.max((params.kindutil.IE ? body.scrollHeight : body.offsetHeight) +  33, paramsHeight));
        },
        afterCreate:function(){
            //init
            var edit = this.edit;
            var body = edit.doc.body;
            edit.iframe[0].scroll = 'no';
            body.style.overflowY = 'hidden';
            //reset height
            var edit = this.edit;
            edit.html(params.textarea.innerHTML);
            var body = edit.doc.body;
            paramsHeight = paramsHeight == undefined ? params.kindutil.removeUnit(this.height) : paramsHeight;
            edit.iframe.height(paramsHeight);
            this.resize(null, Math.max((params.kindutil.IE ? body.scrollHeight : body.offsetHeight), paramsHeight));
            elocalStorage(editor2,'org_document');
        }
    }).loadPlugin('paste');
    return editor;
}

function nh_check_field(params){
    var result=true;
    if(!check_org_title()) {
        result=false;
        return result;
    }
    if(params.content!=undefined){
        if(params.content.isEmpty()){
            result=false;
        }
        if(params.content.html()!=params.textarea.html() || params.issubmit==true){
            params.textarea.html(params.content.html());
            params.content.sync();

            if(params.content.isEmpty()){
                params.contentmsg.html('请输入文章描述');
                params.contentmsg.css({color:'#ff0000'});
            }else{
                params.contentmsg.html('填写正确');
                params.contentmsg.css({color:'#008000'});
            }
            }
    }
    return result;
}
function init_form(params){
    params.form.submit(function(){
        var flag = false;
        if(params.form.attr('data-remote') != undefined ){
            flag = true
        }
        var is_checked = nh_check_field({
            issubmit:true,
            //title:params.texttitle,
            content:params.editor,
            //titlemsg:params.titlemsg,
            contentmsg:params.contentmsg,
            textarea:params.textarea
        });
        if(is_checked){
            if(flag){
                return true;
            }else{
                $(this)[0].submit();
                return false;
            }
        }
        return false;
    });
}
function nh_reset_form(params){
    params.form[0].reset();
    $("#document_title").val("");
    $('#org_document_editor').hide();
    $('#doc_title_hint').hide();
    params.textarea.empty();
    if(params.editor != undefined){
        params.editor.html(params.textarea.html());
    }
    params.contentmsg.hide();
    params.titlemsg.html("");
}
//第二个参数是高度，可以传，可以不传
function init_des_data(){
    var height = arguments[0] ? arguments[0] : undefined;
    var width = arguments[1] ? arguments[1] : undefined;
    KindEditor.ready(function (K) {
        $("div[nhname='new_topic_form']").each(function () {
            var params = {};
            params.kindutil = K;
            params.div_form = $(this);
            params.form = $("form", params.div_form);
            if (params.form == undefined || params.form.length == 0) {
                return;
            }
            //params.texttitle = $("#document_title", params.div_form);
            params.textarea = $("textarea[nhname='description_textarea']", params.div_form);
            //params.titlemsg = $("#doc_title_hint", params.div_form);
            params.contentmsg = $("#homework_course_id_span", params.div_form);
            params.cancel_btn = $("#new_topic_cancel_btn");
            params.submit_btn = $("#new_topic_submit_btn");
            params.height = height;
            params.width = width;
            if (params.textarea.data('init') == undefined) {
                params.editor = init_des_editor(params);
                editor2 = params.editor;
                init_form(params);
                params.cancel_btn.click(function () {
                    nh_reset_form(params);
                });
                params.submit_btn.click(function () {
                    params.editor.sync();
                    params.form.submit();
                });
                params.textarea.data('init', 1);
                $(this).show();
            }
        });
    });

    div_form = $("div[nhname='new_topic_form']");
    $(".ke-edit", div_form).css("height","150px");
    $(".ke-edit-iframe",div_form).css("height","150px");
}