$ = jQuery.noConflict();

window.currentBanner = 1;
window.currentClient = 1;

function nextBanner() {
	clearInterval(window.timerBanner);
	
	if(currentBanner < 3)
		window.currentBanner++ ;
	else
		window.currentBanner = 1;
		
	marcaLink($('#link'+currentBanner));		
	
	$('#wrapper').scrollTo('#banner'+currentBanner,300);
	
	window.timerBanner = setInterval(nextBanner,15000);	
}
// Marca o link do banner ativo
function marcaLink(lnk){
	$('a[id^="link"]').each(function(i, item){
		if(lnk.attr('id')==$(item).attr('id'))
			lnk.addClass('banner_desq');
		else
			jQuery(item).removeClass('banner_desq');
	});
}

function nextClient() {
	if(currentClient < 6)
		window.currentClient++ ;
	else
		window.currentClient = 1;
	$('#clients-wrapper').scrollTo('#cliente_'+currentClient,300);
}


$(document).ready(function (){
							if($('#link_proposta')!=null){
												$('#link_proposta').click(function() { $('#div_proposta').slideToggle(); });
							}
							if($('#empresas-grupo')!=null){
								$('#empresas-grupo > dt').unbind().click(function(){ 
																				  $('#empresas-grupo > dt > a').css('z-index',0); 
																				  $('#empresas-grupo > dd').fadeOut('fast'); 																				  
																				  $(this).find('a').css('z-index',1); 
																				  $('#'+$(this).attr('id').replace('bt','bg')).fadeIn('fast');
																		});
								$('.bt-fechar').click(function(){ 
															   $(this).parent('dd').fadeOut('fast'); 
															   });								
							}
							
							if($('a[id^="link"]')!=null){
								$('a[id^="link"]').unbind().click(function (){
									$('#wrapper').scrollTo('#'+$(this).attr('id').replace('link','banner'),300);
									marcaLink($(this));																		
								});
							}
							
							if($('#wrapper')!=null){
								window.timerBanner = setInterval(nextBanner,15000);
								marcaLink($("#link"+window.currentBanner));
							}
							
							if($('#clients-wrapper')!=null){
								window.timerBanner = setInterval(nextClient,5000);
							}
							
							});

function deletar(endereco){
	var conf = confirm("Tem certeza de que deseja remover o registro?");
	if (conf) {
		window.location = endereco;
	}
	return false;
}

function concorda() {

	if(document.getElementById('finalizar').style.display == 'none'){
		document.getElementById('finalizar').style.display = '';
		document.getElementById('espera').style.display = 'none';
	}else{
		document.getElementById('finalizar').style.display = 'none';
		document.getElementById('espera').style.display = '';
	}	
}

function MudaLayer(idlayer){
	idlayer = document.getElementById(idlayer);

	if(idlayer.style.display == 'none'){
		idlayer.style.display = '';
	}else{
		idlayer.style.display = 'none';
	}
}

function MudaAbasProduto(idlayer){
	
	switch(idlayer) {
		case 'Objetivos':
			document.getElementById('Objetivos').style.display = '';
			document.getElementById('Metodologia').style.display = 'none';
			document.getElementById('Conteudo').style.display = 'none';
			document.getElementById('Publico').style.display = 'none';
			document.getElementById('Atividades').style.display = 'none';
			document.getElementById('Demonstracao').style.display = 'none';
			break;
		case 'Metodologia':
			document.getElementById('Objetivos').style.display = 'none';
			document.getElementById('Metodologia').style.display = '';
			document.getElementById('Conteudo').style.display = 'none';
			document.getElementById('Publico').style.display = 'none';
			document.getElementById('Atividades').style.display = 'none';
			document.getElementById('Demonstracao').style.display = 'none';			
			break;
		case 'Conteudo':
			document.getElementById('Objetivos').style.display = 'none';
			document.getElementById('Metodologia').style.display = 'none';
			document.getElementById('Conteudo').style.display = '';
			document.getElementById('Publico').style.display = 'none';
			document.getElementById('Atividades').style.display = 'none';
			document.getElementById('Demonstracao').style.display = 'none';			
			break;
		case 'Publico':
			document.getElementById('Objetivos').style.display = 'none';
			document.getElementById('Metodologia').style.display = 'none';
			document.getElementById('Conteudo').style.display = 'none';
			document.getElementById('Publico').style.display = '';
			document.getElementById('Atividades').style.display = 'none';
			document.getElementById('Demonstracao').style.display = 'none';			
			break;
		case 'Atividades':
			document.getElementById('Objetivos').style.display = 'none';
			document.getElementById('Metodologia').style.display = 'none';
			document.getElementById('Conteudo').style.display = 'none';
			document.getElementById('Publico').style.display = 'none';
			document.getElementById('Atividades').style.display = '';
			document.getElementById('Demonstracao').style.display = 'none';			
			break;
		case 'Demonstracao':
			document.getElementById('Objetivos').style.display = 'none';
			document.getElementById('Metodologia').style.display = 'none';
			document.getElementById('Conteudo').style.display = 'none';
			document.getElementById('Publico').style.display = 'none';
			document.getElementById('Atividades').style.display = 'none';
			document.getElementById('Demonstracao').style.display = 'inline';			
			break;			
	}
}

function selecionarTreino(treino,campo) {

	window.opener.document.form.elements["dados["+ campo +"]"].value = treino;
	window.close();

}

function salvarNovoModelo() {
	window.document.form.action = '?vis=planilhas.criarmodeloPlanilha';
	window.document.form.acao.value = 'inserirModeloPlanilha';
	window.form.elements["dados[plm_id]"].disabled = true;
	window.form.submit();
}

function validaSubscribe() { 
	$('#subscribe_erro').hide();
	if($('#subscribe_nome').val()=='' || $('#subscribe_email').val()=='' || !isEmail($('#subscribe_email').val())){
		$('#subscribe_erro').fadeIn().text('Por favor preencha corretamente.').addClass('subscribe_falhou');
		$('#form_subscribe_campos').show();
	} else {
		$('#form_subscribe_campos').hide();		
		$('#subscribe_loading').show('fast');		
		$.ajax({
		   type: "POST",
		   url: "subscribe.php",
		   data: 'nome='+ $('#subscribe_nome').val() + '&email=' +$('#subscribe_email').val(),
		   success: function(msg){
			   	$('#subscribe_loading').hide();
				if(msg == 'true'){
					$('#subscribe_erro').fadeIn().text('Obrigado, seu cadastro foi efetuado com sucesso.').addClass('subscribe_sucesso');
				} else {
					$('#subscribe_erro').fadeIn().text('Email já cadastrado ou inválido.').addClass('subscribe_falhou');
					$('#form_subscribe_campos').show();		
				}
		   }
		 });
	}
	return false;
}

function isEmpty(str){
  return (str == null) || (str.length == 0);
}
// retorna true se a string (str) for um email valido
function isEmail(str){
  if(isEmpty(str)) return false;
  var re = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
  return re.test(str);
}
// retorna true se os caracteres forem alpha (A-Z or a-z)
function isAlpha(str){
  var re = /[^a-zA-Z]/g
  if (re.test(str)) return false;
  return true;
}