var imgPath = '/thumb.php?s=imagens/database/news/';
var noticias = null;
var noticiaAtual = 0;
var pause = false;

function resetVars(){
	noticias = null;
	noticias = new Array();
	noticiaAtual = 0;
	pause = false;
}
function noticiaClass(id, titulo, conteudo, imagem){
	this.id=id;
	this.titulo=titulo;
	this.conteudo=conteudo;
	this.imagem=imagem;
}
function mostraDados(id){
	var titulo = $('noticiaTitulo');
	var conteudo = $('noticiaConteudo');
	var link = $('noticiaLink');
	if ((conteudo != undefined)&&(noticias!=null) ){
		conteudo.innerHTML = noticias[id].conteudo;
	}
	if ((titulo != undefined)&&(noticias!=null)) {
		titulo.innerHTML = noticias[id].titulo;
	}
	if ((link != undefined)&&(noticias!=null)) {
		link.href = '/noticias/'+ noticias[id].id;
	}
}
function carregaDados(reqURL){
	new Ajax.Request(reqURL, {
		onSuccess: function(transport) {
			/* JSON */
			var decoded = eval( transport.responseText );
			if(decoded.length>0){
				for(var i=0; i<decoded.length;i++){
									
					//noticiaClass(id, titulo, conteudo, imagem)
					noticias.push( 					
						new noticiaClass( decoded[i].id,
							decoded[i].titulo,
							decoded[i].conteudo,
							imgPath + decoded[i].id + '.jpg')
					);
					
					preLoadImg(noticias[i].imagem);
					//alert(noticias[i].id);
				}

				mudaNoticia(0, false);

				setInterval("proximaNoticia()", 8000);				
			}
		}
	});
}
function _mudaNoticia(noticia){	
	var im=document.getElementById('imagem');	
	if(im!=undefined){
		im.src = imgPath + noticia + ".jpg";
	}
	var capa=$('capa');
	if(capa!=undefined){
		capa.fade({ duration: 1.0 });
	}
}
function mudaNoticia(id, pausar){
 	if($('news')!=undefined){
 
		/* limpa botoes */
		for(var i=0; i<noticias.length;i++){
			var xbt = $('bt'+i);
			if(xbt!=undefined){
				xbt.className = "";
			}
		}
		/* destaca */
		var bt = $('bt'+id);
		if(bt!=undefined){
			bt.className+=" sel";
		}
		if(pausar){
			play(false);
		}
		mostraDados(id);		
		var capa = $('capa');
		if(capa != undefined){
			capa.appear({ duration: 0.2 });
		}
		setTimeout("_mudaNoticia("+noticias[id].id+");", 200);
	}
}
function play(pl){
	pause = !pl;
	var implay=document.getElementById('play');
	var impause=document.getElementById('pause');	
	if(pause){
		implay.src='/imagens/control_play.gif';
		impause.src='/imagens/control_pause_a.gif';
	}else{
		implay.src='/imagens/control_play_a.gif';
		impause.src='/imagens/control_pause.gif';		
	}
}
function proximaNoticia(){
	if(!pause){
		noticiaAtual++;
		if(noticiaAtual>=noticias.length){
			noticiaAtual=0;
		}
		mudaNoticia(noticiaAtual, false);
	}
}
function verImagem(id){
	var imagem = $(id);
	if(imagem != null){
		popup(imagem.src, 400, 300, imagem.alt);
	}
}
function loadNews(){
	if( noticias == null){
		resetVars();
		var cp = $('capa');
		if((cp != undefined)&&(cp != null)){
			$('capa').fade({ duration: 0.5 });
			carregaDados('/noticias.php?json');
		}
	}
}