// JavaScript Document

// Carrega os videos na home.
function carregaVideo($id,$start) {
	$('#videosAjax').fadeOut('fast',function(){$(this).load('videos.php?id='+$id+'&start='+$start,function(){$(this).fadeIn('fast');});});
};

// Exibe o relógio na programação.
function relogio(){ 
	var momentoAtual = new Date();
	var hora = ((momentoAtual.getHours()<10) ? "0" : "") + momentoAtual.getHours();
	var minuto = ((momentoAtual.getMinutes()<10) ? "0" : "") + momentoAtual.getMinutes();
	var segundo = ((momentoAtual.getSeconds()<10) ? "0" : "") + momentoAtual.getSeconds();
	var horaImprimivel = hora + ":" + minuto + ":" + segundo;
	$('#relogio').text(horaImprimivel);
	setTimeout("relogio()",1000);
}

// Atualiza programação na home.
function atualizaProg() {
	$('#progAjax').fadeOut('fast',function(){$(this).load('programacao.php',function(){$(this).fadeIn('fast');});});
	setTimeout("atualizaProg()",60000);
}

// Exibe resultado parcial da enquete.
function exibeParcialis(){
	$('#enqueteAjax').fadeOut('fast',function(){$(this).load('enquete.php?parciais=true',function(){$(this).fadeIn('fast');});})
}

// Exibe a enquete atual.
function exibeEnquete(){
	$('#enqueteAjax').fadeOut('fast',function(){$(this).load('enquete.php',function(){$(this).fadeIn('fast');});})
}

// Computa voto na enquete atual.
function votoEnquete(){
	var $opcao = $('input[name=resp]:checked').val();
	if(!$opcao) {
		$('#enqueteErro').fadeIn('fast');
		return false;
	}
	$('#enqueteAjax').fadeOut('fast',function(){$(this).load('enquete.php?parciais=true',{'opcao':$opcao},function(){$(this).fadeIn('fast');});
	});
}

// Funções para o slideshow dos anúncios dos programas.
function SlideShow(slideList, image, speed, name,nameList,links,target,tipo){
	this.slideList = slideList;
	this.image = image;
	this.speed = speed;
	this.name = name;
	this.current = 0;
	this.timer = 0;
	this.nameList = nameList;
	this.Link = links;
	this.Target = target;
	this.Tipo = tipo;
}
function switchImage(imgName, imgSrc, titulo, links, target, tipo){
	if (imgName=='bgtopo') {
		document.getElementById(imgName).background=imgSrc;
	} else {
		document.getElementById(imgName).src=imgSrc;
		document.getElementById(imgName).title=titulo;
		document.getElementById(imgName).alt=titulo;
		
		if (links != ''){
			if (tipo == 'jsc'){
				document.getElementById('linkTopo').href='javascript:void(0);';
				document.getElementById('linkTopo').onClick=function(){ links }
			} else { 
				document.getElementById('linkTopo').href=links;
				document.getElementById('linkTopo').onClick='';
				document.getElementById(imgName).target=target;
			}
		}
	}
}

function SlideShow_play(){
	with(this){
		if(current++ == slideList.length-1) current = 0;
		switchImage(image, slideList[current], nameList[current], Link[current], Target[current], Tipo[current]);
		clearTimeout(timer);
		timer = setTimeout(name+'.play()', speed);
	}
}
SlideShow.prototype.play = SlideShow_play;
// Fim do slideshow.

function ajaxHTML($destino,$path) {
	$('#'+$destino).fadeOut('fast',function(){
		$(this).load($path,function(){
			$(this).fadeIn('fast');
		})
	})
}

// Envia form contato.
function enviaContato($form){
	$dados = $('#'+$form).serialize();
	$.post('contato.inc.php',$dados,function(data){
		if(data.status == 'success') {
			alert(data.msg);
			document.location = 'index.php';
		} else if(data.status == 'codeError') {
			alert(data.msg);
		} else if(data.status == 'failed') {
			alert(data.msg);
			document.location = 'index.php';
		} else {
			alert('Um erro desconhecido ocorreu durante o envio de sua mensagem, por favor tente novamente mais tarde');
			document.location = 'index.php';
		}
	},'json');
}

// Envia form sintonia.
function enviaPesqSinal($form){
	$dados = $('#'+$form).serialize();
	$.post('sintonia.inc.php',$dados,function(data){
		if(data.status == 'success') {
			alert(data.msg);
			document.location = 'index.php';
		} else if(data.status == 'codeError') {
			alert(data.msg);
		} else if(data.status == 'failed') {
			alert(data.msg);
			document.location = 'index.php';
		} else {
			alert('Um erro desconhecido ocorreu durante o envio de sua mensagem, por favor tente novamente mais tarde');
			document.location = 'index.php';
		}
	},'json');
}

// Envia form denuncia.
function enviaDenuncia($form){
	$dados = $('#'+$form).serialize();
	$.post('webdenuncia.inc.php',$dados,function(data){
		if(data.status == 'success') {
			alert(data.msg);
			document.location = 'index.php';
		} else if(data.status == 'codeError') {
			alert(data.msg);
		} else if(data.status == 'failed') {
			alert(data.msg);
			document.location = 'index.php';
		} else {
			alert('Um erro desconhecido ocorreu durante o envio de sua mensagem, por favor tente novamente mais tarde');
			document.location = 'index.php';
		}
	},'json');
}

// Envia form sugestão de pauta
function enviaSugPauta($form){
	$dados = $('#'+$form).serialize();
	$.post('sugestaopauta.inc.php',$dados,function(data){
		if(data.status == 'success') {
			alert(data.msg);
			document.location = 'index.php';
		} else if(data.status == 'codeError') {
			alert(data.msg);
		} else if(data.status == 'failed') {
			alert(data.msg);
			document.location = 'index.php';
		} else {
			alert('Um erro desconhecido ocorreu durante o envio de sua mensagem, por favor tente novamente mais tarde');
			document.location = 'index.php';
		}
	},'json');
}

// Troca pagina
function mudaPagina($pag) {
	if($pag > 1 && $('#noticiasDestaque:visible')) {
		$('#noticiasDestaque').fadeOut('fast');
	} else {
		$('#noticiasDestaque').fadeIn('fast');
	}
	$('#lista > div:visible').fadeOut('fast',function(){
		$('#pag_'+$pag).fadeIn('fast');
	});
	$('.numDestaque').removeClass('numDestaque');
	$('.nPag_'+$pag).addClass('numDestaque');
}

// Troca pagina eventos
function mudaPaginaEv($pag) {
	if($pag > 1 && $('#eventosDestaque:visible')) {
		$('#eventosDestaque').fadeOut('fast');
	} else {
		$('#eventosDestaque').fadeIn('fast');
	}
	$('#eventoterceiro > table:visible').fadeOut('fast',function(){
		$('#pag_'+$pag).fadeIn('fast');
	});
	$('.numDestaque').removeClass('numDestaque');
	$('.nPag_'+$pag).addClass('numDestaque');
}

// Troca pagina hotsites
function mudaPaginaHS($pag) {
	$('.paghotsite:visible').fadeOut('fast',function(){
		$('#pag_'+$pag).fadeIn('fast');
	});
	$('.numDestaque').removeClass('numDestaque');
	$('.nPag_'+$pag).addClass('numDestaque');
}

// Trim
function trim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "").replace(new RegExp("[" + chars + "]+$", "g"), "");
}

//Buscar noticias.
function buscar($key) {
	$key = trim($key);
	if(!$key) { return false; }
	$key = $key.replace(/[\s*]/g,'+');
	document.location = 'buscar/'+$key;
}

function addCover() {
	var $cover = $('#cover');
	$cover.height($(document).height());
	$cover.width($(document).width());
	$cover.fadeIn('fast');
	$cover.bind('click',function(){ocultaCadNews();});
}

function remCover() {
	var $cover = $('#cover');
	$cover.fadeOut('fast');
	$cover.height(0);
	$cover.width(0);
}

function exibeCadNews() {
	addCover();
	$('#cadNews').fadeIn('fast');
	$('#cadNews #nome').focus();
}

function ocultaCadNews() {
	$('#cadNews').fadeOut('fast');
	$('#cadNews input[type=text]').val('');
	$('#cadNewsErro2').text('').hide();
	$('#sucesso').hide();
	$('#form').show();
	remCover();
}

function efetuaCadNews() {
	$('#nome,#email,#security_code').css('border-color','#999');
	var $nome = $('#nome').val();
	var $email = $('#email').val();
	var $code = $('#security_code').val();
	$('#cadNewsErro2').text('').fadeOut('fast');
	
	if($nome == '') {
		$('#nome').css('border-color','#F00');
		$('#cadNewsErro2').text('Preencha o campo "nome"').fadeIn('fast');
		return false;
	}
	if($email == '') {
		$('#email').css('border-color','#F00');
		$('#cadNewsErro2').text('Preencha o campo "E-mail"').fadeIn('fast');
		return false;
	}
	if($code == '') {
		$('#security_code').css('border-color','#F00');
		$('#cadNewsErro2').text('Preencha o campo "Código de segurança"').fadeIn('fast');
		return false;
	}
	
	$.post('cadNews.inc.php',{'nome':$nome,'email':$email,'code':$code},function(data){
		if(data.status == 'sucess') {
			$('#cadNewsErro').text(data.msg);
			$('#form').fadeOut('fast',function(){
				$('#sucesso').fadeIn('fast');
			});
			setTimeout("ocultaCadNews()",5000);
		} else if(data.status == 'error') {
			$('#cadNewsErro').text(data.msg);
			$('#form').fadeOut('fast',function(){
				$('#sucesso').fadeIn('fast');
			});
		} else if(data.status == 'codeError') {
			$('#cadNewsErro2').text(data.msg).fadeIn('fast');
			$('#security_code').css('border-color','#F00');
		}
	},'json');
	
}

function atualizaRSS(){
	$('#rss_ajax').fadeOut('fast',function(){$(this).load('includes/rss_R7.php?max=9',function(){$(this).fadeIn('fast');});});
	setTimeout("atualizaRSS()",600000);
}
