<%
  @todo = nil
  @initial_context_name = @context.name unless @context.nil?
  @initial_context_name ||= @project.default_context.name unless @project.nil? || @project.default_context.nil? 
  @initial_context_name ||= @contexts[0].name unless @contexts[0].nil?
  @initial_project_name = @project.name unless @project.nil?
%>
<div id="todo_new_action_container">

<div id="show_todo_new_action" style="display:none">
  <%= link_to_function("Add a next action &#187;",
                       "todoItems.showNewActionForm();return false;",
                       {:title => "Add a next action"}) %>
</div>
<div id="hide_todo_new_action" style="display:block">
  <%= link_to_function("&laquo; Hide form",
                       "todoItems.hideNewActionForm();return false;",
                       {:title => "Hide next action form", :accesskey => "n"}) %>
                     </div>

<div id="todo_new_action" style="display:block">

<% form_remote_tag(
    :url => todos_path, :method => :post,
    :html=> { :id=>'todo-form-new-action', :name=>'todo', :class => 'inline-form' },
    :before => "$('todo_new_action_submit').startWaiting()",
    :complete => "$('todo_new_action_submit').stopWaiting()",
    :condition => "!$('todo_new_action_submit').isWaiting()") do -%>

<div id="status"><%= error_messages_for("item", :object_name => 'action') %></div>

<label for="todo_description">Description</label>
<%= text_field( "todo", "description", "size" => 30, "tabindex" => 1) %>

<label for="todo_notes">Notes</label>
<%= text_area( "todo", "notes", "cols" => 29, "rows" => 6, "tabindex" => 2) %>

<label for="todo_project_name">Project</label>
<input id="todo_project_name" name="project_name" autocomplete="off" tabindex="3" size="30" type="text" value="<%= @initial_project_name %>" />
<div class="page_name_auto_complete" id="project_list" style="display:none"></div>

<script type="text/javascript">
projectAutoCompleter = new Autocompleter.Local('todo_project_name', 'project_list', <%= project_names_for_autocomplete %>, {choices:100,autoSelect:false});
function selectDefaultContext() {
  todoContextNameElement = $('todo_context_name');
  defaultContextName = todoContextNameElement.projectDefaultContextsMap[this.value];
  if (defaultContextName && !todoContextNameElement.editedByTracksUser) {
    todoContextNameElement.value = defaultContextName;
  }
}
Event.observe($('todo_project_name'), "focus", projectAutoCompleter.activate.bind(projectAutoCompleter));
Event.observe($('todo_project_name'), "click", projectAutoCompleter.activate.bind(projectAutoCompleter));
Event.observe($('todo_project_name'), "blur", selectDefaultContext.bind($('todo_project_name')));
</script>

<label for="todo_context_name">Context</label>
<input id="todo_context_name" name="context_name" autocomplete="off" tabindex="4" size="30" type="text" value="<%= @initial_context_name %>" />
<div class="page_name_auto_complete" id="context_list" style="display:none"></div>

<script type="text/javascript">
var contextAutoCompleter;
function initializeNamesForAutoComplete(contextNamesForAutoComplete) {
	if (contextNamesForAutoComplete.length == 0 || contextNamesForAutoComplete[0].length == 0) {
		return;
	}
	contextAutoCompleter = new Autocompleter.Local('todo_context_name', 'context_list', contextNamesForAutoComplete, {choices:100,autoSelect:false});
	Event.observe($('todo_context_name'), "focus", function(){ $('todo_context_name').editedByTracksUser = true; });
	Event.observe($('todo_context_name'), "focus", contextAutoCompleter.activate.bind(contextAutoCompleter));
	Event.observe($('todo_context_name'), "click", contextAutoCompleter.activate.bind(contextAutoCompleter));	
}
function updateContextNamesForAutoComplete(contextNamesForAutoComplete) {
	if (contextAutoCompleter) // i.e. if we're already initialized
	{
		contextAutoCompleter.options.array = contextNamesForAutoComplete
	}
	else
	{
		initializeNamesForAutoComplete(contextNamesForAutoComplete)		
	}
}
initializeNamesForAutoComplete(<%= context_names_for_autocomplete %>);
$('todo_context_name').projectDefaultContextsMap = eval('(' + <%= @default_project_context_name_map %> + ')');
</script>

<label for="tag_list">Tags (separate with commas)</label>
<%= text_field_tag "tag_list", nil, :size => 30, :tabindex => 5 %> 

<div class="due_input">
<label for="todo_due">Due</label>
<%= text_field("todo", "due", "size" => 12, "class" => "Date", "onfocus" => "Calendar.setup", "tabindex" => 6, "autocomplete" => "off") %>
</div>

<div class="show_from_input">
<label for="todo_show_from">Show from</label>
  <%= text_field("todo", "show_from", "size" => 12, "class" => "Date", "onfocus" => "Calendar.setup", "tabindex" => 7, "autocomplete" => "off") %>
</div>

<%= source_view_tag( @source_view ) %> 

<div class="submit_box">
  <div class="widgets">
    <button type="submit" class="positive" id="todo_new_action_submit">
        <%=image_tag("accept.png", :alt => "") %>
        Add action
    </button>
  </div>
</div>
<% end -%>

                  
<%= calendar_setup( "todo_due"  ) %>
<%= calendar_setup( "todo_show_from"  ) %>
</div>
</div>