/**
 * JAlerta - v0.5
 * 
 * Desenvolvido por: Alexandre Almeida Ferreira
 * E-Mail: satannish@hotmail.com
 * 
 */
var JAlerta = new Object();
JAlerta.opacidade = 0.8;
JAlerta.opaco = 1.0;
JAlerta.velocidade = 'fast'; //fast, slow ou milisegundos
JAlerta.label_fechar = "Fechar";
JAlerta.label_sim = "Sim";
JAlerta.label_nao = "N&atilde;o";
JAlerta.label_enviar = 'Enviar';
JAlerta.label_limpar = 'Limpar';
JAlerta.label_cancelar = 'Cancelar';
JAlerta.selecao = '';

JAlerta.centraliza = function (id, opcoes){
	if((opcoes)&&(opcoes.width)){
		$('#'+id).css('width', opcoes.width);
	}
	if((opcoes)&&(opcoes.height)){
		$('#'+id).css('height', opcoes.height);
	}
	bWidth = $('body').outerWidth();
	bHeight = $('body').outerHeight();
	jWidth = $('#'+id).outerWidth();
	jHeight = $('#'+id).outerHeight();
	cWidth = ((bWidth/2)-(jWidth/2));
	cHeight = ((bHeight/2)-(jHeight/2));
	$('#'+id).css('left', cWidth);
	$('#'+id).animate({
		opacity: JAlerta.opaco,
	    top: '+='+cHeight
	},1000, function(){
	});
};
JAlerta.init = function (texto, titulo, opcoes, funcao){
	if(JAlerta.selecao!=''){
		JAlerta.selecao = '';
	}
	$('body').append('<div id="jalerta-fundo"></div>');
	$('#jalerta-fundo').fadeTo(JAlerta.velocidade, JAlerta.opacidade, function() {
		$('body').append('<div id="jalerta-corpo"></div>');
		$('#jalerta-corpo').append('<div id="jalerta-titulo"></div>');
		$('#jalerta-corpo').append('<div id="jalerta-conteudo"></div>');
		if(opcoes){
			switch(opcoes.tipo){
				default:
					$('#jalerta-conteudo').append('<div id="jalerta-image-alerta"></div>');
				break;
				case 'alerta':
					$('#jalerta-conteudo').append('<div id="jalerta-image-alerta"></div>');
				break;
				case 'confirma':
					$('#jalerta-conteudo').append('<div id="jalerta-image-confirma"></div>');
				break;
				case 'form':
					$('#jalerta-conteudo').append('<form id="jalerta-form"></form>');
					if(opcoes.action){
						$('#jalerta-form').attr('action',opcoes.action);
					}
					if(opcoes.method){
						$('#jalerta-form').attr('method',opcoes.method);
					}
				break;
				case 'none':
				break;
			}
		}else{
			$('#jalerta-conteudo').append('<div id="jalerta-image-alerta"></div>');
		}
		if(titulo){
			$('#jalerta-titulo').append(titulo);
		}
		if((opcoes)&&(opcoes.tipo=='form')){
			if(texto){
				$('#jalerta-form').append(texto);
				$('#jalerta-form').append('<div id="jalerta-botoes"></div>');
			}
		}else{
			if(texto){
				$('#jalerta-conteudo').append("<p id='jalerta-texto'>"+texto+"</p>");
				if((opcoes)&&(opcoes.align)){
					$('#jalerta-conteudo').css('text-align', opcoes.align);
				}else{
					$('#jalerta-conteudo').css('text-align', 'center');
				}
			}
			$('#jalerta-conteudo').append('<div id="jalerta-botoes"></div>');
		}
		
		if(opcoes){
			switch(opcoes.tipo){
				default:
					$('#jalerta-botoes').append('<input type="button" value="'+JAlerta.label_fechar+'" id="jalerta-btn-fechar">');
				break;
				case 'confirma':
					$('#jalerta-botoes').append('<input type="button" value="'+JAlerta.label_sim+'" id="jalerta-btn-sim">');
					$('#jalerta-botoes').append('<input type="button" value="'+JAlerta.label_nao+'" id="jalerta-btn-nao">');
				break;
				case 'alerta':
					$('#jalerta-botoes').append('<input type="button" value="'+JAlerta.label_fechar+'" id="jalerta-btn-fechar">');
				break;
				case 'form':
					if(opcoes.ajax){
						$('#jalerta-botoes').append('<input type="button" value="'+JAlerta.label_enviar+'" id="jalerta-submit-ajax">');
						$('#jalerta-form').attr('onsubmit', 'return false');
					}else{
						$('#jalerta-botoes').append('<input type="submit" value="'+JAlerta.label_enviar+'">');
					}
					$('#jalerta-botoes').append('<input type="reset" value="'+JAlerta.label_limpar+'">');
					$('#jalerta-botoes').append('<input type="button" value="'+JAlerta.label_cancelar+'" id="jalerta-btn-fechar">');
				break;
				case 'none':
					$('#jalerta-texto').css('padding-bottom','20px');
					$('#jalerta-botoes').append('<input type="button" value="'+JAlerta.label_fechar+'" id="jalerta-btn-fechar">');
				break;
			}
		}else{
			$('#jalerta-botoes').append('<input type="button" value="'+JAlerta.label_fechar+'" id="jalerta-btn-fechar">');
		}
		if((opcoes)&&(opcoes.color)){
			$('#jalerta-corpo').css('border-color', opcoes.color);
			$('#jalerta-titulo').css('background-color', opcoes.color);
			$('#jalerta-botoes input').css('border-color', opcoes.color);
			$('#jalerta-botoes input').css('background-color', opcoes.color);
		}
		JAlerta.centraliza('jalerta-corpo', opcoes);
		if($('#jalerta-btn-fechar')){
			$('#jalerta-btn-fechar').click(function(){
				if(typeof(funcao)!='undefined'){
					JAlerta.fechar(funcao);
					JAlerta.selecao = 's';
				}else{
					JAlerta.fechar();
					JAlerta.selecao = '';
				}
			});
		}
		if($('#jalerta-btn-nao')){
			$('#jalerta-btn-nao').click(function(){
				JAlerta.fechar();
				JAlerta.selecao = 'n';
			});
		}
		if($('#jalerta-btn-sim')){
			$('#jalerta-btn-sim').click(function(){
				if(typeof(funcao)!='undefined'){
					JAlerta.fechar(funcao);
					JAlerta.selecao = 's';
				}else{
					JAlerta.fechar();
					JAlerta.selecao = '';
				}
			});
		}
		if($('#jalerta-submit-ajax')){
			$('#jalerta-submit-ajax').click(function(){
				if(opcoes.method=='post'){
					$.post(opcoes.action,$('#jalerta-form').serialize(),function(data){
						$('#'+opcoes.ajax).html(data);
						JAlerta.fechar(funcao);
						JAlerta.selecao = 'a';
					});
				}else{
					$.get(opcoes.action,$('#jalerta-form').serialize(),function(data){
						$('#'+opcoes.ajax).html(data);
						JAlerta.fechar(funcao);
						JAlerta.selecao = 'a';
					});
				}
			});
		}
	});
};
JAlerta.fechar = function(funcao){
	$('#jalerta-corpo').fadeOut(JAlerta.velocidade, function(){
		$('#jalerta-corpo').remove();
		$('#jalerta-fundo').fadeTo(JAlerta.velocidade, 0, function() {
			$('#jalerta-fundo').remove();
			if(JAlerta.selecao=='s'){
				funcao();
			}
		});
	});
};
alerta = JAlerta.init;
