$(function() {
	/*
	 * Method used to add new child form partials
	 */
	$('form a.add_child').click(function() {
	    var assoc   = $(this).attr('data-association');
	    var content = $('#' + assoc + '_fields_template').html();
	    var regexp  = new RegExp('new_' + assoc, 'g');
	    var new_id  = new Date().getTime();
        
			var newElements = jQuery(content.replace(regexp, new_id)).hide();
	    $(this).parent().before(newElements).prev().slideFadeToggle('slow');

	    return false;
	});
  
	/*
	 * Method used to remove child form partials
	 */
	$('form a.remove_child').live('click', function() {
		if(confirm('Are you sure?')) {
			var hidden_field = $(this).prev('input[type=hidden]')[0];
			if(hidden_field) {
				hidden_field.value = '1';
			}
	
			$(this).parents('.fields').slideFadeToggle();
		}
    
		return false;
	});
	
	// All non-GET requests will add the authenticity token if not already present in the data packet
	$(document).ajaxSend(function(event, request, settings) {
		if (typeof(window.AUTH_TOKEN) == "undefined") return;
		// <acronym title="Internet Explorer 6">IE6</acronym> fix for http://dev.jquery.com/ticket/3155
		if (settings.type == 'GET' || settings.type == 'get') return;
		settings.data = settings.data || "";
		settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(window.AUTH_TOKEN);
	});

	ajaxLinks();
});

jQuery.ajaxSetup({ 'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript,application/javascript,text/html")} })

function _ajax_request(url, data, callback, type, method) {
	if (jQuery.isFunction(data)) {
		callback = data;
		data = {};
	}
	return jQuery.ajax({
		type: method,
		url: url,
		data: data,
		success: callback,
		dataType: type
	});
}

jQuery.extend({
	put: function(url, data, callback, type) {
		return _ajax_request(url, data, callback, type, 'PUT');
	},
	delete_: function(url, data, callback, type) {
		return _ajax_request(url, data, callback, type, 'DELETE');
	}
});

jQuery.fn.submitWithAjax = function() {
	this.unbind('submit', false);
	this.submit(function() {
		$.post(this.action, $(this).serialize(), null, "script");
		return false;
	})
	return this;
};

// Send data via get if <acronym title="JavaScript">JS</acronym> enabled
jQuery.fn.getWithAjax = function() {
	this.unbind('click', false);
	this.live("click", function() {
		$.get($(this).attr("href"), $(this).serialize(), null, "script");
		return false;
	})
	return this;
};

// Send data via Post if <acronym title="JavaScript">JS</acronym> enabled
jQuery.fn.postWithAjax = function() {
	this.unbind('click', false);
	this.live("click", function() {
		$.post($(this).attr("href"), $(this).serialize(), null, "script");
		return false;
	})
	return this;
};

jQuery.fn.putWithAjax = function() {
	this.unbind('click', false);
	this.live("click", function() {
		$.put($(this).attr("href"), $(this).serialize(), null, "script");
		return false;
	})
	return this;
};

jQuery.fn.deleteWithAjax = function() {
	this.removeAttr('onclick');
	this.unbind('click', false);
	this.live("click", function() {
		$.delete_($(this).attr("href"), $(this).serialize(), null, "script");
		return false;
	})
	return this;
};

// This will "ajaxify" the links
function ajaxLinks(){
	$('.ajaxForm').submitWithAjax();
	$('a.get').getWithAjax();
	$('a.post').postWithAjax();
	$('a.put').putWithAjax();
	$('a.delete').deleteWithAjax();
}

