/* by Lucas Ferreira - http://www.burnweb.com.br/ */

var MovementTracker = function($conf)
{
	var itens = [];
	var _name = $conf.name;
	var painel = $conf.painel.find("a");
	var data = $conf.data;
	var length = $conf.data.length;

	var index = 1;
	var _x = 0;
	
	return {
		size: function()
		{
			return length;
		},
		init: function()
		{
			data.each(function(){
				
				var tempItem = {};
				tempItem.label = $(this).find("a").attr("title");
				tempItem.uri = $(this).find("a").attr("href");
				
				var img = $(this).find("a").attr("rel").split("src=");
				if(img.length > 1 && img[1].length > 0)
				{
					tempItem.img = img[1];
				}
				else
				{
					tempItem.img = false;
				}
				
				itens.push(tempItem);
				
			});
			
			painel.find("img").load(function(){
				$(this).parent().fadeIn();
				if(t_atualiza) t_atualiza.play();
			});
			
			this.move(1);
			
			return this;
		},
		move: function(index)
		{
			var item = itens[index-1];
			painel.fadeOut("normal", function(){
				
				painel
					.attr("href", item.uri)
					.attr("title", item.label);
					
				painel.find("strong").html(item.label);
				
				if(item.img)
				{
					painel.find("img")
						.attr("src", item.img)
						.attr("alt", item.label)
						.show();
				}
				else
				{
					painel.find("img").hide();
					painel.fadeIn();
				}
				
			});
			
			if(t_atualiza) t_atualiza.pause();
		},
		next: function()
		{
			if(index >= length)
			{
				index = 1;
			}
			else
			{
				index++;
			}
			
			this.move(index);
		},
		prev: function()
		{
			if(index <= 1)
			{
				index = length;
			}
			else
			{
				index--;
			}
			
			this.move(index);
		}
	}
};

var TimerUpdate = function(time)
{
	var time_to_update = time * 1000;
	var _onUpdate = [];
	
	return {
		init: function()
		{
			this.pause();
			this.is_paused = false;
			
			this._timer = setInterval( _d( this, this.update ), time_to_update);
			
			return this;
		},
		_timer: null,
		is_paused: false,
		pause: function()
		{
			this.is_paused = true;
			clearInterval(this._timer);
			this._timer = null;
			
			return this;
		},
		play: function()
		{
			this.is_paused = false;
			return this.init();
		},
		setTime: function(t)
		{
			time_to_update = t * 100;
			return this.init();
		},
		onUpdate: function(f)
		{
			_onUpdate.push(f);
			return this;
		},
		update: function()
		{
			for(var i=0; i<_onUpdate.length; i++)
			{
				try {
					_onUpdate[i](this, i, time_to_update);
				} catch(ex) {}
			}
		}
	}
};

var destaques, t_atualiza;
_c(function(){
	
	$('#capa #conteudo #box-noticias .painel .foto-g a strong').css({
		'opacity' : 0.9
	});
	
	destaques = new MovementTracker({
		name: "painel-destaque",
		painel: $("#news-foto-g"),
		data: $("#box-noticias .painel .lista-news ul li")
	}).init();	

	function next()
	{
		destaques.next();
	}
	
	$("#box-noticias .track #bt-news-prev").click(function(e){
		
		destaques.prev();
		t_atualiza.play();
		
		Event.cancel(e);
		return false;
	});
	
	$("#box-noticias .track #bt-news-next").click(function(e){
		
		next();
		t_atualiza.play();
		
		Event.cancel(e);
		return false;
	});
	
	//7 segundos para trocar de slide...
	if(destaques.size() > 1)
	{
		t_atualiza = new TimerUpdate(7);
		t_atualiza.onUpdate(next);
		t_atualiza.init();
	}
	else
	{
		$("#painel-destaques .controles a").hide();
	}
});