<script type="text/javascript">
  $(function() {
    <% page_params[:page].zones.each do |page_zone| %>
      <% zone = page_zone.zone_definition -%>
      PageLayout.initSortable({
        tab_id: <%= page_params[:current_tab] ? page_params[:current_tab].id : 0 %>,
        tabPos: <%= (params[:t] || 1).to_i %>,
        zoneName: "<%= zone.zone_name.dasherize %>",
        updateUrl: "<%= url_for(page_params[:url_order_module].merge(:zone_id => page_zone.id, :t => params[:t])).html_safe %>"
      });
    <% end -%>
    PageLayout.refreshAddModule();
  });
</script>
<%= form_tag(page_params[:url_save_modules], {:id => 'easy-page_modules-settings-form', :class => 'hidden'}) do -%>
  <%= hidden_field_tag 'back_url', params[:back_url] || page_params[:back_url] -%>
  <%= hidden_field_tag 't', params[:t] || 1 -%>
<% end -%>

<%= link_to l(:button_save_easy_page), 'javascript:void(0);', :onclick => 'PageLayout.prepareSubmitModules();PageLayout.submitModules()', :title => l(:title_button_save_easy_page), :class => 'button-positive save-modules-back icon icon-save' %>
<% if page_params[:page_template] %>
  <%= link_to l(:button_easy_page_add_tab),
    url_for({
      :controller => 'easy_page_template_layout',
      :action => 'add_tab',
      :page_template_id => page_params[:page_template],
      :entity_id => page_params[:entity_id],
      :t => params[:t],
      :original_url => url_for(params)
    }), :remote => true,
    :class => 'button add-tab-button icon icon-add', :title => l(:title_easy_page_add_tab), :data => {:disable_with => l(:label_adding)} %>

  <%= link_to l(:button_duplicate), new_easy_page_template_path(page_id: page_params[:page], copy_from_type: 'template', copy_from_template_id: page_params[:page_template]), class: 'button icon icon-page' %>
<% else %>
  <%= link_to l(:button_easy_page_add_tab),
    url_for({
      :controller => 'easy_page_layout',
      :action => 'add_tab',
      :page_id => page_params[:page],
      :user_id => page_params[:user_id],
      :entity_id => page_params[:entity_id],
      :t => params[:t],
      :original_url => url_for(params)
    }), :remote => true,
    :class => 'button add-tab-button  icon icon-add', :title => l(:title_easy_page_add_tab), :data => {:disable_with => l(:label_adding)} %>

    <% if page_params[:page].has_template? %>
      <%= link_to l(:button_easy_page_create_template), new_easy_page_template_path(page_id: page_params[:page], copy_from_type: 'regular_page', copy_from_user_id: page_params[:user_id], copy_from_entity_id: page_params[:entity_id]), class: 'button icon icon-page' %>
    <% end %>
<% end %>
<% if @project.present? %>
  <%
     page_template_options = options_for_select(
         [[l(:title_easy_page_apply_template),nil]].concat(easy_page_context[:page_params][:page].templates.map { |t| [t.template_name, t.id] })
     )
  %>
  <div style="display: inline-block;">
      <%= form_tag({ controller: 'easy_page_layout', action: 'layout_from_template_selected_projects' }) do %>
        <%= hidden_field_tag 'back_url', params[:back_url] || page_params[:back_url] %>
        <%= hidden_field_tag 'projects[]', [@project.id] %>
        <%= select_tag 'page_template_id', page_template_options,
                       onchange: "if(confirm('#{l(:text_are_you_sure)}')) { $(this).parent('form').submit(); }"
        %>
    <% end %>
  </div>
<% end %>

