<%- if @saved -%>
  TracksPages.page_notify('notice', '<%=  escape_javascript(t('todos.deleted_success')) %>', 5);
  TracksPages.set_page_badge(<%=@down_count%>);
  remove_todo_from_page();
  show_new_todo_if_todo_was_recurring();
  activate_pending_todos();
  update_predecessors();
  show_empty_messages();
<%- else -%>
  TracksPages.page_notify('error', "<%= t('todos.error_deleting_item', :description => @todo.description) %>", 8);
<%- end -%>

<% if @saved
  # do not send the js in case of an error
-%>

function show_empty_messages() {
  <% if @old_due_empty -%>
    $('#empty_<%=@original_item_due_id%>').slideDown(1000);
  <% end -%>

  <% if empty_container_msg_div_id && todo_container_is_empty -%>
      $('#<%=empty_container_msg_div_id%>').slideDown(1000);
      <% if @down_count == 0 -%>
        $('#no_todos_in_view').slideDown(1000);
      <% end -%>
  <% end -%>

  <% if source_view_is(:deferred) && @down_count==0 -%>
    $('#tickler-empty-nd').slideDown(100);
  <% end -%>
}

function remove_todo_from_page() {
  <% if (@remaining_in_context == 0) && update_needs_to_hide_context
       # remove context with deleted todo
  -%>
       $('#c<%=@todo.context_id%>').fadeOut(400, function() {
         $('#<%=dom_id(@todo)%>').remove();
       });
       <%= show_empty_message_in_source_container -%>
  <% else
       # remove only the todo
  -%>
       <%= show_empty_message_in_source_container %>
       $('#<%=dom_id(@todo)%>').slideUp(400, function() {
         $('#<%=dom_id(@todo)%>').remove();
         <% if source_view_is :calendar
              # in calendar view it is possible to have a todo twice on the page
         -%>
              $('#<%=dom_id(@todo)%>').remove();
         <% end %>
       });
  <% end -%>
}

function show_new_todo_if_todo_was_recurring() {
  <% if @todo.from_recurring_todo? -%>
    <%  unless @new_recurring_todo.nil? || @new_recurring_todo.deferred? -%>
      TodoItemsContainer.ensureVisibleWithEffectAppear("<%=item_container_id(@new_recurring_todo)%>");
      $('#<%=item_container_id(@new_recurring_todo)%>').append(html_for_new_recurring_todo());
      $('#<%= dom_id(@new_recurring_todo, 'line')%>').effect('highlight', {}, 2000 );
      TracksPages.page_notify('notice', "<%=t('todos.recurring_action_deleted')%>", 5);
    <% else -%>
      <% if @todo.recurring_todo.todos.active.count(:all) == 0 && @new_recurring_todo.nil? -%>
        TracksPages.page_notify('notice', "<%=t('todos.completed_recurrence_completed')%>", 5);
      <% end -%>
    <% end -%>
  <% end -%>
}

function activate_pending_todos() {
 <% # Activate pending todos that are successors of the deleted
    if @saved && @pending_to_activate
      # do not render the js in case of an error or if no todos to activate
      @pending_to_activate.each do |t|
        html = escape_javascript(render(:partial => t, :locals => { :parent_container_type => parent_container_type }))
        # only project and tag view have a deferred/blocked container
        if source_view_is_one_of(:project,:tag) -%>
          $('#<%= dom_id(t) %>').fadeOut(400, function() {
            $('#<%= dom_id(t) %>').remove();
            $('#<%= item_container_id(t) %>').append("<%= html  %>");
            <%= "$('#tickler-empty-nd').show();" if @remaining_deferred_or_pending_count==0 -%>
          });
 <%     else -%>
          $('#<%= item_container_id(t) %>').append("<%= html%>");
 <%     end -%>
        TodoItems.highlight_todo('#<%= dom_id(t, 'line')%>');
 <%   end -%>
 <% end -%>
}

function update_predecessors() {
<%
  if @todo_was_destroyed_from_pending_state
    @uncompleted_predecessors.each do |p| -%>
    if ($('#<%=item_container_id(p)%>')) {
      $('#<%=dom_id(p)%>').html('<%=escape_javascript(render(:partial => p, :locals => { :parent_container_type => parent_container_type }))%>');
    }
<%  end
  end
%>
}

function html_for_new_recurring_todo() {
  return "<%= @saved && @new_recurring_todo ? escape_javascript(render(:partial => @new_recurring_todo, :locals => { :parent_container_type => parent_container_type })) : "" %>";
}

<% end
  # if @saved
-%>
