//TODO: Nefunguje v Google Chrome :-(

(function($) {

	$.fn.makeAsynchronous=function()
	{
		var counter=0;

		$.each(this, function()
		{
			var form=$(this);

			var formFields=$(this).find('input, select, textarea');
			
			form.bind('lock', function() {
				formFields.attr('disabled', 'disabled');
			});

			form.bind('unlock', function() {
				formFields.removeAttr('disabled');
			});

			form.bind('submit', function()
			{
				var iframe;
				var iframeId='asyncForm'+(counter++)+Math.round(Math.random()*99999);				
				
				if(window.ActiveXObject)
					iframe=document.createElement('<iframe id="' + iframeId + '" name="' + iframeId + '" />');
				else
				{
					iframe=document.createElement('iframe');
					iframe.id=iframeId;
					iframe.name=iframeId;
				}

				iframe.style.position='absolute';
				iframe.style.top='-100px';
				iframe.style.left='-100px';
				iframe.style.width='0px';
				iframe.style.height='0px';
				iframe.style.border='none';

				document.body.appendChild(iframe);
				form.attr('target', iframeId);

				$(iframe).bind('load', function()
				{				
					var iframe=document.getElementById(iframeId);
					var response=iframe.contentWindow.document.body?iframe.contentWindow.document.body.innerHTML:null;

					form.trigger('responseReceived', response);
					form.trigger('unlock');

					setTimeout(
						function() {
							$(iframe).remove();
						},
						0
					);

				});

				form.get(0).submit();
				form.trigger('lock');

				return false;
			});
		});
		
		return this;
	};


}) (jQuery);

$(function() {
	$('form.asynchronous').makeAsynchronous();
});
