<% content_for :page_specific_css do %>
  <%= stylesheet_link_tag "lib/main-forms" %>
  <%= stylesheet_link_tag "lib/fileupload" %>
  <%= stylesheet_link_tag "lib/main-list" %>
<% end %>
<style type="text/css">
  .reach_limit{
    background: #a90c0c;
    color: white;
    padding: 4px 12px;
    font-family: 'Varela Round';
    letter-spacing: -.4px;
    cursor: default;
    display: inline-block;
  }
  .add-on.btn-group .dropdown-menu{
    margin: 0;
  }
  .main-forms fieldset .input-area .controls .input-prepend a:hover{
    color: #fff;
  }
</style>
<% content_for :page_specific_javascript do %>
  <%= javascript_include_tag "lib/bootstrap-fileupload" %>
  <%= javascript_include_tag "lib/bootstrap-datetimepicker" %>
  <%= javascript_include_tag "lib/datetimepicker/datetimepicker.js" %>
  <%= javascript_include_tag "lib/file-type" %>
  <%= javascript_include_tag "lib/module-area" %>  
  <%= javascript_include_tag "form" %>
<% end %>
<script type="text/javascript">
  function trigger_on_add_calendar(ele){
    if ($(ele).prop('checked')){
      $('.trigger_on_add_calendar').show()
    }else{
      $('.trigger_on_add_calendar').hide()
    }
  }
</script>
<!-- Input Area -->
<div class="input-area">

  <!-- Module Tabs -->
  <div class="nav-name"><strong><%= t(:module) %></strong></div>
  <ul class="nav nav-pills module-nav">
    <li class="active"><a href="#basic" data-toggle="tab"><%= t(:basic) %></a></li>
    <% if defined? Calendar %>
      <li><a href="#calendar" data-toggle="tab"><%= t('calendar.calendar') %></a></li>
    <% end %>
    <% if((!CustomAnnouncementSetting.first.only_manager_can_edit_status) || (CustomAnnouncementSetting.first.only_manager_can_edit_status && (@current_user.is_admin? || @current_user.is_manager?(@module_app))) ) %>
      <li><a href="#status" data-toggle="tab"><%= t(:status) %></a></li>
    <% end %>
    <li><a href="#tag" data-toggle="tab"><%= t(:tags) %></a></li>
    <li><a href="#imageupload" data-toggle="tab"><%= t('custom_announcement.image') %></a></li>
    <li><a href="#carousel_image_upload" data-toggle="tab" title="<%= t('custom_announcement.carousel_image_title') %>"><%= t('custom_announcement.carousel_image') %></a></li>
    <li><a href="#mail-group" data-toggle="tab"><%= t('custom_announcement.email_reminder')%></a></li>    
  </ul>
  <!-- Module -->
  <div class="tab-content module-area">

    <!-- Basic Module -->
    <div class="tab-pane fade in active" id="basic">

      <!-- Category -->
      <div class="control-group">
        <label class="control-label muted"><%= t(:category) %></label>
        <div class="controls">
          <%= select_category(f, @module_app) %>
        </div>
      </div>

      <!-- Date Time Picker -->
      <div class="control-group">
        <label class="control-label muted"><%= t(:start_date) %></label>
        <div class="controls">
          <%= f.datetime_picker :postdate, :no_label => true, :new_record => @custom_bulletin.new_record?, :data=>{"picker-type" => "range", "range" => "start"} %>
        </div>
      </div>

      <div class="control-group">
        <label class="control-label muted"><%= t(:end_date) %></label>
        <div class="controls">
          <%= f.datetime_picker :deadline, :no_label => true, :new_record => @custom_bulletin.new_record?, :data=>{"picker-type" => "range", "range" => "end"}  %>
        </div>
      </div>

      <div class="control-group">
        <%= f.label :is_external_link, t("custom_announcement.is_external_link"), :class => "control-label muted" %>
        <div class="controls">
          <%= f.check_box :is_external_link %>
        </div>
      </div>

      <div class="control-group" style="display: none;" id="external_link_box">
        <%= f.label :external_link, t("custom_announcement.external_link"), :class => "control-label muted" %>
        <div class="controls">
          <%= f.text_field :external_link %>
          <div class="hint"><%= t("custom_announcement.external_link_hint") %></div>
        </div>
      </div>

      <!-- display subtitle -->
      <div class="control-group">
        <%= f.label :display_subtitle, t("custom_announcement.display_subtitle"), :class => "control-label muted" %>
        <div class="controls">
          <%= f.check_box :display_subtitle %>
        </div>
      </div>

      <!-- display img src -->
      <div class="control-group">
        <%= f.label :display_img, t("custom_announcement.display_img"), :class => "control-label muted" %>
        <div class="controls">
          <%= f.check_box :display_img %>
        </div>
      </div>

      <!-- Image display setting -->
      <% image_display_class_relation = {"full_width"=>"full-size-img","up_left_corner"=>"pull-left","up_right_corner"=>"pull-right"} %>
      <div class="control-group <%='hide' if !f.object.display_img %>" id="image_display_setting">
        <%= f.label :image_display_class, t("custom_announcement.cover_image_display_setting"), :class => "control-label muted" %>
        <div class="controls">
          <% image_display_class_relation.each.with_index do |(key,value),i| %>
            <label>
              <%= radio_button_tag "#{f.object_name}[image_display_class]", value , (f.object.image_display_class == value) %>
              <%= t("custom_announcement.#{key}") %>
            </label>
          <% end %>
        </div>
      </div>
    </div>

    <!-- Calendar Module -->
    <% if defined? Calendar %>
      <div class="tab-pane fade" id="calendar">
        <div class="control-group">
          <label class="control-label muted"><%= t('custom_announcement.add_to_calendar') %></label>
          <div class="controls">
            <%= f.check_box :add_to_calendar,onchange: 'trigger_on_add_calendar(this)' %>
          </div>
        </div>
        <div class="trigger_on_add_calendar" <%= "style=display:none;" if !@custom_bulletin.add_to_calendar %>>
          <div class="control-group">
            <label class="control-label muted"><%= t('calendar.calendar') %></label>
            <div class="controls">
              <%= f.select :calendar_type_id, @calendar_categories.collect{|t| [ t.title, t.id ]} %>
            </div>
          </div>
          <div class="control-group" style="display: flex;flex-wrap: wrap;">
            <div style="display: flex;flex-direction: column;">
              <label class="control-label muted"><%= t(:start_date) %></label>
              <label class="control-label muted"><%= t('custom_announcement.blank_to_set') %></label>
            </div>
            <div class="controls" style="margin-left: 1.5em;">
              <%= f.datetime_picker :calendar_start_date, :new_record => @custom_bulletin.new_record?, :no_label => true, :data=>{"picker-type" => "range", "range" => "start"} %>
            </div>
          </div>
          <div class="control-group" style="display: flex;flex-wrap: wrap;">
            <div style="display: flex;flex-direction: column;">
              <label class="control-label muted"><%= t(:end_date) %></label>
              <label class="control-label muted"><%= t('custom_announcement.blank_to_set') %></label>
            </div>
            <div class="controls" style="margin-left: 1.5em;">
              <%= f.datetime_picker :calendar_end_date, :new_record => @custom_bulletin.new_record?, :no_label => true, :data=>{"picker-type" => "range", "range" => "end"} %>
            </div>
          </div>
          <div class="control-group">
            <label class="control-label muted"><%= t('calendar.all_day') %></label>
            <div class="controls">
              <%= f.check_box :calendar_all_day %>
            </div>
          </div>
          <div class="control-group">
            <label class="control-label muted">Select Read More Page :</label>
            <div class="controls">
              <%= f.select :page_id,[["----- Select a page -----",nil]]+@module_pages %>
            </div>
          </div>
        </div>
        <%= f.hidden_field :event_id %>
      </div>
    <% end %>
    <!-- Status Module -->
    <% if((!CustomAnnouncementSetting.first.only_manager_can_edit_status) || (CustomAnnouncementSetting.first.only_manager_can_edit_status && (@current_user.is_admin? || @current_user.is_manager?(@module_app))) ) %>
      <div class="tab-pane fade" id="status">
        <!-- Status -->
        <div class="control-group">
          <label class="control-label muted"><%= t(:status) %></label>
          <div class="controls" data-toggle="buttons-checkbox">
            <% if !(@reach_limit.include?('is_top') && @custom_bulletin.is_top != true) || current_user.is_admin? %>
              <label class="checkbox inline btn <%= 'active' if @custom_bulletin.is_top? || (!@custom_bulletin.top_end_date.nil? && @custom_bulletin.top_end_date > Time.now) %>">
                <%= f.check_box :is_top %> <%= t(:top) %>
              </label>
            <% else %>
              <label class="reach_limit">
                <%= t(:top) %>
              </label>
            <% end %>
            <% if !(@reach_limit.include?('is_hot') && @custom_bulletin.is_hot != true) || current_user.is_admin? %>
              <label class="checkbox inline btn <%= 'active' if @custom_bulletin.is_hot? %>">
                <%= f.check_box :is_hot %> <%= t(:hot) %>
              </label>
            <% else %>
              <label class="reach_limit">
                <%= t(:hot) %>
              </label>
            <% end %>
            <label class="checkbox inline btn <%= 'active' if @custom_bulletin.is_hidden? %>">
              <%= f.check_box :is_hidden %> <%= t(:hide) %>
            </label>
        </div>
        <div class="controls">
          <% if !@custom_bulletin.is_top? && !CustomAnnouncementSetting.check_limit_for_user((@custom_bulletin.new_record? ? current_user.id : @custom_bulletin.create_user_id)) %>
              <span>Top limit has been reached. The custom_bulletin wont be marked as top even if you click on it.</span>
          <% end %>
        </div>
        </div>
        <div class="control-group <%= @custom_bulletin.is_top? || (!@custom_bulletin.top_end_date.nil? && @custom_bulletin.top_end_date > Time.now) ? "" : "hide" %>" data-for="is_top">
          <label for="" class="control-label muted">Top end time</label>
          <div class="controls">
            <%= f.datetime_picker :top_end_date, :no_label => true, :new_record => @custom_bulletin.new_record? %>
          </div>
        </div>
        <div class="control-group">
          <label class="control-label muted"><%= t('custom_announcement.open_comment')%></label>
          <div class="controls">
            <%= f.check_box :open_comment %><%= t('custom_announcement.open') %>
          </div>
        </div>
        <div class="control-group <%= @custom_bulletin.open_comment ? "" : "hide" %>" data-for="open_comment">
          <label for="" class="control-label muted"><%= t('custom_announcement.comment_end_time')%></label>
          <div class="controls">
            <%= f.datetime_picker :comment_end_time, :no_label => true, :new_record => @custom_bulletin.new_record? %>
          </div>
        </div>
        <div class="control-group <%= @custom_bulletin.open_comment ? "" : "hide" %>" data-for="open_comment">
          <label for="" class="control-label muted"><%= t('custom_announcement.comment_role')%></label>
          <div class="controls">
            <%= check_box_tag 'custom_bulletin[comment_role][]','visitor',@custom_bulletin.comment_role.include?('visitor') %>
            <%= t('custom_announcement.visitor') %>
            <br>
            <%= check_box_tag 'custom_bulletin[comment_role][]','all_member',@custom_bulletin.comment_role.include?('all_member'),class: 'role_all_member' %>
            <%= t('custom_announcement.all_member') %>
            <br>
            <% Role.all.each do |role| %>
              <%= check_box_tag 'custom_bulletin[comment_role][]',role.id.to_s,@custom_bulletin.comment_role.include?(role.id.to_s),class: 'role' %>
              <%= role.title %>
            <% end %>
          </div>
        </div>
      </div>
    <% end %>

    <!-- Tag Module -->
    <div class="tab-pane fade" id="tag">
      <div class="control-group">
        <label class="control-label muted"><%= t(:tags) %></label>
        <%= select_tags(f, @module_app) %>
      </div>
    </div>

    <!-- Images Module -->
    <div class="tab-pane fade" id="imageupload">

      <!-- Images Upload -->
      <div class="control-group">
        <label class="control-label muted"><%= t(:image) %></label>
        <div class="controls">
          <div class="fileupload fileupload-new clearfix <%= 'fileupload-edit' if @custom_bulletin.image.file %>" data-provides="fileupload">
            <div class="fileupload-new thumbnail pull-left">
              <% if @custom_bulletin.image.file %>
                <%= image_tag @custom_bulletin.image %>
              <% else %>
                <img src="http://www.placehold.it/50x50/EFEFEF/AAAAAA" />
              <% end %>
            </div>
            <div class="fileupload-preview fileupload-exists thumbnail pull-left"></div>
            <span class="btn btn-file">
              <span class="fileupload-new"><%= t(:select_image) %></span>
              <span class="fileupload-exists"><%= t(:change) %></span>
              <%= f.file_field :image %>
            </span>
            <a href="#" class="btn fileupload-exists" data-dismiss="fileupload"><%= t(:cancel) %></a>
            <div class="controls" data-toggle="buttons-checkbox">
              <label class="checkbox inline btn btn-danger fileupload-remove">
                <%= f.check_box :remove_image %><%= t(:remove) %>
              </label>
            </div>
          </div>
        </div>
      </div>
        <% @site_in_use_locales.each do |locale| %>
          <%= f.fields_for :image_description_translations do |f| %>
            <div class="control-group">
            <label class="control-label muted" for="image_description_<%= locale.to_s %>"><%= t(:description) + " (#{t(locale.to_s)})" %></label>
              <div class="controls">
                <%= f.text_field locale, value: (@custom_bulletin.image_description_translations[locale.to_s] rescue nil) %> 
              </div>
            </div>
          <% end %>
        <% end %>

    </div>
    <!-- Images Module -->
    <div class="tab-pane fade" id="carousel_image_upload">
      <% if @custom_bulletin && !@custom_bulletin.custom_bulletin_carousel_images.blank? %>
        <div class="exist">
          <% @custom_bulletin.custom_bulletin_carousel_images.each_with_index do |custom_bulletin_carousel_image, i| %>
            <%= f.fields_for :custom_bulletin_carousel_images, custom_bulletin_carousel_image do |f| %>
              <%= render :partial => 'form_image', :object => custom_bulletin_carousel_image, :locals => {:f => f, :i => i} %>
            <% end %>
          <% end %> 
          <hr>
        </div>
      <% end %>
      <!-- Add -->
      <div class="add-target">
      </div>
      <p class="add-btn controls">
        <%= hidden_field_tag 'custom_bulletin_carousel_image_count', @custom_bulletin.custom_bulletin_carousel_images.count %>
        <a id="add_carousel_image" class="trigger btn btn-small btn-primary"><i class="icons-plus"></i> <%= t(:add) %></a>
      </p>
      </div>
    </div>

    <!-- Mail Group Module -->
    <div class="tab-pane fade" id="mail-group">

      <!-- Mail Group -->
      <div class="control-group">
        <label class="control-label muted"><%= t("custom_announcement.email_to") %></label>
        <div class="controls">

          <label class="checkbox inline">
            <%= check_box_tag('custom_bulletin[email_sent]', '1', (!@custom_bulletin.email_sent.blank? ? true : false), :id=>'remind-check') %><%= t('custom_announcement.activate_email_reminder')%>
          </label>

          <div class="content-box">
            <%= render partial: 'admin/member_selects/email_selection_box', locals: {field: 'custom_bulletin[email_member_ids][]', email_members: @custom_bulletin.email_members} %>
          </div>
        </div>
      </div>
      <div class="control-group">
        <label class="control-label muted"></label>
        <div class="controls">
          <div class="content-box">
            <span class="help-block"><%= "#{t("custom_announcement.other_mailaddress")}(#{t("custom_announcement.other_mailaddress_note")})"%> </span>
            <%= f.text_area :other_mailaddress, :class=>"span12", :cols=>"25", :rows=>"10" %>
          </div>
        </div>
      </div> 

      <div class="content-box">
      <div class="control-group">
        <label class="control-label muted"><%= t("custom_announcement.email_sentdate") %></label>
        <div class="controls">
          <%= f.datetime_picker :email_sentdate, :no_label => true %>
        </div>
      </div>
      </div>

      <% if (@custom_bulletin.email.is_sent rescue false) %>
        <div class="content-box">
        <div class="control-group">
          <label class="control-label muted"><%= t("custom_announcement.resend_mail") %></label>
          <div class="controls">
            <input type="checkbox" name="resend_mail" value="true">
          </div>
        </div>
        </div>
      <% end %>

    </div>

  </div>

  <!-- Language Tabs -->
  <div class="nav-name"><strong><%= t(:language) %></strong></div>
  <ul class="nav nav-pills language-nav">
    <% @site_in_use_locales.each_with_index do |locale, i| %>
      <li class="<%= 'active' if i == 0 %>">
        <a data-toggle="tab" href=".<%= locale %>"><%= t(locale) %></a>
      </li>
    <% end %>
  </ul>

  <!-- Language -->
  <div class="tab-content language-area">

    <% @site_in_use_locales.each_with_index do |locale, i| %>
    
      <div class="<%= locale %> tab-pane fade <%= ( i == 0 ) ? "in active" : '' %>">

        <!-- Title-->
        <div class="control-group input-title">
          <label class="control-label muted"><%= t(:title) %></label>
          <div class="controls">
            <%= f.fields_for :title_translations do |f| %>
              <%= f.text_area locale, class: "ckeditor_reduce input-block-level", placeholder: t(:title), value: (@custom_bulletin.title_translations[locale] rescue nil) %>
            <% end %>
          </div>
        </div>

        <!-- Sub Title -->
        <div class="control-group input-subtitle">
          <label class="control-label muted"><%= t(:subtitle) %></label>
          <div class="controls">
            <div class="textarea">
              <%= f.fields_for :subtitle_translations do |f| %>
                <%= f.text_area locale, rows: 2, class: "ckeditor input-block-level", value: (@custom_bulletin.subtitle_translations[locale] rescue nil) %>
              <% end %>
            </div>
          </div>
        </div>

        <!-- Content -->
        <div class="control-group input-content">
          <label class="control-label muted"><%= t(:content) %></label>
          <div class="controls">
            <div class="textarea">
              <%= f.fields_for :text_translations do |f| %>
                <%= f.cktext_area locale, rows: 5, class: "input-block-level", :value => (@custom_bulletin.text_translations[locale] rescue nil) %>
              <% end %>
            </div>
          </div>
        </div>

      </div>

    <% end %>

    <!-- Link -->
    <div class="control-group">
      <label class="control-label muted"><%= t(:link) %></label>
      <div class="controls add-input">

        <!-- Exist -->
        <% if @custom_bulletin && !@custom_bulletin.custom_bulletin_links.blank? %>
          <div class="exist">
            <% @custom_bulletin.custom_bulletin_links.each_with_index do |custom_bulletin_link, i| %>
              <%= f.fields_for :custom_bulletin_links, custom_bulletin_link do |f| %>
                <%= render :partial => 'form_link', :object => custom_bulletin_link, :locals => {:f => f, :i => i} %>
              <% end %>
            <% end %> 
            <hr>
          </div>
        <% end %>

        <!-- Add -->
        <div class="add-target">
        </div>
        <p class="add-btn">
          <%= hidden_field_tag 'custom_bulletin_link_field_count', @custom_bulletin.custom_bulletin_links.count %>
          <a id="add_link" class="trigger btn btn-small btn-primary"><i class="icons-plus"></i> <%= t(:add) %></a>
        </p>

      </div>
    </div>

    <!-- File -->
    <div class="control-group">
      <label class="control-label muted"><%= t(:file_) %></label>
      <div class="controls">
        <div id="file_description_hint"><%= t("custom_announcement.file_description_hint") %></div>
        <!-- Exist -->
        <% if @custom_bulletin && !@custom_bulletin.custom_bulletin_files.blank? %>
          <div class="exist">
            <% @custom_bulletin.custom_bulletin_files.each_with_index do |custom_bulletin_file, i| %>
              <%= f.fields_for :custom_bulletin_files, custom_bulletin_file do |f| %>
                <%= render :partial => 'form_file', :object => custom_bulletin_file, :locals => {:f => f, :i => i} %>
              <% end %>
            <% end %> 
            <hr>
          </div>
        <% end %>

        <!-- Add -->
        <div class="add-target">
        </div>
        <p class="add-btn">
          <%= hidden_field_tag 'custom_bulletin_file_field_count', @custom_bulletin.custom_bulletin_files.count %>
          <a id="add_file" class="trigger btn btn-small btn-primary"><i class="icons-plus"></i> <%= t(:add) %></a>
        </p>

      </div>
    </div>

  </div>

</div>

<!-- Form Actions -->
<div class="form-actions">
  <%= get_referer_url[:action] rescue "" %>
  <%= f.submit t('submit'), class: 'btn btn-primary' %>
  <input type="hidden" name="referer_url" value="<%= get_referer_url %>">
  <%= button_tag t("preview"), id: "button_for_preview", name: "commit", class: 'btn', type: :button %>
  <%= link_to t('cancel'), admin_custom_announcements_path + (params[:custom_module].blank? ? '' : "/#{params[:custom_module]}#{(params[:bind_uid].blank? ? '' : ('-'+params[:bind_uid]))}"), :class=>"btn" %> 
  <% if params[:custom_module].present? %>
    <%= f.hidden_field :custom_module, :value=> params[:custom_module] %>
    <% if params[:bind_uid].present? %>
      <%= f.hidden_field :bind_uid, :value=> params[:bind_uid] %>
    <% end %>
  <% end %> 
</div>

<span id='show_preview'>
  <div class="modal hide fade in banner-preview" id="">
    <div class="modal-header">
      <a class="close" data-dismiss="modal">×</a>
      <h3><%= t(:preview) %></h3>
    </div>
    <div class="modal-body">
      <iframe id="preview-iframe" src=""></iframe>
    </div>
    <div class="modal-footer">
      <a href="#" class="btn" data-dismiss="modal"><%= t(:close) %></a>
    </div>
  </div>
</span>
<% if !@module_app.tags.empty? %>
  <script type="text/javascript">
    $("form.previewable").on("submit", function(){
      if(!$("input[name='custom_bulletin[tags][]']").is(":checked")){
        if(!confirm("You have selected no tag, do you wish to continue?")){
          return false;
        }
      }
    })
  </script>
<% end %>
<style type="text/css">
	[aria-describedby] ul.dropdown-menu {
	    display: block;
	}
	[aria-describedby] .checkbox  input{
		margin-left: 0;
	}
</style>
<script>
  $("#custom_bulletin_display_img").click(function(){$("#image_display_setting").toggleClass("hide")})
  function Appendzero(obj)
  {
    if(obj<10) return "0" +""+ obj;
    else return obj;  
  }
  function add_click_for_privacy()
  {
  	$('.add-on.btn-group').off('show.bs.tooltip').on('show.bs.tooltip',function(){$(this).find('ul').toggleClass('show')});
    $(".privacy-changer-btn").off('click')
    $(".privacy-changer-btn").click(function(){
        var val = $(this).attr('for')
        var parent_node = $(this).parents('.add-on.btn-group').eq(0)
        parent_node.find('.privacy_type').val(val)
        parent_node.find('i').eq(0).attr('class',$(this).find('i').attr('class'))
        parent_node.find('.dropdown-menu').hide()
        window.setTimeout(function(){
                        parent_node.find('.dropdown-menu').css('display','')
                      },1000)
    
  })
  }
  $(function() {
    $('#custom_bulletin_open_comment').change(function(){
      $(this).prop('checked') ? $('div[data-for="open_comment"]').removeClass('hide') : $('div[data-for="open_comment"]').addClass('hide')
    })
    $('.role_all_member').change(function(){
      if ($(this).prop('checked')){
        $('.role').each(function() {
           $(this).prop("checked", true);
        })
      } else{
        $('.role').each(function() {
           $(this).prop("checked", false);
         })
      }
    })
    $('.role').change(function(){
      if (!$(this).prop('checked') && $('.role_all_member').prop('checked')){
        $('.role_all_member').prop('checked',false)
      }
    })
    if (location.pathname.substr(-3)=='new'){
      var getDate = new Date();
      var toDay = getDate.getFullYear()+"/"+ (Appendzero(getDate.getMonth()+1))+"/"+Appendzero(getDate.getDate())+" "+Appendzero(getDate.getHours())+":"+Appendzero(getDate.getMinutes());
      $('input[name="custom_bulletin[postdate]"]').val(toDay);
    }

    $("#main-wrap").after("");

    $(document).on('click', '#add_link', function(){
      var new_id = $(this).prev().attr('value');
      var old_id = new RegExp("new_custom_bulletin_links", "g");
      var on = $('.language-nav li.active').index();
      var le = $(this).parent('.add-btn').prev('.add-target').children('.start-line').length;
      $(this).prev().attr('value', parseInt(new_id) + 1);
      $(this).parent().siblings('.add-target').append(("<%= escape_javascript(add_attribute 'form_link', f, :custom_bulletin_links) %>").replace(old_id, new_id));
      $(this).parent('.add-btn').prev('.add-target').children('.start-line').eq(le).children('.tab-content').children('.tab-pane').eq(on).addClass('in active').siblings().removeClass('in active');
      formTip();
    });
    $(document).on('click', '#add_file', function(){
      var new_id = $(this).prev().attr('value');
      var old_id = new RegExp("new_custom_bulletin_files", "g");
      var on = $('.language-nav li.active').index();
      var le = $(this).parent('.add-btn').prev('.add-target').children('.start-line').length;
      $(this).prev().attr('value', parseInt(new_id) + 1);
      $(this).parent().siblings('.add-target').append(("<%= escape_javascript(add_attribute 'form_file', f, :custom_bulletin_files) %>").replace(old_id, new_id));
      $(this).parent('.add-btn').prev('.add-target').children('.start-line').eq(le).children('.input-append').find('.tab-content').each(function() {
        $(this).children('.tab-pane').eq(on).addClass('in active').siblings().removeClass('in active');
      });
      formTip();
      add_click_for_privacy();
    });
    $(document).on('click', '#add_carousel_image', function(){
      var new_id = $(this).prev().attr('value');
      var old_id = new RegExp("new_custom_bulletin_carousel_images", "g");
      var on = $('.language-nav li.active').index();
      var le = $(this).parent('.add-btn').prev('.add-target').children('.start-line').length;
      $(this).prev().attr('value', parseInt(new_id) + 1);
      $(this).parent().siblings('.add-target').append(("<%= escape_javascript(add_attribute 'form_image', f, :custom_bulletin_carousel_images) %>").replace(old_id, new_id));
      $(this).parent('.add-btn').prev('.add-target').children('.start-line').eq(le).children('.input-append').find('.tab-content').each(function() {
        $(this).children('.tab-pane').eq(on).addClass('in active').siblings().removeClass('in active');
      });
    });
    $(document).on('click', '.fileupload-remove', function(){
      if($(this).find(".delete_image").length != 0){
        $(this).parents('.image_group').remove();
      }
    });
    $(document).on('click', '.delete_link', function(){
      $(this).parents('.input-prepend').remove();
    });
    $(document).on('click', '.delete_file', function(){
      $(this).parents('.input-prepend').remove();
    });
    $(document).on('click', '.remove_existing_record', function(){
      if(confirm("<%= I18n.t(:sure?)%>")){
        $(this).children('.should_destroy').attr('value', 1);
        $(this).parents('.start-line').hide();
      }
    });

    $('#remind-check').prop('checked') ? '':$('.content-box').addClass('hide')
    $('#remind-check').on('change', function() {
        $(this).prop('checked') ? $('.content-box').removeClass('hide'):$('.content-box').addClass('hide')
    })

    $('#button_for_preview').click(function(){
      var method = $('.main-forms input[name="_method"]').val();
      $('.main-forms input[name="_method"]').val("post");
      
      for ( instance in CKEDITOR.instances )
        CKEDITOR.instances[instance].updateElement();

      var formData = new FormData( $('.main-forms')[0] );
      formData.append("preview_type", ( (method==undefined) ? "new" : "edit" ));
      formData.append("custom_bulletin_id", '<%= @custom_bulletin.id.to_s %>');

      $.ajax({
        type: "post",
        url: '<%= admin_custom_announcement_preview_path %>',
        data : formData,
        processData: false,
        contentType: false
      }).done(function(data){
        if(window.location.protocol === "https:"){
          data = data.replace("http:","https:");
        }
        $('.modal-body iframe').attr('src',data);
        $('#show_preview .modal').modal();
        $('#show_preview .modal').height(function() {
          return $(window).height() * 0.7;
        });

        var slug = data.split('/')[(data.split('/').length-1)];
        // $('#preview-iframe').on('load', function(){
        //   $.get('/admin/custom_announcement/destroy_preview/'+slug,function(data){
        //   });
        // });
      });
      $('.main-forms input[name="_method"]').val(method);
      return false;
    });

    $("#custom_bulletin_is_top").parent().on("click",function(){
      setTimeout(function(){
        if($("#custom_bulletin_is_top").parent().hasClass("active")){
          $("div[data-for=is_top]").removeClass("hide");
        }else{
          $("div[data-for=is_top]").addClass("hide");
          $("div[data-for=is_top]").find("input[type=text]").val("");
        }
      },100)
    })

    $("#custom_bulletin_is_external_link").on("click",function(){
      if($(this).is(":checked")){
        $("#external_link_box").show();
      }else{
        $("#external_link_box").hide();
      }
    })
    add_click_for_privacy()
  });
</script>
