var tempo_load_noticia = 20000;
var controlaClickTop10 = true;
var controlaClickNoticias = true;
var timeoutNoticia;
var noticiaAtual;

$(document).ready(function(){

	//click em proxima lista do top10
	$('#top10_prox.enabled').live('click',function() {
		if (controlaClickTop10) {
			
			controlaClickTop10 = false;
			
			$('#top10_elementos_all').animate({
				left: '-=630'
			}, 1000, function() {
				controlaClickTop10 = true;
				$('#top10_prox').removeClass().addClass('disabled');
				$('#top10_ant').removeClass().addClass('enabled');
			});
		}
	});
	
	//click em lista anterior do top10
	$('#top10_ant.enabled').live('click',function() {
		if (controlaClickTop10) {
			
			controlaClickTop10 = false;
			
			$('#top10_elementos_all').animate({
				left: '+=630'
			}, 1000, function() {
				controlaClickTop10 = true;
				$('#top10_ant').removeClass().addClass('disabled');
				$('#top10_prox').removeClass().addClass('enabled');
			});
		}
	});

	//click em proxima lista do top10
	$('#notdest_prox.enabled').live('click',function() {
		if (controlaClickNoticias) {
			
			controlaClickNoticias = false;
			
			$('#notfotosmenu_elementos_all').animate({
				top: '-=337'
			}, 1000, function() {
				controlaClickNoticias = true;
				$('#notdest_prox').removeClass().addClass('disabled');
				$('#notdest_ant').removeClass().addClass('enabled');
			});
		}
	});
	
	//click em lista anterior do top10
	$('#notdest_ant.enabled').live('click',function() {
		if (controlaClickNoticias) {
			
			controlaClickNoticias = false;
			
			$('#notfotosmenu_elementos_all').animate({
				top: '+=337'
			}, 1000, function() {
				controlaClickNoticias = true;
				$('#notdest_ant').removeClass().addClass('disabled');
				$('#notdest_prox').removeClass().addClass('enabled');
			});
		}
	});
	
	$('.notdest').click(function() {	
		carregaNoticia($(this).attr('id'),0);
	});

	$('#marker ul li')
	.mouseenter(function() { 
		$(this).css('background','#C3DFF7');
	})
	.mouseleave(function() { 
		$(this).css('background','#FFFFFF');
	});
	
	$('#cadastro').submit(function(){
		 //valida campos
		 var nome = $('input#nome').val();
		 var email = $('input#email').val();
		 var categorias = [];
	     $('input[name="categorias[]"]:checked').each(function() {
	    	 categorias.push($(this).val());
	     });

		 if (nome == "") {
			 alert("Preencha nome");
			 $('input#murnome').blur();
			 return false;
		 }
		 else if (email == "" || !emailValido(email)) {
			 alert("Preencha email valido");
			 $('input#muremail').blur();
			 return false;
		 }
		 else if (categorias == "") {
			 alert("Escolha ao menos uma categoria");
			 $('input[name="categorias[]"]').blur();
			 return false;
		 }

		 //ajax participar
		 $.post(
	         full_path + 'json/registrar-newsletter/', 
	         { nome: nome, email: email, categorias: categorias },
			 function(data) {
	        	 $.modal.close();
	        	 
		         if (data == 'true') {
		        	 alert('Cadastro efetuado com sucesso!');
		         }
		         else {
		        	 alert('Erro ao cadastrar!');
		         }
		     }
	     );
			
		 return false;
	 });
	 
	 $('.notdest:first').click();
});

function newTimeoutNoticia(tipoNoticia) 
{
	if (timeoutNoticia) {
		clearTimeout(timeoutNoticia);
	}
	
	//acha a noticia a disparar
	timeoutNoticia = setTimeout(function(){carregaNoticia(tipoNoticia,1)},tempo_load_noticia);
}

function carregaNoticia(tipoNoticia,tipoReq)
{
	noticiaAtual = 0;
	var i = 0;
	$('.notdest').each(function(){
		i++;
		if ($(this).attr('id') == tipoNoticia) {
			noticiaAtual = i;
		}
	});

	noticiaAtual = noticiaAtual == 6 ? 0 : noticiaAtual;

	if ((noticiaAtual >= 4 && noticiaAtual <= 6) || noticiaAtual == 0) {
		$('#notdest_prox').click();
	}
	else if (noticiaAtual >= 1 && noticiaAtual <= 3) {
		$('#notdest_ant').click();
	}
	
	
	
	tipoNoticia = tipoNoticia.split('nfa_');
	
	$('#notfoto_carregando').show();
	$('#notfoto_ajax').hide();

	$('.notdest').each(function(){
		$(this).removeClass('atual');
	});
	$('#nfa_'+tipoNoticia[1]).addClass('atual');		

	$.get(full_path + 'json/destaque/id/' + tipoNoticia[1] + '/' + new Date().getTime(),function(data) {
		$('#notfoto_ajax').html(data);
		$('#notfoto_carregando').hide();
		$('#notfoto_ajax').show();
	});
	
	var j = 0;
	$('.notdest').each(function(){
		j++;

		if (j-1 == noticiaAtual) {	
			noticiaAtual = $(this).attr('id');
		}

	});
	
	newTimeoutNoticia(noticiaAtual);
}

function carregaProgramaProximo()
{
	$('p#atualeprox_prox').html('Atualizando programa...');
	 
	$.getJSON(full_path + 'json/get-programa-proximo', function(data) {
		var texto = (data.Pagina != '' && data.Pagina != null) ? 'Depois: <a href="' + full_path + 'programas/ver/programa/' + data.Programa + '">' : 'Depois: ';
		texto += data.Programa;
		texto += (data.Pagina != '' && data.Pagina != null) ? '</a>' : '';

		$('p#atualeprox_prox').html(texto);
	});
	
	setTimeout('carregaProgramaProximo()',tempo_reload_programa);
}

carregaProgramaProximo();

