<%= content_for(:header_tags) do %>
    <%= import_ke(enable_at: true, prettify: false, init_activity: false) %>
<% end %>
<script type="text/javascript">
    var first_click = true;
    function nh_check_field(params){
        var result=true;
        if(!regexTopicSubject()) {
            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.text("描述不能为空");
                    params.contentmsg.css('color','#ff0000');
                }
                else if(params.content.html().length >=20000){
                    params.contentmsg.text("描述最多20000个汉字(或40000个英文字符)");
                    params.contentmsg.css('color','#ff0000');
                    result=false;
                }
                else
                {
                    params.contentmsg.text("填写正确");
                    params.contentmsg.css('color','#008000');
                }
            }
        }
        return result;
    }
    function init_homework_form(params){
        params.form.submit(function(){
            params.textarea.html(params.editor.html());
            params.editor.sync();
            var flag = false;
            if(params.form.attr('data-remote') != undefined ){
                flag = true
            }
            var is_checked = false;
            is_checked = nh_check_field({
             issubmit:true,
             content:params.editor,
             contentmsg:params.contentmsg,
             textarea:params.textarea
             });
            if(first_click && is_checked){
                if(flag){
                    first_click = false;
                    return true;
                }else{
                    first_click = false;
                    $(this)[0].submit();
                    return false;
                }
            }
            return false;
        });
    }
    function init_homework_editor(params){
        params.textarea.removeAttr('placeholder');
        var editor = params.kindutil.create(params.textarea, {
            resizeType : 1,minWidth:"1px",width:"100%",minHeight:"30px",height:"30px",
            items : ['code','emoticons','fontname',
                'forecolor', 'hilitecolor', 'bold', '|', 'justifyleft', 'justifycenter', 'insertorderedlist','insertunorderedlist', '|',
                'formatblock', 'fontsize', '|','indent', 'outdent',
                '|','imagedirectupload','table',   'media', 'preview',"more"
            ],
            afterChange:function(){//按键事件
                var edit = this.edit;
                var body = edit.doc.body;
                //paramsHeight = params.kindutil.removeUnit(this.height);
                edit.iframe.height(150);
                this.resize(null, Math.max((params.kindutil.IE ? body.scrollHeight : body.offsetHeight) +  33, 150));
            },
            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;
                var body = edit.doc.body;
                edit.html(params.textarea.innerHTML);
                //paramsHeight = params.kindutil.removeUnit(this.height);
                edit.iframe.height(150);
                this.resize(null, Math.max((params.kindutil.IE ? body.scrollHeight : body.offsetHeight) , 150));
                elocalStorage(message_content_editor,'topic_course_<%=course.id %>');
            }
        }).loadPlugin('paste');
        return editor;
    }
    KindEditor.ready(function(K){
        $("div[nhname='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.textarea = $("textarea[nhname='topic_textarea']",params.div_form);
            params.contentmsg = $("#message_content_span");
            params.submit_btn = $("#new_message_submit_btn");
            if(params.textarea.data('init') == undefined) {
                params.editor = init_homework_editor(params);
                message_content_editor = params.editor;
                init_homework_form(params);
                params.submit_btn.click(function () {
                    params.form.submit();
                });
                params.textarea.data('init', 1);
            }
        });
    });
</script>
<%= error_messages_for 'message' %>
<div class="resources mt10">
  <div id="new_course_topic">
    <div class="homepagePostBrief c_grey">
      <div>
        <input  type="text" name="message[subject]" id="message_subject" class="InputBox  w713" maxlength="255" onfocus="$('#topic_editor').show()" onkeyup="regexTopicSubject();" placeholder="发布帖子，请先输入帖子标题" value="<%= topic.subject%>" >
        <p id="subjectmsg"></p>
      </div>
      <div id="topic_editor" style="display: none;">
        <%if User.current.member_of_course?(course)%>
            <div class="mt10">
          <%= f.check_box :sticky, :value => topic.sticky%>
          <%= label_tag 'message_sticky', l(:label_board_sticky) %>
          <%= f.check_box :locked, :value => topic.locked%>
          <%= label_tag 'message_locked', l(:label_board_locked) %>
          <div class="cl"></div>
        </div>
        <% end %>
        <div class="mt10">
          <div id="message_quote" class="wiki" style="width: 92%;word-break: break-all;word-wrap: break-word;margin-left: 40px;"></div>
          <%= text_area :quote,:quote,:style => 'display:none' %>
          <%= hidden_field_tag :asset_id,params[:asset_id],:required => false,:style => 'display:none' %>

          <%#= f.kindeditor :content, :editor_id => 'message_content_editor',
                           :owner_id => topic.nil? ? 0: topic.id,
                           :owner_type => OwnerTypeHelper::MESSAGE,
                           :width => '100%',
                           :height => 300,
                           :minHeight=>300,
                           :class => 'talk_text fl',
                           :input_html => { :id => 'message_content',
                                            :class => 'talk_text fl',
                                            :maxlength => 5000 },
                           at_id: topic.id, at_type: topic.class.to_s
          %>
          <textarea placeholder="有问题或有建议，请直接给我留言吧！" style="display: none" nhname='topic_textarea' name="message[content]"><%=topic.content %></textarea>
          <div class="cl"></div>
          <p id="message_content_span"></p>
          <p id="e_tip" class="c_grey"></p>
          <p id="e_tips" class="c_grey"></p>
        </div>
        <div class="cl"></div>
        <div class="mt10">
          <div class="fl" id="topic_attachments">
            <%= render :partial => 'attachments/form_course', :locals => {:container => topic, :isReply => @isReply} %>
          </div>
        </div>
        <div class="cl"></div>
        <div class="mt5">
          <%if !edit_mode %>
              <a href="javascript:void(0);" class="BlueCirBtnMini fr" id="new_message_submit_btn">确定</a>
              <span class="fr mr10 mt3">或</span>
              <a href="javascript:void(0);" class="fr mr10 mt3" onclick="reset_topic();">取消</a>
          <% else %>
              <a href="javascript:void(0);" class="BlueCirBtnMini fr" id="new_message_submit_btn" onclick="submit_topic();">确定</a>
              <span class="fr mr10 mt3">或</span>
              <%= link_to "取消",board_message_url(topic.board, topic.root, :r => (topic.parent_id && topic.id)), :class => "fr mr10 mt3"%>
        <% end %>
        </div>
        <div class="cl"></div>
      </div>
    </div>
    <%#= render :partial => 'course_new_topic', :locals => {:f => f,  :topic => @message} %>
    <!--<li>
      <div class="ml55 fl" nhname="toolbar_container"></div>
      <a href="javascript:void(0);"  nhname="cancelbtn" class="grey_btn fr ml10"><%#= l(:button_cancel) %></a>
      <a href="javascript:void(0);" nhname="submitbtn" class="blue_btn fr " style="margin-left: 55px">
        <%#= l(:button_submit)%>
      </a>
      <div class="cl"></div>
    </li>-->
  </div>
</div>