(function($) {
	window.parseResponse = function () {
	};

	$.prototype.ajaxSubmit = function() {
		if (this[0].tagName.toLowerCase() !== 'form') {
			return false;
		}

		var $form = this;
		var start = arguments[1] || null;
		var callback = arguments[0] || null;

		function getDocument(frame) {
			return frame.contentWindow ? frame.contentWindow.document : (frame.contentDocument ? frame.contentDocument : frame.document);
		}

		function iframeCallback() {
			var iframeDocument = getDocument(this[0]);

			if (iframeDocument && iframeDocument.body.innerHTML.length) {
				try {
					if (jQuery.isFunction(callback)) {
						callback.call(null, iframeDocument.body.innerHTML, $form);
					}
				}
				finally {
					this.unbind('load');
				}
			}
		}

		function uploadFile(action) {
			var iframeName = 'auto_iframe_' + Math.floor(Math.random() * 99999);
			var iframe = jQuery('<iframe style="position: absolute; top: -1000em; left: -1000em" action="' + action + '" src="about:blank" id="' + iframeName + '" name="' + iframeName + '"></iframe>').appendTo(document.body);

			$form.attr('target', iframeName);

			iframe.bind('load', function () {
				iframeCallback.call(iframe);
			});

			$form[0].submit();
			$form.removeAttr('target');
		}

		$form.unbind('submit.ajax').bind('submit.ajax', function (event) {
			if (event.isDefaultPrevented()) {
				return;
			}

			event.preventDefault();

			var action = $form.attr('action') || window.location.pathname;
			var method = $form.attr('method').toLowerCase() || 'get';

			// Validate
			if (jQuery.isFunction(start) && start.call(null, $form) === false) {
				return;
			}

			if ($form[0].enctype == 'multipart/form-data' && method == 'post') {
				uploadFile(action);
			}
			else {
				jQuery[method](action, $form.serialize(), function(content) {
					if (jQuery.isFunction(callback)) {
						callback.call(null, content, $form);
					}
				});
			}
		});

		return this;
	}
})(jQuery);
