<%= semantic_form_for [:admin, @guide, @latest_edition] do |f| %>
  <%= f.inputs do %>
    <%= f.input :title %>
    <%= f.input :introduction %>
  <% end %>

  <div id="parts">
    <h2>Parts</h2>

    <% setup_association(f.object, :parts, :new => 3, :edit => 3) %>

    <%= f.semantic_fields_for :parts do |part| %>
      <%= render :partial => 'part', :locals => {:f => part} %>
    <% end %>

  </div>

  <p class="button"><a href="#" class="add-associated" data-target="parts" data-tmpl_id="tmpl-parts">+ Part</a></p>

  <%= f.buttons %>

  <%= f.add_associated_jquery_template :parts %>
<% end %>

<% content_for :extra_headers do %>
<style type="text/css" media="screen">
  #parts fieldset {
    border: 1px solid #ccc;
    padding: 0.5em;
    margin: 0.5em;
  }
</style>
<% end %>

<% content_for :extra_javascript do %>
<script type="text/javascript" charset="utf-8">
  $(function () { $("#parts").accordion(); });
</script>
<% end %>
<%= semantic_form_for [:admin, @guide, @latest_edition] do |f| %>
  <%= f.inputs do %>
    <%= f.input :title %>
    <%= f.input :introduction %>
  <% end %>

  <h2>Parts</h2>

  <div id="parts">

    <% setup_association(f.object, :parts, :new => 3, :edit => 3) %>

    <%= f.semantic_fields_for :parts do |part| %>
      <%= render :partial => 'part', :locals => {:f => part} %>
    <% end %>

  </div>

  <p class="button"><a href="#" class="add-associated" data-target="parts" data-tmpl_id="tmpl-parts">+ Part</a></p>

  <%= f.buttons %>

  <%= f.add_associated_jquery_template :parts %>
<% end %>

<% content_for :extra_headers do %>
<style type="text/css" media="screen">
  #parts fieldset {
    border: 1px solid #ccc;
    padding: 0.5em;
    margin: 0.5em;
  }
</style>
<% end %>

<% content_for :extra_javascript do %>
<script type="text/javascript">
  $(function () { 
    $("#parts").accordion({'collapsible': true}); 
    $('.add-associated').bind('associated-added', function () {
      var active_index = $('#parts div.part').length;
      $('#parts').accordion( "destroy" ).accordion({'collapsible': true, active: active_index - 1});
    });
  });
</script>
<% end %>
Add event handler for associated forms (for parts) being added. They should reinitialise the accordion with the new part open
Add event handler for associated forms (for parts) being added. They should reinitialise the accordion with the new part open
