$(document).ready(function() {		
	$("#inputUsuario").live("focus", function(e) {
		if ($(this).val()=='digite seu e-mail'){
			$(this).val('');
			$(this).css('color','#000');
			//$(this).css('font-family','VAGRoundedBT, Arial, sans-serif');
		}
	 });
	 
	 $("#inputUsuario").live("blur", function(e) {
		if ($(this).val()==""){
			//$(this).css('font-family','');		
			$(this).css('color','#CCC');
			$(this).val('digite seu e-mail');
		}
	 });
	
	$("#inputUsuario").bind("keypress", function(e) {
		 if (e.keyCode == 13) {
			 $('#btLogin').click();
			 return false;
		}
	 });

	$("#inputSenha").bind("keypress", function(e) {
		 if (e.keyCode == 13) {
			 $('#btLogin').click();
			 return false;
		}
	 });

	$('#btLogin').click(function(){
		if($('#inputUsuario').val() && $('#inputSenha').val()){
			$.fancybox.showActivity();
			$.ajax({
				url: 'usuario_script.php?opx=login',
				data: $('#formLogin').serialize()+'&manterConectado='+ $('#cbConectado').is(':checked'),
				success: function(data){ 
					$.fancybox.hideActivity();	
					if(data==0){
						location.reload();
						
					}
					else if(data==1){
						alert('Usuário e/ou senha inválidos!');
						
					}
					else if(data==2){
						alert('Usuário bloqueado!')	;			
					}
					
				},
				type: 'POST'
			});
		}else{
			alert('Favor preencher todos os campos');
		}						
	});	
	
	$('.msgLogin').click(function(){
		alert ('Logar para acessar...');		
	});	
	
	$('#btSair').click(function(){
		$.fancybox.showActivity();
			$.ajax({
				url: 'usuario_script.php?opx=logoutSite',
				success: function(data){
					$.fancybox.hideActivity();	 							
					if(data){
						location.reload();
					}
					else{
						alert ("Falha em sua solicitação");
						location.reload();
						
					}				
				},
				type: 'POST'
			});
	});
	
	//Resultado = 0 Falha 
	//Resultado = 1 Senha incorreta
	/*
	$('.salvar').click(function(){
		$.fancybox.showActivity();
			$.ajax({
				url: 'usuario_script.php?opx=atualizaSenha',
				data: $('#formSenha').serialize(),
				success: function(data){ 	
					$.fancybox.hideActivity();	
					alert(data);
					if(data == 0){
						alert('Houve um problema em sua solicitação!');
					}
					
					else if(data == 1){
						alert('Senha incorreta!');
						location.reload();
					}
					else{
						alert('Senha alterada com sucesso!');
						location.reload();
					}	
							
				},
				type: 'POST'
			});
	});
	*/
	$(".iframe").fancybox({		 		 
		 'transitionIn' : 'none',
		 'transitionOut' : 'none',
		 'type' : 'iframe',
		 'width': 450,
		 'height':220,
		 'onClosed'		: function() {
	    	location.reload();
		}			 
	 });
	 $('.btalterar').click(function(){
		// alert($('#form').serialize());
		if($('#nome').val()  && $('#sobrenome').val() && $('#data_nascimento').val() && $('#sexo').val() && $('#religiao').val() && $('#pais').val()&& $('#cidade').val()&& $('#senhaAtual').val()&& $('#senha').val()){
			$.fancybox.showActivity();
			$.ajax({
				url: 'usuario_script.php?opx=atualizaUsuarioSite',
				data: $('#formCadastro').serialize(),
				dataType: 'json',
				success: function(data){ 
					$.fancybox.hideActivity();	
					if(data == 0){
						alert('Houve um problema em sua solicitação!');
					}
					else if(data == 1){
						alert('Senha atual incorreta!');
					}
					else if(data == 3){
						alert('A senha deve ter no minimo 6 caracteres');
					}
					else{
						alert('Dados alterados com sucesso!');
						location.reload();
					}	
					
				},
				type: 'POST'
			});
		}else{			 
			alert('Favor preencher todos os campos.');
		}							
	});
	
	
	$('.boxPromessas').click(function(){
		var usuario = $('.boxPromessas').attr('id');
		usuario = usuario.replace('usuario','');
	});
	
	
	$('#btexcluir').click(function(){
		 if(confirm("Você tem certeza que deseja excluir?")) {
			 $.ajax({
				url: 'usuario_script.php?opx=deletaImagem',
				success: function(data){ 
					if(data)
						location.reload();					
				},
				type: 'POST'
			});
		}
	});
	
	
		var seletor = 1;

	$("#peridopersonalizado").hide();	
	
	//Formulario de Busca --------------------	 		
			 
	$('#calendario').click(function() { 
		$("#peridopersonalizado").toggle();
		$('#seletor'+seletor).attr('src', 'images/seletor_pedidos.png');
		$(this).find('img:first').attr('src', 'images/seletor_pedidos2.png');
		seletor = 6;		
	}); 
	
	$('#datainicial').click(function() { ;
		$('#seletor'+seletor).attr('src', 'images/seletor_pedidos.png');
		seletor = 6;
		$('#seletor'+seletor).attr('src', 'images/seletor_pedidos2.png');
		
	}); 

	$('#datafinal').click(function() { ;
		$('#seletor'+seletor).attr('src', 'images/seletor_pedidos.png');
		seletor = 6;
		$('#seletor'+seletor).attr('src', 'images/seletor_pedidos2.png');
	}); 


	$('.seletor').click(function(){		
		var data = $('#data').val();
		data = data.split('/');	
		data = new Date(data[0],data[1],data[2]);
		
		var dia = data.getDate();
		var mes = data.getMonth();
		var ano = data.getFullYear();

		if ($(this).find('img').attr('id') == 'seletor1'){		
			$('#datainicial').val('');
			$('#datafinal').val('');
			
			$('#seletor'+seletor).attr('src', 'images/seletor_pedidos.png');
			$(this).find('img').attr('src', 'images/seletor_pedidos2.png');
			seletor = 1;
		}
		
		else if ($(this).find('img').attr('id') == 'seletor2'){		
			var dataInicial = new Date(data.getTime() - (1 * 24 * 60 * 60 * 1000));	
			dataInicial = formataData2(dataInicial.getDate(),dataInicial.getMonth(),dataInicial.getFullYear(),"/")
			
			$('#datainicial').val(dataInicial);
			
			data = formataData2 (dia, mes, ano, "/");
	
			$('#datafinal').val(data);
			
			$('#seletor'+seletor).attr('src', 'images/seletor_pedidos.png');
			$(this).find('img').attr('src', 'images/seletor_pedidos2.png');
			seletor = 2;
		}
		
		else if ($(this).find('img').attr('id') == 'seletor3'){
			var dataInicial = new Date(data.getTime() - (2 * 24 * 60 * 60 * 1000));
			
			dataInicial = formataData2(dataInicial.getDate(),dataInicial.getMonth(),dataInicial.getFullYear(),"/")
			$('#datainicial').val(dataInicial);
			
			data = formataData2 (dia, mes, ano, "/");
			$('#datafinal').val(data);
			
			$('#seletor'+seletor).attr('src', 'images/seletor_pedidos.png');
			$(this).find('img').attr('src', 'images/seletor_pedidos2.png');
			seletor = 3;
			
			
		}
		else if ($(this).find('img').attr('id') == 'seletor4'){
			var dataInicial = new Date(data.getTime() - (7 * 24 * 60 * 60 * 1000));
			dataInicial = formataData2(dataInicial.getDate(),dataInicial.getMonth(),dataInicial.getFullYear(),"/")
			$('#datainicial').val(dataInicial);
			
			data = formataData2 (dia, mes, ano, "/");
			$('#datafinal').val(data);
			
			$('#seletor'+seletor).attr('src', 'images/seletor_pedidos.png');
			$(this).find('img').attr('src', 'images/seletor_pedidos2.png');
			seletor = 4;
		}
		
			else if ($(this).find('img').attr('id') == 'seletor6'){			
			$('#seletor'+seletor).attr('src', 'images/seletor_pedidos.png');
			$(this).find('img:first').attr('src', 'images/seletor_pedidos2.png');
			seletor = 6;
		}
	});
	
	//Ajax -----------------------------------
	
	$('.btBuscarAgradecimento').click(function(){		
		if ($('#seletor6').attr('src') ==  'images/seletor_pedidos2.png' && $('#datainicial').val() == '' && $('#datafinal').val() == ''){		
			alert ('Digite uma data ínicial e final para período personalizado!');
		}
		else{
			$.ajax({
				url: 'admin/agradecimentos_script.php?opx=geraConsultaAgradecimento',
				data: $('#formAgradecimento').serialize(),
				dataType: 'json',
				success: function(data){ 
					if (data)
						window.location = 'agradecimentos/' + data.chave;
					else
						alert ('Nao foi possivel atender a sua solicitacao. Tente novamente mais tarde.');
				},
				type: 'POST'
			});
		}
	});
	
	$('.btBuscarPedido').click(function(){		
		if ($('#seletor6').attr('src') ==  'images/seletor_pedidos2.png' && $('#datainicial').val() == '' && $('#datafinal').val() == ''){		
			alert ('Digite uma data ínicial e final para período personalizado!');
		}
		else{
			$.ajax({
				url: 'admin/pedidos_script.php?opx=geraConsultaPedido',
				data: $('#formPedido').serialize(),
				dataType: 'json',
				success: function(data){ 
					if (data)
						window.location = 'pedidos/' + data.chave;	
					else
						alert ('Nao foi possivel atender a sua solicitacao. Tente novamente mais tarde.');
				},
				type: 'POST'
			});
		}
	});

	
});
 
   
