$(document).ready(function(){

  $("#new_related_practices").livequery(function() {
    $(this).tokenInput("/practices/find_related", {
      hintText: "Type in the names of practices",
      noResultsText: "No results",
      searchingText: "Searching...",
      initialData: $('#initial_data').val()
    });
  });

  $('.toggle_sibling input[type=checkbox]').click(function(){
    $(this).parent().siblings('.hidden').toggle();
  });

  $('.facebox').facebox({
    loadingImage: '/images/facebox/loading.gif',
    closeImage: '/images/facebox/loading.gif',
    opacity: 0.2
  });

  $('button.delete').click(function() {
    return confirm("Are you sure? There is no going back.");
  });

  $("#flash p").livequery(function() {
    var p = $(this);
    setTimeout(function() {
      p.slideUp('slow', function() { $(this).remove() });
    }, 4000 + 75*p.text().length);
  });

  $('form.button-to:has(button.remove)').submit(function(e) {
    e.preventDefault();
    var form = $(this);
    $.post(form.attr('action'), form.serialize(), function() {
      form.parents('li:first').slideUp('slow', function() { $(this).remove() });
    });
  });

  $('.inline-edit').click(function(e) {
    e.preventDefault();
    var target = $($(this).attr('id').replace(/^edit-/, '#'));
    $.get($(this).attr('href'), function(data) {
      target.html(data);
      target.find('form').submit(function(e) {
        e.preventDefault();
        $.post($(this).attr('action'), $(this).serialize(), function() {
          target.text(target.find('textarea').val());
        });
      });
    });
  });

  hide_endorsement_comments();
  setup_toggle_for_endorsement_comments();

  $("input[id^=endorsement_practice_id_]").click(function() {
    var element = $('#'+$(this).attr('id').replace(/_practice_id_/,'_comment_'));
    var comment = element.find('textarea');
    if($(this).attr('checked')) {
      element.show();
      comment.enable();
    } else {
      comment.disable();
      element.hide();
    }
  });

  $("input[id^=endorsement_practice_id_]").each(function() {
    var element = $('#'+$(this).attr('id').replace(/_practice_id_/,'_comment_'));
    var comment = element.find('textarea');
    if($(this).attr('checked')) {
      element.show();
      comment.enable();
    } else {
      comment.disable();
      element.hide();
    }
  });
});

$.fn.enable = function() {
  $(this).removeAttr('disabled').removeClass('disabled');
}

$.fn.disable = function() {
  $(this).attr('disabled', 'disabled').addClass('disabled');
}

function hide_endorsement_comments() {
  $('.toggle').each(function(){
    var target = $(this).attr('href');
    $(target).hide();
  });
}

function setup_toggle_for_endorsement_comments() {
  $('.toggle').click(function(){
    var target = $(this).attr('href');
    $(target).toggle().css('zoom', '1');
    $(this).toggleClass('active');
    return false;
  });
}

$(document).ajaxSuccess(function(options, r) {
  var notice;
  if(redirect = r.getResponseHeader('X-Redirect-To')){
    window.location = redirect;
  }
  $.each(["Success", "Notice", "Error"], function() {
    if(notice = r.getResponseHeader("X-Flash-" + this)) {
      $("#flash").append($("<p/>").addClass(this.toLowerCase()).html(notice));
    }
  });
});

jQuery(document).ajaxSend(function(event, request, settings) {
  if(typeof(AUTH_TOKEN) == "undefined") return;
  if(settings.type == "GET") return;
  if(settings.contentType != "application/x-www-form-urlencoded") return;
  if(settings.data)
    settings.data += "&";
  else {
    request.setRequestHeader("Content-Type",settings.contentType);
    settings.data = "";
  }
  settings.data += "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
});

