/**
 * @projectDescription Gera efeito de carrossel entre uma lista de chamadas.
 *
 * @author 	 Roberto Robson (Bode) rnogueira@uolinc.com
 * @changed  Gustavo Paes gpaes@uolinc.com
 *    - método selectFirstItem criado  
 * @version  1.4
 * @since	   1.0
*/

/**
 * Verifica se existe o setAndAnimate, caso não ele é carregado
 */
if(typeof(setAndAnimate)=="undefined")
  document.write("<scr"+"ipt src=\"http://lib.uol.com.br/setandanimate/setandanimate.js\"></scr"+"ipt>");
  
  
/**
 * Carrossel, gera efeito de carrossel entre uma lista de chamadas.
 * @type {Object}
 */
carrossel=function(){

  /** Id do elemento HTML lista preparado para receber o efeito */
  var id;
  
  /** status do carrossel quando esta girando, 0 = parado, 1 = rodando. Utilizado para ativar ou não o setAndAnimate */
  var rodando=0;

  /** Lista de itens clonados que foram passados para o outro lado da lista */
  this.elementClone=[];
  
  /** Lista de itens originais dos clone que devem ser exlcuidos no final da animação */
  this.elementMove=[];
  
  /** quantidade de elementos na lista */
  this.qtd = 0;

  this.mover=function(){
    if(rodando==0){

      rodando=1;

      if(typeof(arguments[0])=="object" && arguments[0].length>=1)
        arguments=arguments[0];
      
      /** Para onde deve rodar os elementos, > 0 = esquerda, < 0 = direita */
      this.to=arguments[0]['to'];
      
      id=arguments[0]['id'];
  
      if(this.elementClone.length>0)
        this.deletarClone();
      else
        document.getElementById(id).style.marginLeft=0;
  
      this.qtd=document.getElementById(id).getElementsByTagName('li').length;
  
      document.getElementById(id).style.width=(document.getElementById(id).getElementsByTagName('li')[0].offsetWidth*this.qtd)+"px";
      
      this.marginLeftWidth=0;
      
      for(var i=0;i<((this.to<0)?this.to*-1:this.to);i++){
        this.elementMove[this.elementMove.length]=document.getElementById(id).getElementsByTagName('li')[((this.to>0)?i:this.qtd-i-1)];
        this.marginLeftWidth+=this.elementMove[this.elementMove.length-1].offsetWidth;
        this.elementClone[this.elementClone.length]=this.elementMove[this.elementMove.length-1].cloneNode(true);
      }
  
      if(this.to>0){
        for(var i=0;i<((this.to<0)?this.to*-1:this.to);i++){
          document.getElementById(id).appendChild(this.elementClone[i]);
        }
      }else if(this.to<0){
        for(var i=0;i<((this.to<0)?this.to*-1:this.to);i++){
          document.getElementById(id).insertBefore(this.elementClone[i],document.getElementById(id).getElementsByTagName('li')[0]);
        }
        document.getElementById(id).style.marginLeft=(document.getElementById(id).getElementsByTagName('li')[this.to*-1].offsetLeft*-1)+"px";
      }
      

      setAndAnimate.change({
          objName : id+"Animate",
          propriedade : [document.getElementById(id).style, 'marginLeft', 'px'],
          inicio : document.getElementById(id).style.marginLeft,
          fim : ((this.to>0)?this.marginLeftWidth*-1:0),
          callback : function(){ 
              setTimeout(function(){
                carrossel.deletarClone();
                document.getElementById(id).style.marginLeft=0;
                rodando=0;
              },200);
            },
          velocidade : .85
      });

      
    }
  }
  
  this.deletarClone=function(){

    for(var i=0;i<((this.to<0)?this.to*-1:this.to);i++)
      this.elementMove[i].parentNode.removeChild(this.elementMove[i]);

    this.elementClone=[];
    this.elementMove=[];

  }
  
  this.getRodando=function()  {
    return rodando;
  }
  
  this.setWidth=function() {
    document.getElementById(id).style.width=(document.getElementById(id).getElementsByTagName('li')[0].offsetWidth*document.getElementById(id).getElementsByTagName('li').length)+"px";
  }
  
  /** Escolhe qual item deve ser o primeiro do carrossel (gpaes @ 2008-10-14) */
  this.selectFirstItem = function() {
    
    if(typeof arguments[0] != "undefined")
      // define o tipo de busca
      var tipo = typeof arguments[0]['keySearch'] 

    var keySearch = arguments[0]['keySearch'];
    var id        = arguments[0]['id'];

    var itens = document.getElementById(id).getElementsByTagName("li");
    
    if(tipo == "number" && keySearch > itens.length)
      keySearch = 0;

    // percorre todos os ítens a procura do que possui a classe '@item'
    // se o objeto atual não for o procurado, joga para o fim da lista
    if(tipo == "string") {
      for(var i=0;i<itens.length;i++){
        if(itens[0].className.indexOf(keySearch) > -1) // encontrou a classe
          break;
        document.getElementById(id).appendChild(itens[0]);
      }
    }
    else { // por numeração (item da lista)
      for(var i=0;i<keySearch;i++)
        document.getElementById(id).appendChild(itens[0]);
    }

    return true;
  }

}
carrossel=new carrossel();

