<%
Caboose.log(@block.block_type.field_type)

  
update_on_close = false
@block.block_type.children.each do |bt|
  update_on_close = true if bt.field_type == 'image' || bt.field_type == 'file'
end

crumbs = []
b = @block
while b
  href    = b.id == @block.id ? "#" : "/admin/pages/#{b.page_id}/blocks/#{b.id}/edit"
  onclick = b.id == @block.id ? " onclick=\"$('#block_#{@block.id}_block_type_id_container').slideToggle(100, function() { modal.autosize(); });\"" : ''
  text = b.name ? "#{b.block_type.description} (#{b.name})" : b.block_type.description  
  crumbs << "<a href=\"#{href}\"#{onclick}>#{text}</a>"     
  b = b.parent
end

%>
<!--<h2 style='margin-top: 0; padding-top: 0;'>Edit <%= @block.name ? @block.name : @block.block_type.description %></h2>-->
<h2 style='margin-top: 0; padding-top: 0;'><%= raw crumbs.reverse.join(' > ') %></h2>
<p id='block_type_container'><div id='block_<%= @block.id %>_block_type_id'></div></p>
                                         
<% if @block.block_type.use_render_function_for_layout %>
  <!-- Start -->
  <%= raw @block.render(@block, {
    :modal => true,
    :empty_text => '[Empty, click to edit]',
    :editing => true
  }) %>
  <!-- Stop -->
<% elsif @block.block_type.field_type != 'block' %>
  <p><div id='block_<%= @block.id %>_value'></div></p>
<% else %>  
  <% if @block.children.count > 0 %>  
    <% @block.children.each do |b| %>
      <% if b.block_type.field_type != 'block' && b.block_type.field_type != 'richtext' %>
        <p><div id='block_<%= b.id %>_value'></div></p>
      <% else %>
        <%= raw @block.render(b, {
          :modal => true,
          :empty_text => '[Empty, click to edit]',
          :editing => true
        }) %>
      <% end %>
    <% end %>
  <% else %>
    <p>This block doesn't have any content yet. 
      <% if @block.block_type.allow_child_blocks %>
        <a href='/admin/pages/<%= @block.page_id %>/blocks/<%= @block.id %>/new'>Add a child block!</a>
      <% end %>
    </p>
  <% end %>
<% end %>
<div id='message'></div>
<p>  
<input type='button' value='Close' onclick="<% if update_on_close %>parent.controller.render_blocks();<% end %>modal.close();" />
<% if @block.name.nil? %>
  <input type='button' value='Delete Block' onclick="controller.delete_block();" />
<% end %>
<input type='button' value='Move Up'   onclick="controller.move_up();" />
<input type='button' value='Move Down' onclick="controller.move_down();" />
</p>

<% content_for :caboose_css do %>
<style type='text/css'>
.block { border: #ccc 1px dotted; }
#block_<%= @block.id %>_block_type_id_container { }
</style>
<% end %>
<% content_for :caboose_js do %>
<%= javascript_include_tag "caboose/model/all" %>
<%= javascript_include_tag "caboose/admin_block_edit" %>
<script type='text/javascript'>

var modal = false;
$(window).load(function() {
  modal = new CabooseModal(800);
});

<%
#b = @block, nil, { 
#  :include => [
#    :block_type, 
#    :children => { :include => :block_type } 
#  ]
#})
#b.image
%>
    
var controller = false;
$(document).ready(function() {
  controller = new BlockController({
    block: <%= raw Caboose.json(@block.js_hash) %>,    
    authenticity_token: '<%= form_authenticity_token %>'    
  });    
});    

</script>
<% if @block.block_type.field_type == 'richtext' %>
  <%= tinymce_assets %>
  <script type='textjavascript'>
  //document.domain = "<%= raw @cdn_domain %>";
  </script>
  <%= tinymce :caboose, width: '800px', height:'300px' %>
<% end %>

<% end %>