function history_fnc (hash){
	if (hash.match(/^noticias\/\d+/))
		get_noticia(hash.split('/')[1])
	else
		get_noticias()
}

$(document).ready(function(){
	$.historyInit(history_fnc);
	ajaxificar();
});

function get_noticia (id){
	display_cargando(function(){
		$.ajax({
			type: 'GET',
			url: '/noticias/'+id+'/',
			success:function(msg){
						$('#cargando').fadeOut('slow', function(){
							$('#contenido').html(msg).fadeIn('slow', function(){
							});
						});
					},
			error:	function(){
						$('#cargando').fadeOut('slow');
						alert('Ocurrió un error inesperado, intente de nuevo');
					} 
		});
	});
}

function display_cargando(fnc){
	$('#contenido').
	fadeOut('slow', function(){
		$('#cargando').fadeIn('slow', fnc);
	});
}

function ajaxificar (){
	$("a.titulo").unbind('click');
	$("a.titulo").click(function(){
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);
		return false;
	});
}

function get_noticias(){
	display_cargando(function(){
		$.ajax({
			type: 'GET',
			url: '/noticias/',
			success:function(msg){
						$('#cargando').fadeOut('slow', function(){
							$('#contenido').html(msg).fadeIn('slow', function(){
								ajaxificar();
							});
						});
					},
			error:	function(){
						$('#cargando').fadeOut('slow');
						alert('Ocurrió un error inesperado, intente de nuevo');
					} 
		});
	});
}
