// JavaScript Document

function ver_noticia( id ) {
	$('#noticias_box_esq').show();
	$('#noticias_flash_list').hide();
	$.ajax({
		url: '/ajax/noticias.php',
		type: 'post',
		data: {
			acao: 'noticia',
			noticia: id
		},
		success: function ( retorno ) {
			$('#noticias_content').empty();
			$('#noticias_content').html( retorno );
			$("#content_text_scroll").scrollTo( 0 );
			
		}
	});
}

function listar( retorno ) {
	
	$('#noticias_box_esq').hide();
	$('#noticias_flash_list').show();
	
	$('#noticias_content').empty();
	$('#noticias_content').html( retorno );
	$('a[title=\'noticia\']').unbind('click');
	$('a[title=\'noticia\']').bind('click', function(){
		ver_noticia( $(this).attr('rel') );
		return false;
	});
	
	$('a[title=\'pg_proxima\']').bind('click', function(){
		var pg = $(this).attr('rel');
		$.ajax({
			url: '/ajax/noticias.php',
			type: 'post',
			data: {
				acao: 'listar',
				pag: pg
			},
			success: function(retorno) {
				listar( retorno );
			}
		});
		return false;
	});
	
	$('a[title=\'pg_anterior\']').bind('click', function(){
		var pg = $(this).attr('rel');
		$.ajax({
			url: '/ajax/noticias.php',
			type: 'post',
			data: {
				acao: 'listar',
				pag: pg
			},
			success: function(retorno) {
				listar( retorno );
			}
		});
		return false;
	});
}

$(document).ready(function(){
	
	if (noticia) {
		ver_noticia( noticia );
	}
	else {
		// Recupera a ultima noticia
		$.ajax({
			url: '/ajax/noticias.php',
			type: 'post',
			data: {
				acao: 'inicial'
			},
			success: function( retorno ) {
				$('#noticias_content').empty();
				$('#noticias_content').html( retorno );
			}
		});
	}
	
	// Recupera as ultimas 5 noticias
	$.ajax({
		url: '/ajax/noticias.php',
		type: 'post',
		data: {
			acao: 'ultimas'
		},
		success: function( retorno ) {
			$('#noticias_box_menu_text').html( retorno );
			$('a[title=\'noticia\']').bind('click', function() {
				ver_noticia( $(this).attr('rel') );
				return false;
			});
		}
	});
	
	$('a[rel=\'listar\']').click(function(){
		$.ajax({
			url: '/ajax/noticias.php',
			type: 'post',
			data: {
				acao: 'listar'
			},
			success: function(retorno) {
				listar( retorno );
			}
		});
		return false;
	});

});