// public/javascripts/application.js
$(function(){

  jQuery.ajaxSetup({ 
    'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
  })

  jQuery.fn.submitWithAjax = function() {
    this.submit(function() {
      $.post(this.action, $(this).serialize(), null, "script");
      return false;
    })
    return this;
  };
  
  function remove_source(e){
    $(e).parent('p').remove();
    return false;
  }
  
 
  $('.add_source').click(function() {

    cnt = $(this).parents('fieldset').children("p").size();
    $(this).parent('p').before(
      $('<p>')
      .append('<label for="article_sources_attributes_' + cnt + '_name">Name</label><br/>')
      .append('<input id="article_sources_attributes_' + cnt + '_name" name="article[sources_attributes][' + cnt + '][name]" size="30" type="text"><br/>')
      
      .append('<label for="article_sources_attributes_' + cnt + '_url">Url</label><br/>')
      .append('<input id="article_sources_attributes_' + cnt + '_url" name="article[sources_attributes][' + cnt + '][url]" size="30" type="text"><br/>')
      .append($(' <a href="#" class="remove_source">Entfernen</a>').click(
        function() { return remove_source(this); }
      ))
    );
    return false;
  });

});