/*******

	***	NewsTicker by Charles Cavalcante ***
	*** http://www.mediagroup.com.br     ***
	
*****/
		
function NewsTicker(nome)
{
	this.atual = this.primeira = this.novo = $("#noticia-item:first", $("#" + nome + " div"));
	this.ultima = $("#" + nome + " div #noticia-item:last");
	this.delay = ($("#noticia-item", $("#" + nome + " div")).length==1)? 0 : 5000;
	this.temporizador = null;
	this.locked = true;
	this.anima();	
}

NewsTicker.prototype.anima = function(novo)
{ 
	var caller = this;
	
	if(!novo)
	{
		this.atual.animate({ top: "0px" }, 300, function(){ caller.locked = false; });
		this.ativaTemporizador();
	}
	else
	{
		caller.locked = true;
		// desce atual
		this.atual.animate
		(
			{ top: "50px" }, // acao
			300, // tempo
			"linear", // tipo de animação
			function() // callback
			{
				// sobe a proxima
				novo.animate({ top: "0px" }, 300, function(){ caller.locked = false; });
			}
		);
		this.atual = novo;
		this.ativaTemporizador();
	}
};

NewsTicker.prototype.ativaTemporizador = function()
{
	if(this.delay > 0)
	{
		var caller = this;
		this.temporizador = setTimeout(function(){ caller.proxima() }, this.delay);
	}
}

NewsTicker.prototype.cancelaTemporizador = function()
{
	this.delay = 0;
	
	if(this.temporizador)
	{
		clearTimeout(this.temporizador);
	}
}

NewsTicker.prototype.proxima = function() 
{
	if(!this.locked)
	{	
		if(this.novo.is(":last-child")) this.novo = this.primeira;
		else this.novo = this.atual.next();

		this.anima(this.novo);
	}
};

NewsTicker.prototype.anterior = function()
{
	if(!this.locked)
	{
		if(this.novo.is(":first-child")) this.novo = this.ultima;
		else this.novo = this.atual.prev();
	
		if(this.novo.attr("id")=="noticia-item") this.anima(this.novo);
		else this.anterior();
	}
};