﻿$(function(){
	if($('#contato').length > 0){
		$('#contato').submit(function(){
			var nome = $('#nome').val(),
				telefone = $('#telefone').val(),
				msg = $('#msg').val(),
				erro = 0,
				texto = '';
				
			if(nome == ''){
				erro = 1;
				texto += "<br/>Você não preencheu seu nome.<br/>";
			}
			if(email == ''){
				erro = 1;
				texto += "<br/>Você não preencheu seu email.<br/>";
			}
			/*
			Formatos aceitos para o telefone:
			
			(11) 2225-1389
			11 2225-1389
			11 22251389
			1122251389
			2225-1389
			22251389
			*/
			if(telefone == '' || telefone.length > 8){
				if(!telefone.match(/(\(*[0-9]{2}\)*|) *[0-9]{4}-*[0-9]{4}/)){
					erro = 1;
					texto += "<br/>Preencha com um telefone válido.<br/>";
				}
			}
			if(msg == ''){
				erro = 1;
				texto += "<br/>Escreva sua mensagem!<br/>";
			}
			
			if(erro == 1){			
				$('#retorno').fadeIn(1800, function(){
					$(this).children('p').html(texto).slideToggle().fadeIn(1500)
				});
				
				return false;
			} else {
				$('#retorno').fadeOut('fast', function(){
					return true;
				});
			}
		});
	}
	
	$('#voltar').css({
		display: 'none',
		textDecoration: 'none',
		color: '#D82618'
	});
	
	$('#busca_trabalhos').submit(function(){
		var q = $('#busca').val(),
			disp = $('#disp');
			
		(disp.is(':checked')) ? d = 's' : d = 'n';
		
		if(q.match(/(Faça uma busca...)/) || q == ''){
			alert('Preencha o campo de busca!');
		} else {
			$('.cats').fadeOut('fast');
			
			$.post(
				"php/busca.php",
				{q: q, d: d},
				function(dados){
					$('#voltar').css('display', 'block').click(function(e){
						e.preventDefault();
						
						$('#resultados, #voltar').hide();
						$('.cats').fadeIn('fast');
					});
					
					$('#resultados').html(dados).show();
				}
			);
		}
			
		
		return false;
	}).children('#busca').click(function(){
		$(this).select();
	});
	
	$('.cats li a.item, .cats li a.sub-item').click(function(e){
		e.preventDefault();
		
		if($(this).not('.ativo')){
			if($(this).is('.sub-item')){
				$('.cats a.sub-item img').attr('src', 'img/mais.png');
			} else {
				$('.cats a[class] img').attr('src', 'img/mais.png');
			}
		}	

		if($(this).parent().children('ul').css('display') == 'none'){
			if(this.className == 'item'){
				$('.cats a').removeClass('ativo').parent().children('ul').slideUp();
			} else {
				$('.cats .sub-item').removeClass('ativo').parent().children('ul').slideUp();
			}
			
			$(this).addClass('ativo').children('img').attr('src', 'img/menos.png').parent().parent().children('ul').slideDown();
		} else {
			$(this).removeClass('ativo').children('img').attr('src', 'img/mais.png').parent().parent().children('ul').slideUp();
		}
	});
	
	if(location.search != ''){
		var url = location.search.split("="),
			id = url[1];
		
		$('.cats ul li a').each(function(){
			var link_painel = this.href.split("="),
				painel = link_painel[1];
				
			if(painel == id){
				$(this).addClass('ativo') // endereço
					   .parent() // cats ul li ul li
					   .parent() // cats ul li ul
					   .css('display', 'block') // cats ul li ul
					   .parent() // cats ul li
					   .children('a') // cats ul li a
					   .addClass('ativo') // cats ul li a
					   .children('img') // cats ul li a img
					   .attr('src', 'img/menos.png') // cats ul li a img
					   .parent() // cats ul li a
					   .parent() // cats ul li
					   .parent() // cats ul
					   .css('display', 'block') // cats ul
					   .parent() // cats li
					   .children('a') // cats li a
					   .addClass('ativo') // cats li a
					   .children('img') // cats li a img
					   .attr('src', 'img/menos.png'); // cats li a img
			}
		});
	}
});
