/*AlternarItens(): faz com que itens fiquem subindo dentro de uma div
configurações:
    - quantidade: quantidade de itens que vão alternar por vez
    - tempo: tempo entre as alternancias(em milisegundos)
    - height(opcional): height que a div principal vai ter 
    - velocidade(opcional): velocidade em que as notícias vão se mover(em milisegundos)*/
$.fn.AlternarItens = function(config) 
{
	return this.each(function() 
	{
        var obj = $(this);
        obj.css({ overflow: 'hidden', position: 'relative' });
        if (config.height) obj.css('height', config.height);
        obj[0].itens = obj.children().css('position', 'absolute');
        
        //posiciona os itens dentro do container
        obj[0].itens.each(function(i) 
        {
            var t = (i == 0) ? 0 : $(obj[0].itens[i - 1]).offset().top + $(obj[0].itens[i - 1]).height() - obj.offset().top;
            $(this).css('top', t + 'px');
        });
		
		obj[0].movimentar = IniciarMovimento;
		obj[0].pausar = PararMovimento;
		obj[0].iniciar = function(){
			var _obj = this;
			this.intervalo = setInterval(function(){_obj.movimentar();}, config.tempo);
		}
        
        obj.css("visibility","visible");
		
		obj[0].iniciar();
		
		$("#carregando").hide();
    });
        
    function IniciarMovimento()
    {

		var obj = $(this);
        //calcula quanto deve mover as divs
        var tamanho = CalcularMovimento(obj);
        //move as divs
        
        obj[0].itens.animate({ 'top': '-=' + tamanho + 'px' }, (config.velocidade)? config.velocidade : 'slow', function() 
        {
            //verifica se é o ultimo item
            if (this == obj[0].itens[obj[0].itens.length - 1]) 
            {
                //varre todos os itens, e manda para baixo os que já sairam    
                obj[0].itens.each(function() 
                {
                    if ($(this).offset().top < obj.offset().top) 
                    {
                        var ultimo = RecuperarUltimoItem(obj);
                        if (ultimo[0] != this) 
                        {
                            $(this).css('top', (ultimo.offset().top + ultimo.height() - obj.offset().top) + 'px');
                        }
                    }
                });
            }
        });
    }
    
    function PararMovimento()
    {
		clearInterval(this.intervalo);
    }

    function CalcularMovimento(box) 
    {
        var height = $(box).height();
        var top = $(box).offset().top;
        var count = 0;
        var tamanho = 0;
        for (var i = 0; i < box[0].itens.length; i++) 
        {
			if( $(box[0].itens[i]).is("div") )
			{
				if ($(box[0].itens[i]).offset().top - top >= height) 
				{
					tamanho += $(box[0].itens[i]).height();
					count++;
					if (count == config.quantidade)
						break;
				}
			}
        }
        if(box[0].itens.length <= config.exibirPorVez)
			return tamanho;
			
        return tamanho + config.quantidade;
    }

    function RecuperarUltimoItem(box) 
    {
        var maior = null, max = 0;
        for (var i = 0; i < box[0].itens.length; i++) 
        {
        
			if( $(box[0].itens[i]).is("div") )
			{
			
				var t = $(box[0].itens[i]).offset().top;
				if (t > max) {
					max = t;
					maior = box[0].itens[i];
				}
            }
        }
        return $(maior);
    }
}
