// Jquery / Quando a pagina carregar dispara a chamada da init.
$(document).ready(init);

function init()
{
	// Faz a paginacao da tabela
	$('.newsList').pager('dt');
	
	// Chama a funcao para imprimir a resenha
	$(".imprimir").click(imprimir);
	
	// Chama a funcao que envia a noticia
	$(".enviar").click(enviarPorEmail);
	
	// Chama a funcao que gera o pdf
	$(".download").click(geraPDF);
	
	$("#flashBar").click(flashPlayer);
	$("#mediaplayerBar").click(mediaPlayer);
	
	// Chama a funcao que faz a busca da noticia
	$("#buscarNoticia").click(buscarNoticia);
	
	$("#buscaNoticia").keypress(function(event){
 		if (event.keyCode == 13) {
 			buscarNoticia();
 		}
 	}) 
	
	$("#cancela").hide();
}

// Responsavel por imprimir a noticia
function imprimir()
{
	$('.news').printElement(); 
}

// Responsavel por gerar o pdf com a noticia
function geraPDF()
{
	var noticiaid = $("#noticiaid").val();
	window.location = (basepath + "index/noticias/montaPDF/" + noticiaid);
}

// Responsavel por abrir a pop up que ira enviar a noticia por email
function enviarPorEmail()
{
	// Defini o tamanho do popup
	var largura = 400;
	var altura  = 350;
	var id = $("#noticiaid").val();
	
	// Calcula o tamanho da tela do usuario
	var esquerda = (screen.width - largura) / 2;
	var topo = (screen.height - altura) / 2;
	
	// Abre o pop com as configuracoes
	window.open(
			basepath + "index/noticias/enviar/"+id,"","width="+largura+", height="+altura+", scrollbars=yes, resizable=yes, toolbar=no, location=no, top="+ topo +", left="+ esquerda
	);
}

// Carrega o pop up com o player de flash
function flashBar()
{
	// Define o tamanho do popup
	var largura = 410;
	var altura  = 160;
	
	// Calcula o tamanho da tela do usuario
	var esquerda = (screen.width - largura) / 2;
	var topo = (screen.height - altura) / 2;
	
	window.open(
		basepath + "index/home/flashPlayer","","width="+largura+", height="+altura+", scrollbars=yes, resizable=yes, toolbar=no, location=no, top="+ topo +", left="+ esquerda
	);
}


// Carrega o pop up com o player de windows media
function mediaplayerBar()
{
	// Define o tamanho do popup
	var largura = 410;
	var altura  = 160;
	
	// Calcula o tamanho da tela do usuario
	var esquerda = (screen.width - largura) / 2;
	var topo = (screen.height - altura) / 2;
	
	window.open(
		basepath + "index/home/mediaPlayer","","width="+largura+", height="+altura+", scrollbars=yes, resizable=yes, toolbar=no, location=no, top="+ topo +", left="+ esquerda
	);
}

function buscarNoticia()
{
	var noticia = $("#buscaNoticia").val();
	
	if(noticia != "" && noticia.length > 3)
	{
		$.post(basepath + "index/noticias/buscaNoticia", {
			noticia:noticia
		}, function(dados){
			$("#resultadoBusca").html(dados);
			$("#nav").html("");
			
			// Faz a paginacao da tabela
			$('.newsList').pager('dt');
			
			// Cancela a busca da noticia
			$("#cancela").show();
			$("#cancela").click(cancelaBusca);
		});
	}else{
		alert("Por favor preencha pelo menos parte da notícia que deseja procurar.");
		$("#buscaNoticia").focus();
	}
}

function cancelaBusca()
{
	window.location = location;
}