$(document).ready(function(){


	/* HOME */
	/* Esconder texto dos inputs ao clicar */
	$('#txtNewsNome').focus(function(){ 
		if ($(this).val() == 'Nome') {$(this).val(''); }
	});
	$('#txtNewsNome').blur(function(){
		if ($(this).val() == '') { $(this).val('Nome'); }
	});

	$('#txtNewsEmail').focus(function(){
		if ($(this).val() == 'E-mail') { $(this).val(''); }
	});
	$('#txtNewsEmail').blur(function(){
		if ($(this).val() == '') { $(this).val('E-mail'); }
	});


	/* FORM DO NEWLETTER */

	$("#txtNewsNome").focus(function(){
		if($(this).val() == "nome" || $(this).val() == "cadastrado com sucesso"){
			$(this).val('');
		}
	});
	
	$("#txtNewsNome").blur(function(){
		if($(this).val() == ""){
			$(this).val('nome');
		}
	});

	$("#txtNewsEmail").focus(function(){
		if($(this).val() == "e-mail" || $(this).val() == "cadastrado com sucesso"){
			$(this).val('');
		}
	});
	
	$("#txtNewsEmail").blur(function(){
		if($(this).val() == ""){
			$(this).val('e-mail');
		}
	});
	
	$("#enviarNewsletter").submit(function(){ $("#btnOK").click(); return false; });
	
	$("#btnOK").click(function(){
		var nome = $("#txtNewsNome").val();
		var email = $("#txtNewsEmail").val();
		var cota_dia = $("#txtNewsCotaDiaria").val();
		var cota_rel = $("#txtNewsCotaRelatorios").val();
		
		
		if($("#txtNewsCotaDiaria").is(":checked")){  
			cota_dia = 1;
		}else{
			cota_dia = 0;
		}
		
		
		if($("#txtNewsCotaRelatorios").is(":checked")){  
			cota_rel = 1;
		}else{
			cota_rel = 0;
		}
		
		
		if (nome == "nome" || email == "e-mail" || email.indexOf('@', 2) == -1 || email.indexOf('.', 1) == -1) {
			alert('Por favor preencha os campos corretamente.');
			return;
		}
		
		//$("#enviarNewsletter input").hide();
		//$(".txtNewsOpcoesLabel").hide();
		$('#enviarNewsletter fieldset').append('<p class="msg">Enviando...</p>');
		
		$.post("/includes/newsletter.php", { nome: nome , email: email , cota_dia: cota_dia , cota_rel: cota_rel },
		   function(data){
		   //alert(data);
		   $('#enviarNewsletter p.msg').html('Cadastrado com sucesso');
		   $("#txtNewsNome").val('Nome');
		   $("#txtNewsEmail").val('E-mail');
		   //$("#txtNewsCotaRelatorios").reset();
		   //$("#txtNewsCotaDiaria").reset();
		   $('#txtNewsCotaRelatorios').attr('checked', false);
		   $('#txtNewsCotaDiaria').attr('checked', false);
		   $('#enviarNewsletter p.msg').fadeOut(3000);
		   //$("input:reset");
		});
		
	});
	
	
	/* FIM DO FORM DO NEWSLETTER*/



	/* RODAPÉ */
	/* Mostrar texto rodape completo */

	$('#verTxtRodape').toggle(function(){
		$('#txtRodapeCompleto').slideDown();
		$('#pl_info').slideDown();
		$('#pl_info').css('display', '');
		$(this).html('- ver texto resumido');
		return false;
	}, function(){
		$('#txtRodapeCompleto').slideUp();
		$('#pl_info').slideUp();
		$('#pl_info').css('display', 'none');
		$(this).html('+ ver texto completo');
		return false;
	});



	/* SUBMENUS */
	/* Highlight Submenus Laterais */
	urllink = window.location.href;
	excluirCaracteres = urllink.search('.com.br') + 7; /* Excluir o dominio da url */
	urllink = urllink.substring(excluirCaracteres); /* Extrair url sem o dominio */

	$('#subMenu a').each(function(){
		link = $(this).attr('href');
		if(link == urllink){
			$(this).parent().addClass('selecionado');
		}
	});



	/* EQUIPE */
	/* Exibir minicurriculos */
	$('.miniCurriculo').append('<span class="setinha" />');

	$('.listaEquipe .nome').hover(function(){
		nome = $(this);
		delay = setTimeout(function(){
			nome.next().fadeIn(150);		
		}, 150)
	}, function(){
		clearTimeout(delay);
		$(this).next().hide();
	});
	$('.listaEquipe .nome').click(function(){ return false; });



	/* INTERATIVO */
	/* Box Premiações */
	if($('#lstPremiacoesArquivo').attr('id')){
		/*$("#lstPremiacoesArquivo a").fancybox({
			'autoScale' : false,
			'overlayColor' : '#000',
			'titlePosition' : 'outside'
		});
		*/
	}


	$('#lstPremiacoesArquivo li ul').hide();
	$('#lstPremiacoesArquivo li:first').find('ul').show();

	$('#lstPremiacoesArquivo h3').click(function(){
		$('#lstPremiacoesArquivo li ul').hide();
		$(this).next().toggle();
		return false;
	});
	
	//Premiacao
		
	$("#lstPremiacoesArquivo ul li").click(function() {
    
		var id_premio = $(this).attr("value");
		
		$("#galleryLoad").html('');

		$.getJSON("http://www.perfininvestimentos.com.br/includes/premiacoes.php",{ id_premio: id_premio },
			function(data){
				
				$.each(data, function(i, j){
					$("<a/>").appendTo("#galleryLoad").attr("rel", "group").attr("title", j.title).attr("href", j.href).append($("<img/>").attr("src", j.href));
				});
			
				$("#galleryLoad a").fancybox({
					'titlePosition': 'inside'
				});

				$("#galleryLoad a:first").trigger('click');
			});
			
	
	});	
	
	
	/* Botão imprimir */
	$("#btn_imprimir").click(function(){
		window.print();											 
		return false;
	});
	
	/* FUNDOS */
	
	$("#loading").ajaxStart(function(){
		$(this).fadeIn();
		$(this).html('<img src="../imagem/loading.gif" />Carregando...');
		$("#exibirTabelaHistorico").html('');
		
	});
	$("#loading").ajaxComplete(function(){
		$(this).fadeOut();
	});

	$("#formHistoricoCotas").validate({
		submitHandler: function(form) {
			var data_inicio = $("#dataInicial").val();
			var data_final = $("#dataFinal").val();
			var url = $("#url").val();
			
			$.post("historico-cotas.php",
			{
				data_inicio			: data_inicio,
				data_final			: data_final,
				url					: url,
				action				: "historico-cotas"
			},
			function(data){
				if (data != 0) {
					$("#exibirTabelaHistorico").animate({"height": "90px"}, 1000).html(data);	
				} else {
					$("#exibirTabelaHistorico").animate({"height": "40px"}, 1000).html('<p>Nenhum resultado encontrado.</p>');	
				}
				
			});
			return false;
		},
		rules: {  
			dataInicial: {
				required	: true
			},  
			dataFinal: {
				required	: true
			}
		},
		messages: {  
			dataInicial: {
				required	: "Selecione uma data inicial"
			},  
			dataFinal: {
				required	: "Selecione uma data final"
			}
		},
		errorPlacement: function(error, element) {
			/* posicionamento das mensagens de erro */
			if (element.attr("name") == "dataInicial" || element.attr("name") == "dataFinal" )
				$('#formHistoricoCotas').append(error);
		}
	});
	
	
});
