/**
 * @author Eli Sand
 * @copyright Copyright © 2008-2009 Eli Sand
 * @link http://www.elisand.com/
 *
 * Licensed under the Non-Profit Software License, Version 1 (the "License")
 * as published by Eli Sand.  You may not use this file except in compliance
 * with the License.
 *
 * You may obtain a copy of the License at:
 *
 *    http://www.elisand.com/licenses/LICENSE-NSL-1.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

jQuery('form [type="submit"]').live('click', function(e) {
	jQuery(e.target.form).data('submitButton', this);
});

jQuery.fn.AJAXForm = function(options, callback) {
	if (jQuery.isFunction(options)) {
		callback = options;
		options = {};
	}

	return this.each(function() {
		var fields = [];
		var button = jQuery(this).data('submitButton');

		jQuery(':input', this).each(function() {
			if (this.name && !this.disabled && (this.type != 'submit') && (this.checked || ((this.type != 'radio') && (this.type != 'checkbox')))) {
				fields.push(this.name + '=' + escape(this.value));
			}
		});

		if (button.name) {
			fields.push(button.name + '=' + escape(button.value));
		}

		jQuery.ajax(jQuery.extend({
			url: this.getAttribute('action') || this.ownerDocument.URL,
			type: this.getAttribute('method'),
			data: fields.join('&'),
			complete: callback,
			beforeSend: function(xhr) {
				xhr.setRequestHeader('X-Ajax-Request', 'true');
			}
		}, options));
	});
};

jQuery.getJSON = function(url, data, callback) {
	if (jQuery.isFunction(data)) {
		callback = data;
		data = null;
	}

	return jQuery.ajax({
		url: url,
		type: 'GET',
		data: data,
		dataType: 'json',
		complete: function(xhr, status) {
			var json = eval("(" + xhr.responseText + ")");
			callback(json, status);
		},
		beforeSend: function(xhr) {
			xhr.setRequestHeader('X-Ajax-Request', 'true');
		}
	});
};

/*
jQuery.fn.AJAXSubmit = function() {
	return jQuery(this).submit(function(e) {
		return false;
	});
};
*/
