<% content_for :page_specific_css do %>
  <%= stylesheet_link_tag("admin/tags") %>
<% end %>
<% content_for :page_specific_javascript do %>
  <%= javascript_include_tag "validator" %>
<% end %>

<table class="table main-list">
  <thead>
    <tr class="sort-header">
      <% @table_feed_fields.each do |f| %>
      <%= thead(f) %>
      <% end %>
    </tr>
  </thead>
  <tbody>
    <%= render :partial => "feed", :collection => @feeds %>
  </tbody>
  </table>
<% if current_user.is_admin? or current_user.is_manager?(@module_app) %>

  <div class="bottomnav clearfix" style="left: 81px;">
    <div class="action pull-right">
      <a class="btn btn-primary new-feed" href="#">
        <i class="icon-plus"></i> <%= t(:new_) %>
      </a>
    </div>
    <div class="pagination pagination-centered"></div>
  </div>

  <div id="newFeedModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="newFeedModalLabel" aria-hidden="true">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
      <h3 id="newFeedModalLabel">Create New Feed</h3>
    </div>
    <div class="modal-body">
      <%#= render :partial => "feed_form" %>
    </div>
    <div class="modal-footer">
      <button class="btn btn-primary" id="save_new_feed">Save changes</button>
    </div>
</div>
<% end %>

<script type="text/javascript">
 
  $(".new-feed").on("click",function(){
    var modal = $("#newFeedModal");
     modal.find("#newFeedModalLabel").text("Create New Feed");
     modal.modal("show");
     openFeedModal("new",null);
  })

  var bindEditButtons = function(){
    $(".edit-feed").on("click",function(){
      var modal = $("#newFeedModal");
       modal.find("#newFeedModalLabel").text("Edit Feed");
       modal.modal("show");
       openFeedModal("edit",$(this).data("feed-id"));
       return false;
    })
    $(".delete-feed").on("click",function(){
      if(confirm("Are you sure?")){
       var el = $(this);
        $.ajax({
          url : el.attr("href"),
          type : "delete",
          dataType : "html"
        }).done(function(data){
          $("table.main-list tbody").html(data);
          bindEditButtons();
        })
      }
      return false;
    })
  }
  bindEditButtons();
  var openFeedModal = function(type,feed_id){
     $.ajax({
        url : "/admin/custom_announcements/feedform",
        type : "get",
        data : {"type" : type, "id" : feed_id},
        dataType : "html"
      }).done(function(form){
        $("#newFeedModal .modal-body").html(form);
        bindHandlers();
      })
  }

  var bindHandlers = function(){
    $(".tag-checkbox").on("click",function(){
      if($(this).is(":checked")){
        $(this).parent().addClass("active");
      }else{
        $(this).parent().removeClass("active");     
      }
    })

    var fv = new FormValidator($("#newFeedModal form"));
    fv.form.on("submit",function(){
          $.ajax({
            url : fv.form.attr("action"),
            data : fv.form.serializeArray(),
            type : "post",
            dataType : "html"
          }).done(function(data){
            $("table.main-list tbody").html(data);
            bindEditButtons();
            $("#newFeedModal").modal("hide");
            fv.form.resetForm();
            fv.form.find("ul.tags-groups p.active").removeClass("active");
          })
          return false;
    })
    $("#save_new_feed").on("click",function(){
      if(fv.isFormValidated()){
        fv.form.submit();
      }else{
        return false;
      }
    })
  }
</script>
</br>