var textIniP = "pesquisar";
//slide de notícias	
var first = 0;
var speed = 700;
var pause = 6000;

$(document).ready(function(){
	
	$("#termoPesquisa").val(textIniP);
	
	//newsletter
	$("#termoPesquisa").focus(function(){
		$("#erroNews").html("");
		if($(this).val() == textIniP){
			$(this).val("");
		}
	});
	
	$("#termoPesquisa").blur(function(){
		if($(this).val() == ""){
			$(this).val(textIniP);
		}
	});
	
	$("#formPesquisa").submit(function (){
		return validaPesquisa();
	});

	//slide Notícias	
	if($('.noticiasLinha .noticias .item').length>1){
		interval = setInterval(removeFirst, pause);
	}
	
});

function removeFirst(){
	first = $('.noticiasLinha .noticias .item:first').clone();

	//fade imagem
	$("#img_" + first.attr("rel")).fadeOut(speed);
	
	$('.noticiasLinha .noticias .item:first')
		.fadeOut(speed,function() {
			$(this).remove();
			addLast(first);
		});
}

function addLast(first){
	
	//descelecciona o numero
	$("#num_" + first.attr("rel")).removeClass("selected");
	
	$('.noticiasLinha .noticias').append(first);
	//esconde
	first.hide();
	
	//selecciona o numero
	$("#num_" + $('.noticiasLinha .noticias .item:first').attr("rel")).addClass("selected");
	
	//fade imagem
	$("#img_" + $('.noticiasLinha .noticias .item:first').attr("rel")).fadeIn(speed);
	
	$('.noticiasLinha .noticias .item:first')
		.fadeIn(speed);
}

function validaPesquisa(){
	
	var pEmail  =/^.+@.+\..{2,3}$/
	var retorno = true;
	var sErro = "";

	//se nao tiver um email
	if ($('#termoPesquisa').val() == null || $('#termoPesquisa').val() == "" || $('#termoPesquisa').val() == textIniP) {
		
		sErro = "Tem que inserir o termo a pesquisar!";
		retorno = false;
		
	}else if($('#termoPesquisa').val().length < 4){
		sErro = "A pesquisa deve ter mais do que 3 caracteres!";
		retorno = false;
		
	}
	
 	if(sErro == ""){
	 	$("#erroPesquisa").html("");
	}else{
		$("#erroPesquisa").html(sErro);
	}
 	
	return retorno;
	
}