var Rotativo = {

  // bloco principal do rotativo
  bloco        : "#slider",

  // total de imagens para o totativo
  totalImgs    : 0,

  // tamanho (em px) das imagens
  tamanhoImgs  : 0,

  // imagem atual (primeira que está sendo exibida
  imgAtual     : 1,

  // imagens exibidas no rotativo
  imgsExibidas : 8,

  // tempo para a transição das imagens
  time         : 1000,

  // indica se o script está sendo rodado
  running      : false,

  /**
   * Inicia o rotativo:
   *  - Mostra a primeira imagem
   *  - Pega o total de imagens
   *  - Pega o tamanho das imagens (em px)
   */
  init : function(){
    this.totalImgs    = $(this.bloco).find("ul li").length;
    this.tamanhoImgs  = (+$(this.bloco).find("ul li:eq(0)").width() +
                        +$(Rotativo.bloco).find("ul li:eq(0)").css("padding-right").replace("px", "") +
                        +$(Rotativo.bloco).find("ul li:eq(0)").css("padding-left").replace("px", ""));

    $(this.bloco).find("ul").css("width", this.tamanhoImgs * (this.totalImgs + 8));
    
    // descobre em qual home está
    var home = location.href.replace(/.*?home([0-9]{1,2})_h\.jhtm/, "$1");
    
    if(isNaN(home)) {
    	//home = this.totalImgs
    	home = this.imgAtual;
      $(document.body).addClass("home" + home);
    }
    else
      $(document.body).addClass("home" + home);
    
    this.imgAtual = home;
    
    // scrolla até a home atual
    this.next( this.imgAtual - 1, false );
  },

  next : function(andar) {
    if(this.running)
      return;

		if(typeof arguments[1] == "undefined")
			var animate = true;
		else
			var animate = arguments[1];

    this.running = true;

		// copia 8 elementos iniciais para o fim da fila
		$(Rotativo.bloco).find("ul li:lt("+(Rotativo.imgAtual-1)+")").clone().appendTo( $(Rotativo.bloco).find("ul") );
		$(Rotativo.bloco).find("ul li:lt("+(Rotativo.imgAtual-1)+")").addClass("remover");

		if(animate) {
	    $(this.bloco).find("ul").animate({
	      marginLeft : "-=" + Rotativo.tamanhoImgs * andar + "px"
	    }, this.time, function() { Rotativo.running = false; });
	    
			setTimeout(function() {
				$(Rotativo.bloco).find("ul").css("margin-left", "0");
				$(Rotativo.bloco).find("ul li.remover").remove();
				Rotativo.imgAtual = 9;
			}, this.time + 100);
	    
	  }
	  else {
	  	$(this.bloco).find("ul").css("margin-left", Rotativo.tamanhoImgs * andar + "px");
	  	
	  	$(Rotativo.bloco).find("ul").css("margin-left", "0");
	  	$(Rotativo.bloco).find("ul li.remover").remove();
	  	Rotativo.imgAtual = 9;
	  	Rotativo.running = false;
	  }
	  
		if(!animate)
			setTimeout(function() { $(Rotativo.bloco).find("ul").css("visibility", "visible") }, 250);

  },

  prev : function(andar) {
    if(this.running)
      return;

    this.running = true;

		// copia 8 elementos finais para o inicio da fila
		$(Rotativo.bloco).find("ul li:gt("+(Rotativo.totalImgs - 9)+")").addClass("remover").clone().removeClass("remover").prependTo( $(Rotativo.bloco).find("ul") );
		$(Rotativo.bloco).find("ul").css("margin-left", "-"+Rotativo.tamanhoImgs * 8 + "px");

    $(this.bloco).find("ul").animate({
      marginLeft : "+=" + Rotativo.tamanhoImgs * andar + "px"
    }, this.time, function() { Rotativo.running = false; });

		setTimeout(function() {
			$(Rotativo.bloco).find("ul").css("margin-left", "0");
			$(Rotativo.bloco).find("ul li.remover").remove();
			Rotativo.imgAtual = 9;
		}, this.time + 100);

  }


}




