(function($){
	$.fn.ajaxForm = function(options) {
		var defaults = { hideForm: true, method: "POST", cache: false, sucess: defaultSucess, incomplete: incompleto,
						complete: defaultComplete, result: null, bindString: 'obrigatorio', bindAttr: 'title', loading: false };
						
		var o = $.extend(defaults, options);
		var t = $(this);
		
		var esperaCss = { width: '100%', height: '100%', backgroundColor: 'white', position: 'absolute', top: 0, left: 0, opacity: 0.8 };
		var esperaImgCss = { position: 'absolute', top: '50%', left: '50%', marginLeft: -110, marginTop: -20 };
		var esperaPCss = { position: 'absolute', top: '50%', marginTop: 0, width: '100%', textAlign: 'center', fontSize: '12px' };
		var esperaAlertCss = { position: 'absolute', top: '45%', marginTop: 0, width: '100%', textAlign: 'center', fontSize: '18px' };
		
		function onSubmit() 
		{
			var multipart 	= ($(this).attr('enctype') == "multipart/form-data") ? true : false;
			var action 		= $(this).attr('action');
			var serialize	= $(this).serialize();
			var preenchido	= true;
			var RCChecked	= [];
			
			$(this).find("input, select, textarea").not("input[type=submit], input[type=reset]").each(function(index, input)
			{
				var bind = $(input).attr(o.bindAttr);				
				if(bind == o.bindString)
				{					
					var type = $(input).attr("type");					
					if(type == "checkbox" || type == "radio")
					{						
						if($.inArray($(input).attr("name"), RCChecked) < 0)
						{							
							var RCPreenchido = false;							
							$("input[name=" + $(this).attr('name') + "]").each(function(ind, inp)
							{
								if($(inp).is(':checked'))
								{
									RCPreenchido = true;
									RCChecked.push($(input).attr("name"));
									return false;
								}
							});
							if(!RCPreenchido) preenchido = false;
						}
					} else if($(input).val() == '') {
						preenchido = false;
					}
				}
			});
			
			if(preenchido)
			{
				if(multipart)
				{
					return true;
				} else {
					request(action, serialize);
					if(o.loading) espera();
					return false;
				}
			} else {
				o.incomplete();
				return false;
			}
		}
		
		function request(url, params)
		{
			$.ajax({
				type: o.method,
				url: url,
				data: params,
				success: o.sucess,
				complete: o.complete
			});	
		}
		
		function defaultComplete(XMLHttpRequest, status)
		{
			var msg = 'Ocorreu algum erro na tentativa de acessar o arquivo: ' + t.attr("action");
			if(status == "error") if(window.Alert) { Alert(msg); } else { alert(msg); }
		}
		
		function defaultSucess(data, status, XMLHttpRequest)
		{
			if(o.result) { $(o.result).html(data); $(o.result).delay(700).fadeIn(500); } 
			else {
				$('div.aj-espera p, div.aj-espera img').fadeOut(500);
				$('div.aj-espera').append('<p class="aj-alert">' + data + '</p>');
				$('div.aj-espera p.aj-alert').css(esperaAlertCss);
			}
			if(o.hideForm && o.result) { $(t).hide(700); }
		}
		
		function incompleto()
		{
			if(window.Alert) { Alert('Preencha os campos obrigatórios por favor.'); } else { alert('Preencha os campos obrigatórios por favor.'); }
		}
		
		function espera()
		{
			$(t).append('<div class="aj-espera"><img src="imgs/loading.gif" alt="Carregando" /><p>Enviando Mensagem</p></div>');
			$(t).css('position', 'relative');
			$('div.aj-espera').css(esperaCss);
			$('div.aj-espera img').css(esperaImgCss);
			$('div.aj-espera p').css(esperaPCss);
			$('div.aj-espera a.formBack').live('click', function(){ $('div.aj-espera').fadeOut(500, function(){ $(this).remove(); }); });
		}
		
		return $(this).each(function() { t = $(this); $(this).submit(onSubmit); } );		
	}
})(jQuery);
