<script>
    function g(o){return document.getElementById(o);}
    function HoverLi(n){
//如果有N个标签,就将i<=N;
        for(var i=1;i<=3;i++){
            g('orgSetting_'+i).className='orgSettingOp';
            g('orgContent_'+i).className='undis';}
            g('orgContent_'+n).className='dis ml15 mr15';
          g('orgSetting_'+n).className='orgSettingOp orgOpActive';}
    //如果要做成点击后再转到请将<li>中的onmouseover 改成 onclick;
    //]]>
    $checkName = true;
    function check_uniq(id){
        if($("#organization_name").val().trim() == ""){
            $("#check_name_hint").html('<span class="c_red">名字不能为空<span>').show();
            return false ;
        }
        $.get(
                '<%=  check_uniq_organizations_path%>'+'?org_name='+$("#organization_name").val().trim()+"&config_page=Y" + "&org_id="+id
        )
    }
        function update_org(id, old_value, input_value){
            if (old_value == input_value)
            {
                check_uniq(id);
            }
            if( $checkName){
                $("#edit_organization_"+id).submit();
            }
        }

</script>
<% domain = Secdomain.where("sub_type=2 and pid=?", @organization.id).first %>
    <div class="homepageRightBanner">
      <div class="NewsBannerName">配置</div>
    </div>
    <div class="resources mt10">
      <ul class="mb10">
        <li class="orgSettingOp orgOpActive" id="orgSetting_1" onclick="HoverLi(1);">信息</li>
        <li class="orgSettingOp" id="orgSetting_2" onclick="HoverLi(2);">成员</li>
        <li class="orgSettingOp" id="orgSetting_3" onclick="HoverLi(3);">栏目</li>
        <li class="orgBorder"></li>
        <div class="cl"></div>
      </ul>
      <div class="ml15 mr15" id="orgContent_1">
        <!--<div class="orgLogo mb10"><a href="javascript:void(0);"><img src="images/0" width="55" height="55" alt="组织logo" class="mr10 logoBorder fl ml10" /></a>-->
          <!--<a href="javascript:void(0);" class="logoEnter fl linkGrey4">上传图片</a>-->
        <%#= form_for( @organization,{:controller => 'organizations',:action => 'update',:id=>@organization,:html=>{:id=>'update_org_form',:method=>'put'}}) do %>
        <%= labelled_form_for @organization do |f|%>
        <%= render :partial=>"new_org_avatar_form",:locals=> {source:@organization} %>
          <!--<div class="cl"></div>-->
        <!--</div>-->
        <div class="orgRow mb10"><span class="c_red">*&nbsp;</span>组织名称：<input type="text" name="organization[name]" id="organization_name" maxlength="100" onblur="check_uniq(<%=@organization.id %>);" onfocus="$('#check_name_hint').hide()" class="orgNameInput"  value="<%= @organization.name%>" />
          <div class="cl"></div>
        </div>
            <div style="margin-left: 80px " id="check_name_hint"></div>
        <div class="orgRow mb10"><span class="ml10">组织描述：</span><textarea type="text" name="organization[description]" class="orgDes" id="org_desc"  placeholder="最多3000个汉字（或6000个英文字符）"><%= @organization.description%></textarea>
          <div class="cl"></div>
        </div>
        <div style="margin-left: 80px " id="check_desc_hint"></div>
        <div class="orgRow mb10"><span class="ml10">组织URL：</span>
          <div class="w607 fr">http：//
            <input type="text" name="organization[domain]" id="domain" value="<%= domain.nil? ? '' : domain.subname %>" class="orgUrlInput" onfocus="observe_input_to_lowercase($(this));" />
            .trustie.net<a href="javascript:void(0);" class="linkBlue ml15" style="text-decoration:underline;" onclick="apply_subdomain(<%= @organization.id %>,$('#domain').val());">申请</a>
            <% record = OrgMessage.where("organization_id=? and message_type='ApplySubdomain'", @organization.id).order("updated_at desc").first %>
            <% if domain.present? and record.present? and record.content == domain.subname %>
                <span>(已批准)</span>
            <% elsif record %>
                <span>(您申请了子域名<%= OrgMessage.where("organization_id=? and message_type='ApplySubdomain'", @organization.id).order("updated_at desc").first.content %>，还未批准)</span>
            <% end %>
            <p class="c_green f12" id="apply_hint" ></p></div>
          <!--class="c_green f12" 您的申请已提交，系统会以消息的形式通知您结果 -->
        </div>
        <div class="cl"></div>
        <div class="orgRow mb10 mt5">
          <span style="margin-left:10px;" >显示模式&nbsp;: </span>
          <input type="radio" id="show_mode_ordinary" value="0" name="show_mode"  style="margin-left:5px;" <%= @organization.show_mode == 0 ? "checked" : "" %> />
          <label for="show_mode_ordinary">社区模式一</label>
          <input type="radio" id="show_mode_special" value="1" name="show_mode"  style="margin-left:10px;" <%= @organization.show_mode == 1 ? "checked" : "" %> />
          <label for="show_mode_special">社区模式二</label>
        </div>
        <div class="orgRow mb10 mt5"><span style="margin-left:38px;" >公开&nbsp;: </span>
          <input type="checkbox" id="is_public" onclick="disable_down($(this), $('#allow_download'),$('#allow_down_hint'));"  name="organization[is_public]" <%= @organization.is_public ? 'checked': ''%> class="ml3" />
        </div>
        <div class="orgRow mb10 mt5"><span style="margin-left:10px;">下载支持&nbsp;: </span>
          <input id="allow_download" type="checkbox" style="margin-top:5px;" <%= @organization.is_public? ? "":"DISABLED" %> name="organization[allow_guest_download]" <%= @organization.allow_guest_download ? 'checked': ''%> class="ml3" />
          &nbsp;<span>允许游客下载</span>
          <span class="c_green f12" id="allow_down_hint"><%= @organization.is_public? ? "" : "（私有组织不允许游客下载资源）" %></span>
        </div>
        <% if User.current.admin? %>
                <div class="orgRow mb10 mt5"><span style="margin-left:10px;">允许设置名师榜&nbsp;: </span>
                  <input id="allow_set_excellent_teachers" type="checkbox" style="margin-top:5px;" name="organization[allow_teacher]" <%= @organization.allow_teacher==1 ? 'checked': ''%> class="ml3" />
                </div>
        <% end %>
        <a href="javascript:void(0);" class="saveBtn ml80 db fl" onclick="update_org('<%=@organization.id %>','<%= @organization.name %>', $('#organization_name'));">保存</a>
        <% end %>
      </div>
      <div class="undis ml15 mr15" id="orgContent_2">
        <div class="orgMemberList">
          <ul class="orgListRow">
            <li class="orgListUser fb">用户</li>
            <li class="orgListRole fb">角色</li>
            <div class="cl"></div>
          </ul>
          <div id="org_member_list">
          <%= render :partial=>"org_member_list",:locals=> {:members=>@organization.org_members} %>
          </div>
        </div>
        <div class="fr orgMemContainer">
          <div class="orgMemberAdd">
            <p class="fontBlue fb mb5">添加成员</p>
            <%= form_tag url_for(:controller => 'org_member', :action => 'create', :org => @organization),:id=>'org_member_add_form',:remote=>true do |f|%>
                <input type="text" id="not_org_member_search" name="orgAddSearch" placeholder="支持姓名、邮箱、登录名搜索" class="orgAddSearch mb20" />
                <%# if @organization.secdomain_name.nil? %>
                    <%= javascript_tag "observeSearchfield('not_org_member_search', null, '#{ escape_javascript  host_with_protocol + "/org_member/org_member_autocomplete?" + {:org=> @organization.id}.to_query }')" %>
                <%# else %>
                    <%#= javascript_tag "observeSearchfield('not_org_member_search', null, '#{ escape_javascript  secdomain_with_protocol(@organization.secdomain_name) + "/org_member/org_member_autocomplete?" + {:org=> @organization.id}.to_query }')" %>
                <%# end %>

                <div id="principals_for_new_member">
                  <%= find_user_not_in_current_org_by_name(@project) %>
                </div>
                <div class="cl"></div>
                <ul class="orgAddRole">
                  <li class="fontGrey3 fb mb10">角色</li>
                  <li>
                    <input type="radio" id="orgMng" name="orgRole" value="11" />
                    <label for="orgMng">管理人员</label>
                  </li>
                  <li>
                    <input type="radio" id="orgMeb" name="orgRole" checked value="12" />
                    <label for="orgMeb">组织成员</label>
                  </li>
                </ul><a href="javascript:void(0);" onclick="submit_add_org_members();" class="saveBtn db fl mt10">新增成员</a>
                <% end %>
          </div>
        </div>
        <div class="cl"></div>
      </div>
      <div class="undis ml15 mr15" id="orgContent_3">
<!--新增二级栏目-->
      <div>
        <%= form_tag url_for(:controller => 'org_subfields', :action => 'create', :organization_id => @organization.id), :id=> 'add_subfield_form',:remote => true do %>
            <span class="fontGrey3 fb mb5 mr10" >新增一级栏目</span>
            <input type="text" id="subfield_name" name="name" placeholder="栏目名称" class="orgAddSearch mb10" />
            <div class="mb10">
              <span class="fontGrey3 fb mr10 ml24">域名目录</span>
              <% if domain %>
                  <span><%= domain.subname %>.trustie.net/</span><input id="sub_dir" class="fl personalUrl ml3" type="text" name="sub_dir" onfocus="observe_input_to_lowercase($(this));">
              <% else %>
                  <span class="fontGrey2">(用户自定义url，可选) 您还没有子域名，请先在左侧信息栏申请子域名</span>
              <% end %>
              <div class="cl"></div>
            </div>
            <ul class="orgAddRole mb10">
              <li class="fontGrey3 fb ml24 mr10 fl">栏目类型</li>
              <li class="fl mr15">
                <input type="radio" id="orgMng" value="Post" name="field_type" checked="checked"/>
                <label for="orgMng">帖子</label>
              </li>
              <li class="fl mr15">
                <input type="radio" id="orgMeb" value="Resource" name="field_type" />
                <label for="orgMeb">资源</label>
              </li>
              <div id="org_excellent_teachers">
                <%= render :partial => 'organizations/setting_excellent_teachers' %>
              </div>
              <li class="fr"><a href="javascript:void(0);" class="saveBtn db" onclick="add_org_subfield();">新增</a></li>
              <li class="cl"></li>
            </ul>
        <% end %>
      </div>
<!--over-->
        <div class="orgColumnList" id="org_subfield_list">
          <%= render :partial => 'organizations/subfield_list', :locals => {:subfields => @organization.org_subfields.order("priority")} %>
        </div>
    </div>
</div>
<script>
  function add_org_subfield(){
      if ($("#subfield_name").val().trim() != "")
        $("#add_subfield_form").submit();
  }
  function add_org_subfield_subdomain(){
      if ($("#subfield_name_subdomain").val().trim() != "")
      $("#add_subfield_form_subdomain").submit();
      $("#org_subfield_list").html('<%= escape_javascript(render(:partial => 'organizations/subfield_list', :locals => {:subfields => @organization.org_subfields.order("priority")}  )) %>');
  }
  var $is_exist = false;
  function apply_subdomain(id, domain){
      var reg = new RegExp("^[a-zA-Z0-9_]{1,}$");
      if (reg.exec(domain)){
          $.ajax({
              url: "<%= check_uniq_domain_organizations_path %>",
              type: 'get',
              data:{
                  org_domain:domain
              },
              success:function(){
                  if(!$is_exist){
                      $.ajax({
                          url:"<%= apply_subdomain_organization_path %>",
                          type:'post',
                          data:{
                              id:id,
                              domain:domain
                          },
                          success:function(){
                              $("#apply_hint").text("您的申请已提交，系统会以消息的形式通知您结果");
                          }
                      });
                  }
                  else{
                      $("#apply_hint").text("您申请的子域名已存在，请重新输入");
                  }
              }
          });
      }
      else{
          $("#apply_hint").text("子域名命名不规范，只能包含字母、数字和下划线，请重新输入");
    }
  }

  //输入子域名或者二级目录，自动将输入换成小写
  function observe_input_to_lowercase(id){
      $(id).each(function(){
          var $this = $(this);
          var old_input = $this.val();
          var check=function(){
              var val = $this.val();
              if ( old_input != val){
                  $this.val(val.toLowerCase());
                  old_input = val.toLowerCase();
              }
          };
          var reset = function() {
              if (timer) {
                  clearInterval(timer);
              }
          };
          var timer = setInterval(check, 300);
          $this.bind('blur', reset);
      });
  }
</script>