/*!
 * Plugin do jQuery para envio de e-mails com AJAX
 * 
 * @author Emanuel Melo @ PC4Team
 * Data: quarta-feira 11 de maio de 2011 15:10:29
 */
(function($) {
	$.fn.ajaxLogin = function(opcoes) {
		var config = {
			url: BASE_URL + 'login/logar',
			carregando: 'Identificando...',
			msgbox: this.find('.notification'),
			callback: function(retorno) {
				window.location.href = BASE_URL + retorno.sessao;
			},
			predispatch: null
		};
		$.extend(config, opcoes);

		// recupera/cria a caixa de mensagens
		if (config.msgbox.length == 0) {
			config.msgbox = $('<div/>');
			this.prepend(config.msgbox);
		}
		config.msgbox.hide();
		
		this.submit(function() {
			var form = $(this);
			config.msgbox.show();
			config.msgbox.removeClass();
			config.msgbox.addClass('notification');
			config.msgbox.html(config.carregando);

			if (config.predispatch != null) {
				if (!config.predispatch(this)) {
					return false;
				}
			}
			
			$.post(config.url, (form.serialize()), function (retorno) {
				if (retorno.resultado == 0) {
					config.msgbox.addClass('error');
					config.msgbox.html(retorno.erros);
					form[0].reset();
				}
				else {
					config.callback(retorno);
				}
			}, 'json');
			return false;
		});
		
		return this;
	};
})(jQuery);
