(function($) {
  
  $(function() {
    
    $("#suretrack_link a").click(function() {
      $(this).hide('slow');
      $("#suretrack_integrator").show('slow');
      return false;
    });
    
    $.fn.quickComplete = function(){
      $(this).click(function(){
        var link = $(this);
        var id = link.metadata().id;
        $.post('quick_complete', {id: id}, function(r){
          link.parent().html("Pending 100% Requests");
        });
        
        return false;
      });
    }
    
    $('.quick_complete').quickComplete();
    
    $.fn.openTodoBox = function(){
        var this_id = $(this).metadata().id;
        $.ajax({url: "create_update_edit_todo", data: {todo_id: this_id}, success: function(text) {
          $("#todo_box").html(text);
          
          $("#todo_box").find("a.demo_link").applyDatePicker().end()
            .find("button[type=reset]").click(function() { refresh_todo(); return false; }).end();
    
          bindTodoBox($("#todo_box"));
    
          $("#todo_box").show();
          $("select#subproject, select#person, select#resource").css('margin-left', '-1000px');
        } });
        return false;
    }
    
    // Open todo items in Day/All views.
    $("table.tasks_table .todo a").click(function(e){
      return $(this).parents(".todo").openTodoBox();
    });
    
    // Open todo items in Month/Week views.
    $("table.calendar").click(function(e) {
      var self = $(e.target);
      if(self.is("a[rel=edit_task]")) {
        return self.parents("td.todo").openTodoBox(); 
      } else if(self.is("button.delete")) {
        var this_id = self.parents("td").metadata().id;
        var tds = $("td.todo").filter(function() { return ($(this).metadata().id == this_id) });
        var between = tds.parents("tr").next("tr.between");
        $.ajax({url: "delete_todo", data: {todo_id: this_id}, success: function() {
            tds.parent().fadeOut("normal");
            between.fadeOut("normal");
            if($("#todo_box form").metadata().todo_id == this_id)
              refresh_todo();
          }
        });
        return false;  
      }
    });
    
    var refresh_todo = function() {
      $.ajax({url: "get_new_todo", data: { type: "new" }, success: 
        function(text) {
          $("#todo_box").html(text);
          $("#todo_box").find("a.demo_link").applyDatePicker().end()
            .find("button[type=reset]").click(function() { refresh_todo(); return false; }).end();
          bindTodoBox($("#todo_box"));
        }
      });
    }
    
    $("#todo_box button[type=reset]").click(function() { refresh_todo(); return false; });
    
  });
  
})(jQuery);

/**
 * Todo items
 */
function bindTodoBox(e) {
  e.hide();
  jQuery("select#subproject, select#person, select#resource").css('margin-left', '0px');

  e.find("button[type=reset]").click(function() {
    e.hide();
    jQuery("select#subproject, select#person, select#resource").css('margin-left', '0px');
  });
  
  // Populate the 'Assign To' select if we've chosen a Project
  e.find("select[name='todo[project_id]']").change(function() {
    var todo_id = e.find("form").metadata().todo_id;
    var project_id = jQuery(this).val();
    jQuery.ajax({url: "get_login_informations", data: {todo_id: todo_id, project_id: project_id}, success:
      function(text) {
        e.find("select[name='todo[assigned_id]']").html(text);
      }
    });
  });
};

jQuery(function($) {
  bindTodoBox($("#todo_box"));

  $("#todo_link").click(function() {
      $("#todo_box").show();
      $("select#subproject, select#person, select#resource").css('margin-left', '-1000px');

      return false;
    });

  if( $("#errorExplanation").length > 0 ) {
      $("#todo_box").show();
      $("select#subproject, select#person, select#resource").css('margin-left', '-1000px');
    }
});