$(document).ready( function() {
	$('body').addClass('hasJS');
	
	$('h2 a').fancybox({
		'autoDimensions': false,
		'width': 600,
		'height': 'auto'
	});
	
	var submitForm = function() {
		$.fancybox.showActivity();
		// send post data via AJAX
		$.ajax({
			type: 'POST',
			cache: false,
			url: 'index.php',
			data: $(this).serializeArray(),
			success: function(data) {
				// cut the form html out of the server reply …
				var formHtml = $(data).find('#formular');
				if(formHtml.length > 0) {
					formHtml = formHtml[0];
					// … and show it within the fancybox
					$.fancybox(formHtml, {
						'autoDimensions': false,
						'width': 600,
						'height': 'auto',
						'onComplete': function() { $('form').submit(submitForm); }
					});
				}
			}
		});
		return false;
	}
	
	$('form').submit(submitForm);
});
