<%= f.fields_for :easy_custom_menus, f.object.easy_custom_menus.select(&:root?) do |m| %>
  <div class="top-item">
    <%= render :partial => 'easy_custom_menus/easy_custom_menu_fields', :locals => { :f => m, :is_top_item => true } %>

    <%= m.fields_for :submenus, m.object.submenus do |m2| %>
      <%= render :partial => 'easy_custom_menus/easy_custom_menu_fields', :locals => { :f => m2, :is_top_item => false } %>
    <% end %>
  </div>
<% end %>

<p>
  <%= link_to_add_association l(:button_add), f, :easy_custom_menus, :class => 'icon icon-add button', :partial => 'easy_custom_menus/easy_custom_menu_fields', :render_options => { :locals => { :is_top_item => true, :cocoon_template => true } } %>
</p>

<script type="text/javascript"  charset="utf-8" >
  $(document).on('cocoon:after-insert', function(event, i) {
    var $item = i.find(".easy-autocomplete-tag[data-easy-autocomplete]");
    var autocompleteSource = $.base64.decode($item.data().easyAutocomplete, true);
    eval(autocompleteSource.replace(/(.*easyAutocomplete\(')([a-z_\-]+)(.*)/, "$1"+i.find("input.easy-icon-select")[0].nextElementSibling.id+"$3"));
    $item.data().autocompleteLoaded = true;
  });
</script>
