$.ajaxSetup({
  'beforeSend': function (xhr) {
    xhr.setRequestHeader("Accept", "text/javascript")
  }
})
$(document).ready(function () {
  /*$.idleTimer(120000);
  $(document).bind("idle.idleTimer", function () {
    location.reload();
  });*/
  $.fn.wait = function (time, type) {
    time = time || 1000;
    type = type || "fx";
    return this.queue(type, function () {
      var self = this;
      setTimeout(function () {
        $(self).dequeue();
      }, time);
    });
  };
  jQuery.fn.exists = function () {
    return jQuery(this).length > 0;
  }
  jQuery.fn.reverse = [].reverse;
  jQuery.fn.fadeToggle = function (speed, easing, callback) {
    return this.animate({
      opacity: 'toggle'
    }, speed, easing, callback);
  };
  $(".selected_date").hide();
  $(".duedate_helper").live("change", function () {
    duedate_helper = $(this);
    duedate_helper.next().next().hide();
    if ($(duedate_helper).val() == "") {
      $(duedate_helper).parent().find("#task_due_at_1i").val("");
      $(duedate_helper).parent().find("#task_due_at_2i").val("");
      $(duedate_helper).parent().find("#task_due_at_3i").val("");
    } else {
      if (duedate_helper.val() == "other") {
        date = $(".todaydate", duedate_helper).val().split(".");
        duedate_helper.next().next().show();
      } else {
        date = duedate_helper.val().split(".");
      }
      year = date[2];
      month = date[1];
      day = date[0];
      year = trimNumber(year);
      month = trimNumber(month);
      day = trimNumber(day);
      duedate_helper.parent().find("#task_due_at_1i").val(year);
      duedate_helper.parent().find("#task_due_at_2i").val(month);
      duedate_helper.parent().find("#task_due_at_3i").val(day);
    }
  });
  $(".details_for_new_task").hide();
  $(".add_details").click(function () {
    $(this).parent().next().show();
    $(this).parent().hide();
    $("textarea", $(this).parent().next()).focus();
  });
  $(".all_inactive_projects").click(function () {
    $("#inactive_projects li").show();
    $(".all_inactive_projects").hide();
  });
  $(".less_inactive_projects").click(function () {
    $(".hideme").hide();
    $(".all_inactive_projects").show();
  });
  if ($(".notification").exists()) {
    $(".notification").fadeIn()
    $(".notification").wait(3000).fadeOut()
  }
  $(".a_task").each(function () {
    if ($(this).hasClass("done_task")) {
      $(this).find(".check_form input").attr("checked", "checked");
    } else {
      $(this).find(".check_form input").removeAttr("checked");
    }
  });
  $(".check_form").live("submit", function () {
    form = $(this)
    if (!form.parent().parent().hasClass("not_in_project")) {
      $.post($(this).attr("action"), $(this).serialize(), function (data) {
        form.parent().parent().fadeOut();
        if ($(data).hasClass("done_task")) {
          $(data).insertAfter($("li div form", form.parent().parent().parent().parent()).parent().parent()).hide().fadeIn();
          $("li[rel=" + form.parent().parent().attr("id") + "]").fadeOut().remove();
        } else {
          $(data).appendTo($(".things_to_do", form.parent().parent().parent().parent())).hide().fadeIn();
        }
      });
    } else {
      form.parent().parent().fadeOut();
      $("#" + form.parent().parent().attr("rel")).find(".check_form").trigger("submit");
    }
    return false;
  })
  $("#projects li div form").submit(function () {
    form = $(this);
    project_id = $(this).attr("rel");
    $.post($(this).attr("action"), $(this).serialize(), function (data) {
      $(data).appendTo($(".things_to_do", form.parent().parent().parent().parent())).hide().fadeIn();
    });
    current_li = $("#projects li div:visible").parent();
    abort_new_task(current_li);
    $(form.parent().parent().find(".add_task")).trigger("click");
    return false;
  });
  // neue aufgabe dialog einblenden
  $(".add_task").click(function () {
    $(this).hide();
    $("div", $(this).parent()).show();
    $("div input[type=text]:first", $(this).parent()).focus();
    return false;
  });
  $(document).keyup(function (e) {
    if (e.keyCode == 27) {
      abort_new_task();
      $(".delete_question").fadeOut("fast");
      $(".edit_task_dialog .abort_edit_task").trigger("click");
    }
  });
  // dialog für neue aufgaben erst einmal verstecken
  $("#projects li li div").hide();
/*$("#projects li li").reverse().each(function () {
    li = $(this);
    if ( li.siblings().length == 0 ) {
      $(".add_task", li).trigger("click");
    }
  });*/
  // beim abbrechen von „Aufgabe hinzufügen“ dialog schließen
  $(".abort_add_task").click(function () {
    current_li = $("#projects li div:visible").parent();
    abort_new_task(current_li);
    return false;
  });
  // dialog für neue projekte erst einmal verstecken
  $("#active_projects div").hide();
  $("#active_projects .add_project").click(function () {
    $("#active_projects div").show();
    $("#active_projects div input[type=text]").focus();
    $(".add_project").hide();
  });
  // beim abbrechen von „Projekt hinzufügen“ dialog schließen
  $("#active_projects .abort_add_project").click(function () {
    $("#active_projects div").hide();
    $("#active_projects input[type=text]").val("");
    $(".add_project").show();
  });
  // beim klicken auf ein aktives projekt in der sidebar zum projekt langsam herunterscrollen
  $("#active_projects .project_name").click(function () {
    id = $(this).attr("href");
    $('html, body').animate({
      scrollTop: $(id).offset().top
    }, 1000);
  });
  // beim abhaken einer aufgabe form abschicken
  $(".celltask form").live("click", function () {
    $(this).submit();
  });
  $(".edit").live("click", function () {
    link = $(this)
    $.get($(this).attr("href"), function (data) {
      link.parent().parent().after(data);
      link.parent().parent().hide();
      link.parent().parent().next().find("input[type=text]:first").focus();
      link.parent().parent().next().find(".selected_date").hide();
    });
    return false;
  });
  $(".edit_task_dialog .abort_edit_task").live("click", function () {
    if ($(this).parent().parent().parent().hasClass("edit_task_dialog")){
      $(this).parent().parent().parent().hide();
    }
    if ($(this).parent().parent().parent().prev().hasClass("a_task")) {
      $(this).parent().parent().parent().prev().show();
    }
  });
  $(".edit_task_dialog form").live("submit", function () {
    form = $(this)
    // form.parent().hide();
    $.post($(this).attr("action"), form.serialize(), function (data) {
      form.parent().parent().replaceWith(data);
    });
    return false;
  });
  $(".project_from_list form").hide();
  $("a.edit_project").click(function () {
    $("form", $(this).parent()).show();
    $("form input[type=text]", $(this).parent()).focus();
    $(".project_name", $(this).parent()).hide();
    $(this).hide();
  });
  $("a.abort_edit_project").click(function () {
    $(this).parent().parent().hide();
    $(this).parent().parent().prev().show();
    $(this).parent().parent().prev().prev().show();
  });
  $(".task_actions form").live("submit", function () {
    form = $(this)
    $.post($(this).attr("action"), form.serialize(), function (data) {
      form.parent().parent().parent().fadeOut();
    });
    return false;
  });
  $(".finish_order").hide();
  $(".order_this").click(function () {
    project = $(this).parent().parent().parent();
    project.addClass("is_in_order_mode");
    $(".finish_order", project).show();
    $(".order_this", project).hide();
    $(".things_to_do", project).sortable({
      handle: '.handle',
      axis: 'y',
      opacity: 0.6,
      cursor: 'move',
      update: function (event, ui) {
        sort = $(this).sortable('serialize');
        $.post("/sort", sort);
      }
    });
  });
  $(".finish_order").click(function () {
    project = $(this).parent().parent().parent();
    $(".order_this", project).show();
    $(".finish_order", project).hide();
    project.removeClass("is_in_order_mode");
  });
  $("#project_due_today .project_name a").click(function () {
    $.get($(this).attr("href"));
    $("#project_due_today #labelproject, #project_due_today .project_name a span, #project_due_today .things_to_do").toggle();
    $(".project_name").toggleClass("minimized");
    return false;
  });
  $(".delettask").live("click", function () {
    $(this).parent().find(".delete_question").fadeIn("fast");
  });
  $(".delete_question .answer_no").live("click", function () {
    $(this).parent().fadeOut("fast");
  });
  $(".companies_menu ul").hide();
  $(".companies_menu").mouseover(function () {
    $(".companies_menu ul").show();
  });
  $(".companies_menu").mouseout(function () {
    $(".companies_menu ul").hide();
  });
});
function trimNumber(s) {
  while (s.substr(0, 1) == '0' && s.length > 1) {
    s = s.substr(1, 9999);
  }
  return s;
}
function abort_new_task(current_li) {
  $(".add_task_div", $("li", current_li)).hide();
  $(".add_task_div input[type=text]", current_li).val("");
  $(".add_task_div textarea", current_li).val("");
  $(".duedate_helper", current_li).val("");
  $(".selected_date select", current_li).val("");
  $(".selected_date", current_li).hide();
  $("select[name=task[user_id]]", current_li).val($(".me_employee", current_li).val());
  $(".add_task", current_li).show();
  $(".details_for_new_task", current_li).hide();
  $(".add_details", current_li).parent().show();
}
