<%= javascript_include_tag 'new_user' %>
<%= stylesheet_link_tag 'public'%>
<script>
  function add_class(id){
      if($("#"+id).hasClass("sortArrowActiveD")){
          $("#"+id).removeClass("sortArrowActiveD");
          $("#"+id).addClass("sortArrowActiveU");
          $.ajax({
              type: 'GET',
              url: '<%= forums_path %>' + '.js?' + id + '=asc&page='+<%= @forums_pages.page%>

          });
      }else if($("#"+id).hasClass("sortArrowActiveU")){
          $("#"+id).removeClass("sortArrowActiveU");
          $("#"+id).addClass("sortArrowActiveD");
          $.ajax({
              type: 'GET',
              url: '<%= forums_path %>' + '.js?' + id + '=desc&page='+<%= @forums_pages.page%>

          });
      }else{
          $("#"+id).addClass("sortArrowActiveD");
          $.ajax({
              type: 'GET',
              url: '<%= forums_path %>' + '.js?' + id + '=desc&page='+<%= @forums_pages.page%>

          });
      }
  }
  $(function() {
      $("#complex").click(function(){
          $("#reorder_popu").removeClass("sortArrowActiveU");
          $("#reorder_popu").removeClass("sortArrowActiveD");
          $("#reorder_time").removeClass("sortArrowActiveD");
          $("#reorder_time").removeClass("sortArrowActiveU");
          add_class("reorder_complex");
      });
      $("#popu").click(function () {
          $("#reorder_complex").removeClass("sortArrowActiveD");
          $("#reorder_complex").removeClass("sortArrowActiveU");
          $("#reorder_time").removeClass("sortArrowActiveD");
          $("#reorder_time").removeClass("sortArrowActiveU");
          add_class("reorder_popu");
      });
      $("#time").click(function () {
          $("#reorder_complex").removeClass("sortArrowActiveD");
          $("#reorder_complex").removeClass("sortArrowActiveU");
          $("#reorder_popu").removeClass("sortArrowActiveU");
          $("#reorder_popu").removeClass("sortArrowActiveD");
          add_class("reorder_time");
      });
      $('#forum_name').keypress( function(e)
      {
          var n = 0;
          var str = this.value;
          for (i = 0; i < str.length; i++) {
              var leg = str.charCodeAt(i);//ASCII码
              if (leg > 255) {//大于255的都是中文
                  n += 2;//如果是中文就是2个字节
              } else {
                  n += 1;//英文，不多说了
              }
          }
          if(n >= 160 && e.keyCode != 8)
              if(document.all)
              {
                  e.returnValue = false;
              }
              else
              {
                  e.preventDefault();
              }
      })
  });
  function check_and_submit(doc){
      $("#error").html('').hide();
      check_forum_name();
    if(  $("textarea[name='forum[name]']").val().trim() == "" &&  $("textarea[name='forum[description]']").val().trim()  != "" ){
        $("#error").html("名称不能为空").show();
        return;
    } else if( $("textarea[name='forum[description]']").val().trim()  == "" && $("textarea[name='forum[name]']").val().trim() != "" ){
        $("#error").html("描述不能为空").show();
        return;
    }else if($("textarea[name='forum[description]']").val().trim()  == "" && $("textarea[name='forum[name]']").val().trim() == ""){
        $("#error").html("名称和描述不能为空").show();
        return;
    }
    else{
        doc.parent().parent().submit();
    }
  }
  var check_pass = true;
  function check_forum_name(){
      check_pass = true;
      name = $("textarea[name='forum[name]']").val().trim();
      if( name != ""){
          $.get(
              '<%= check_forum_name_forums_path %>',
              {"forum_name":encodeURIComponent(name)},
             function(data){

                 if( data == 'true'){
                     $("#error").html("贴吧名称已经存在").show();
                     check_pass = false;
                 }
              }
          );
      }else{
          check_pass = false;
      }
      if(check_pass == false){
          return ;
      }
  }

  function hideError(){
      $("#error").html("").hide();
  }

  function create_cancle(){
      $('#error').hide();
      $('#new_forum_div').slideToggle();
      $('#create_btn').parent().slideToggle();
      clear_form();
  }
  function clear_form(){
      $("#forum_name").val('');
      $("#forum_desc").val('');
  }
</script>

<div class="homepageContentContainer mb10">
    <div class="homepageContent">
    <div class="postContainer">
     <div id="new_forum_div" class="mb5" style="display: none">
       <div class="red fl mb10" id="error" style="display: none">error</div>
       <%= form_tag({:controller => 'forums',:action=>'create',:format=>'js'},:method => 'post',:remote=>'true') do  |f| %>
       <div>
         <textarea type="text" name="forum[name]"  id="forum_name" maxlength="80"  class="postCreateInput" onfocus="hideError();" onblur="check_forum_name();" placeholder="输入贴吧名称"  ></textarea>
         <script>
           var textarea = document.getElementById('forum_name');
               autoTextarea(textarea);
         </script>
       </div>
       <div class="mt15">
         <textarea type="text" name="forum[description]" id="forum_desc" maxlength="5000" class="postCreateInput"  placeholder="输入贴吧描述"></textarea>
         <script>
             var textarea1 = document.getElementById('forum_desc');
             autoTextarea(textarea1);
         </script>
       </div>
       <div class="mt10">
         <div class="fr"><a href="javascript:void(0);" class="submit_btn" onclick="check_and_submit($(this));">确定</a></div>
         <div class="fr"><a href="javascript:void(0);" class="linkGrey2 mr10" onclick="create_cancle();">取消</a><span class="mr10 fontGrey">或</span></div>
         <div class="cl"></div>
       </div>
       <% end %>
     </div>
    <div class="postBanner" id="postBanner">
      <%= render :partial => 'post_banner' ,:locals => {:order_type=>@type,:order_str=>@str}%>
      <!--<div class="postSort" id="complex"><a href="javascript:void(0);" class="linkGrey2 fl">综合</a><a href="javascript:void(0);" id="reorder_complex" class="sortArrowActiveD"></a></div>-->
      <!--<div class="postSort" id="popu"><a href="javascript:void(0);" class="linkGrey2 fl">人气</a><a href="javascript:void(0)"  id="reorder_popu" ></a></div>-->
      <!--<div class="postSort" id="time"><a href="javascript:void(0);" class="linkGrey2 fl">时间</a><a href="javascript:void(0)"  id="reorder_time" ></a></div>-->
      <!--<div class="creatPost"><a href="javascript:void(0);" id="create_btn" class="c_white db creatPostIcon bBlue" onclick="$('#error').hide();$('#new_forum_div').slideToggle();$(this).parent().slideToggle();">新建贴吧</a></div>-->
      <!--<div class="cl"></div>-->

     </div>
      <div id="forum_list">
      <%= render :partial => 'forum_list',:locals => {:forums=>@forums}%>
      </div>
      <ul class="wlist fr" id="pages" style="margin-top: 10px;">
        <%= pagination_links_full @forums_pages, @forums_count ,:per_page_links => true,:remote =>true,:flag=>true%>
      </ul>
    <div class="cl"></div>
    </div>
    </div>
</div>
