function taxonomyUserSave(path, span, node) {
  // special effects!
  $(span).fadeTo(250, 0.5);
  // send it to drupal for processing
  $.post(path, { op: "submit", typed_string: $('.taxonomy_user_tag_input').val(), nid: node[1] }, function(data){
    // replace old thinger with new data
//    alert("Added Tags: "+data);
    $(span).slideUp('slow');
      $("#taxonomy_"+node[1]).fadeOut('fast', function() {
        $("#taxonomy_"+node[1]).empty();
        $("#taxonomy_"+node[1]).append(data);
        $("#taxonomy_"+node[1]).fadeIn('fast');
        taxonomyUserReady();
    });
  });
}

function taxonomyUserReady() {
  $("a.taxonomy_user_addlink").click(function() {
    var span = $(this).parent();
    var path = $(this).attr("href");
    var node = $(span).attr("id").split("_");
    $.post(path, { op: "ajax", nid: node[1] }, function(data){
      
      $(span).fadeOut('fast', function() { 
        // clear out the old link
        $(span).empty();
        // replace it with the form we got from drupal
        $(span).append(data);
        $(span).fadeIn('fast');
        // At this point, the new form element is up
        // So we listen for the tag-it link        
        $("a.taxonomy_user_tag").click(function() { taxonomyUserSave(path, span, node); return false;  });
        // we also listen for the old hit of the return key
        $("form#taxonomy-user-ajax-form").submit(function() { taxonomyUserSave(path, span, node); return false;  });
      }); 
    });
    // close original click out
    return false;
  });
}

$(document).ready(function() {
  taxonomyUserReady();
});