/**
 * 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=[];

  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;
  }

}
carrossel=new carrossel();