$(function(){

	$('input.datePicker').datePicker({startDate:'01/01/2007'});

	/* Mascaras */
	$("#data").mask("99/99/9999");
	$("input.data").mask("99/99/9999");
	$('#cartao').mask('9999.9999.9999.9999');
	$('#nascimento').mask('99/99/9999');
	$('#cpf').mask('999.999.999-99');
	$('#cep').mask('99999-999');

	/* Mascaras para os Telefones */
	$('#telefone').mask('(99) 9999-9999');
	$('#celular').mask('(99) 9999-9999');
	$('#numero').mask('(99) 9999-9999');

	$('#tel_primario').mask('(99) 9999-9999');
	$('#tel_secundario').mask('(99) 9999-9999');
	$('#tel_celular').mask('(99) 9999-9999');
	$('#tel_fax').mask('(99) 9999-9999');


	/* Mensagem de Erro */
	$('div.error p').hide().fadeIn();
	$('p.error').hide().fadeIn();
	$('em.error').hide().fadeIn();
	$('h4.error').hide().fadeIn();

	/* Arvore */
	$('div.arvore li').eq(0).attr('class','root');

	$('div.arvore.binaria li').eq(1).attr('class','open');
	$('div.arvore.binaria li').eq(2).attr('class','open');
	$('div.arvore.binaria li').eq(3).attr('class','open');

	$('div.arvore.natural li').eq(1).attr('class','open');
	$('div.arvore.natural li').eq(2).attr('class','open');
	$('div.arvore.natural li').eq(3).attr('class','open');

	$('div.arvore li').each(function(i){
		var id = i+1;
		$(this).attr('id',id);
	});

	$('div.arvore ul').simpleTree({
		autoclose: false,
		animate: false,
		drag: false
	});

	$('div.arvore p span.expandir').click(function(){
		$('div.arvore ul li.root ul li ul').attr('style','display: block;');
		$('div.arvore ul li.folder-close').each(function(){
			$(this).removeClass('folder-close');
			$(this).addClass('folder-open');
		});
	});

	$('div.arvore p span.recolher').click(function(){
		$('div.arvore ul li.root ul li ul').attr('style','display: none;');
		$('div.arvore ul li.folder-open').each(function(){
			$(this).removeClass('folder-open');
			$(this).addClass('folder-close');
		});
	});

	/* Tabelas */
	$('table tr').hover(function(){
		$(this).addClass('hover');
	},function(){
		$(this).removeClass('hover');
	});

	$('table tr td.acao.mais span.mais a').each(function(i){
		$(this).click(function(){
			var style = $('table tr td.acao.mais span.links').eq(i).attr('style');
			if (style=='display: inline;')
			{
				$('table tr td.acao.mais span.links').attr('style','');
			}
			else
			{
				$('table tr td.acao.mais span.links').attr('style','');
				$('table tr td.acao.mais span.links').eq(i).attr('style','display: inline;');
			}
			return false;
		});
	});

	$('table tr td.acao.mais span.links').hover(function(){
		$(this).attr('style','display: inline;');
	},function(){
		$(this).attr('style','');
	
	});

	/* Aprovar */
	$('table td.acao.aprovar a').click(function(){
		if (!confirm('Deseja realmete aprovar?')) {
			return false;
		}
	});

	/* Excluir */
	$('.acao.excluir a').click(function(){
		if (!confirm('Deseja realmete excluir?')) {
			return false;
		}
	});

	$('.acao.excluir').click(function(){
		if (!confirm('Deseja realmete excluir?')) {
			return false;
		}
	});

	$('.acao a.excluir').click(function(){
		if (!confirm('Deseja realmete excluir?')) {
			return false;
		}
	});
	
	/* Cancelar */
	$('.acao.cancelar a').click(function(){
		if (!confirm('Deseja realmete cancelar?')) {
			return false;
		}
	});

	$('.acao.cancelar').click(function(){
		if (!confirm('Deseja realmete cancelar?')) {
			return false;
		}
	});

	$('.acao a.cancelar').click(function(){
		if (!confirm('Deseja realmete cancelar?')) {
			return false;
		}
	});
	
	$('a.cancelar').click(function(){
		if (!confirm('Deseja realmete cancelar?')) {
			return false;
		}
	});

	/* Enviar E-mail */
	$('table td.acao.email span.links a').click(function(){
		if (!confirm('Deseja realmete enviar o e-mail?')) {
			return false;
		}
	});

	/* Lançar Pagamento */
	$('table td.acao.pagar a').click(function(){
		if (!confirm('Pagar?')) {
			return false;
		}
	});

	/* FAQ */
	if(!$("#faq dt").children().not(".titulo").is("span")) {
		$("#faq dt").prepend("<span> + </span>")
	}
	$("#faq").find("dd").hide();
	$("#faq").find("dt").toggle(function() {
			$(this).next().slideDown();	
			$(this).find("span").not(".titulo").empty();
			$(this).prepend("<span> - <span>");
		}, function() {
			$(this).next().slideUp();
			$(this).find("span").not(".titulo").empty();	
			$(this).prepend("<span> + </span>");
		}
	);

	 $('.qualificados').innerfade({ speed: 700, timeout: 2000, type: 'random', containerheight: '2em' }); 

});


function FormataValor(campo,tammax,teclapres)
{
 	//uso:
	//<input type="Text" name="fat_vr_bruto" maxlength="17" onKeyDown="FormataValor(this,17,event)">

	var tecla = teclapres.keyCode;
	vr = campo.value;
	vr = vr.replace("/", "" );
	vr = vr.replace("/", "" );
	vr = vr.replace(",", "" );
	vr = vr.replace(".", "" );
	vr = vr.replace(".", "" );
	vr = vr.replace(".", "" );
	vr = vr.replace(".", "" );
	tam = vr.length;

	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }
	if (tecla == 8 ){ tam = tam-1; }
	if (tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
	{
		if (tam <= 2)
		{ 
	 		campo.value = vr ; }
	 	if ((tam > 2) && (tam <= 5))
		{
	 		campo.value = vr.substr(0,tam-2)+ ',' + vr.substr(tam-2,tam);
		}

	 	if ((tam >= 6) && (tam <= 8))
		{
	 		campo.value = vr.substr(0,tam-5)+ '.' + vr.substr(tam-5, 3)+ ',' + vr.substr(tam-2,tam);
		}

	 	if ((tam >= 9) && (tam <= 11))
		{
	 		campo.value = vr.substr(0,tam-8)+ '.' + vr.substr(tam-8, 3)+ '.' + vr.substr(tam-5, 3)+ ',' + vr.substr(tam-2,tam);
		}

	 	if ((tam >= 12) && (tam <= 14))
		{
	 		campo.value = vr.substr(0,tam-11)+ '.' + vr.substr(tam-11, 3)+ '.' + vr.substr(tam-8, 3)+ '.' + vr.substr(tam-5, 3)+ ',' + vr.substr(tam-2,tam);
		}

	 	if ((tam >= 15) && (tam <= 17))
		{
	 		campo.value = vr.substr(0,tam-14)+ '.' + vr.substr(tam-14, 3)+ '.' + vr.substr(tam-11, 3)+ '.' + vr.substr(tam-8, 3)+ '.' + vr.substr(tam-5, 3)+ ',' + vr.substr(tam-2,tam);
		}
	}			
}
