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

var MovementTracker = function($conf)
{
	var _name = $conf.name;
	var clip = $conf.clip;
	var track = $conf.track;
	var length = $conf.track.find("li").length;
	var width = $conf.clip.get(0).offsetWidth;
	var max_width = width * length;

	var index = 1;
	var _x = 0;
	
	return {
		size: function()
		{
			return length;
		},
		init: function()
		{
			track.width(max_width).find("li").each(function(i){
				$(this).attr("id", _name + "_slide_" + (i+1)).show();
			});
			
			clip.css({
				"overflow": "hidden"
			});
			
			return this;
		},
		move: function(index)
		{
			_x = ((index - 1) * width) * -1;
			track.fadeOut("normal", function(){
				$(this).css({ marginLeft: _x });
				track.fadeIn();
			})
		},
		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 d_fotos, d_chamadas, t_atualiza;
_c(function(){
	
	d_fotos = new MovementTracker({
		name: "painel-fotos",
		clip: $("#painel-destaques .fotoDestaque"),
		track: $("#painel-destaques .fotoDestaque .destaques-imagens")
	}).init();
	
	d_chamadas = new MovementTracker({
		name: "painel-chamadas",
		clip: $("#painel-destaques .destaqueChamada"),
		track: $("#painel-destaques .destaqueChamada .destaques-chamadas")
	}).init();	

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