Copertina = function(cnt_ticker, t_container, ticker_speed, animation_speed){
  this.current_id = 0;
  this.ticker_container = cnt_ticker;
  this.elements = null;
  this.schede = new Array();
  this.tab_container = $('#' + t_container);
  this.speed = ticker_speed;
  this.animation_speed = animation_speed;
  this.run = false;
  this.attiva = true;
  this.timeout = 0;
};

Copertina.prototype.StartTicker = function(){
  if(!this.attiva){
    return;
  }
  if($('#' + this.ticker_container).length>0){
    this.LoadElements();
    if(this.elements.length > 1){
      this.run = true;
      this.ShowElement(this.current_id);
    }
  }
}

Copertina.prototype.StopTicker = function(){
  this.Clear_Timeout();
  this.attiva = false;
}

Copertina.prototype.LoadElements = function(){
  if(!this.attiva){
    return;
  }
  this.elements = $('#' + this.ticker_container + ' li');
  
  if(this.elements.length > 1){
    this.tab_container.html('');
    var thisc = this;
    var i = 0;
    thisc.elements.each(function(){
      var src_img = i == 0 ? "/gui-images/copertina-scheda-dark.png" : "/gui-images/copertina-scheda-light.png";
      var titolo = $('a', this).attr('title');
      
      var html_scheda = "";
      html_scheda += "<li>";
      html_scheda += "<a href=\"#\" title=\"" + titolo + "\">";
      html_scheda += "<img src=\"" + src_img + "\" alt=\"" + titolo + "\" title=\"" + titolo + "\" />";
      html_scheda += "</a>";
      html_scheda += "</li>";

      var scheda = $(html_scheda);
      thisc.schede[i] = scheda;
      scheda.bind(
        'click',
        {'li_to_show': i},
        $.proxy(
          function(event){
            this.Clear_Timeout();
            $(this.elements[this.current_id]).hide();
            this.SpegniScheda(this.current_id);
            this.current_id = event.data.li_to_show;
            this.AccendiScheda(this.current_id);
            $(this.elements[this.current_id]).show();
            this.Set_Timeout(this.speed);
            return false;
          },
          thisc
        )
      );
      i++;
      thisc.tab_container.append(scheda);
      
      $(this).unbind('mouseenter');
      $(this).unbind('mouseleave');
      $(this).mouseenter(function(){
        thisc.run = false;
        thisc.Clear_Timeout();
        $(this).mouseleave(function(){
          thisc.run = true;
          thisc.Set_Timeout(thisc.speed);
          $(this).unbind('mouseleave');
        });
      });
    });
  }
};

Copertina.prototype.AccendiScheda = function(id_scheda){
  $('img', this.schede[id_scheda]).attr('src', '/gui-images/copertina-scheda-dark.png');
}

Copertina.prototype.SpegniScheda = function(id_scheda){
  $('img', this.schede[id_scheda]).attr('src', '/gui-images/copertina-scheda-light.png');
}

Copertina.prototype.Set_Timeout = function(p_speed){
  if(!this.attiva){
    return;
  }
  if(this.timeout == 0){
    this.timeout = setTimeout($.proxy(this.ChangeElement, this), p_speed);
  }
}

Copertina.prototype.Clear_Timeout = function(){
  clearTimeout(this.timeout);
  this.timeout = 0;
}

Copertina.prototype.ShowElement = function(id){
  if(!this.attiva){
    return;
  }
  $(this.elements[id]).fadeIn(this.animation_speed);
  if(this.elements.length > 1 && this.run == true)
  {
    this.Set_Timeout(this.speed);
  }
}

Copertina.prototype.ChangeElement = function(){
  if(!this.attiva){
    return;
  }
  this.Clear_Timeout();
  if(this.elements.length > 1){
    var aspetta = false;
    this.elements.each(function(){
      $('img', this).each(function() {
        if(!this.complete || (!$.browser.msie && (typeof this.naturalWidth == "undefined" || this.naturalWidth == 0))){
            aspetta = true;
          }
      });
    });
    
    if(!aspetta){
      var new_id = (this.current_id+1) % this.elements.length;
      $(this.elements[this.current_id]).fadeOut(this.animation_speed, this.ShowElement(new_id));
      this.SpegniScheda(this.current_id);
      this.current_id = new_id;
      this.AccendiScheda(this.current_id);
    } else {
      this.Set_Timeout(this.speed);
    }
  }
  else{
    this.Clear_Timeout();
  }
}



