TickerHelper = function(cnt_ticker, ticker_speed, animation_speed, min_for_animate, direction){
  this.current_id = 0;
  this.ticker_container = cnt_ticker;
  this.elements = null;
  this.speed = ticker_speed;
  this.animation_speed = animation_speed;
  this.min_for_animate = min_for_animate;
  this.timeout = 0;
  this.direction = direction;
};

TickerHelper.prototype.StartTicker = function(){
  if($('#' + this.ticker_container).length>0){
    this.LoadElements();
    if(this.elements.length >= this.min_for_animate)
    {
      this.Set_Timeout(this.speed);
    }
  }
}

TickerHelper.prototype.LoadElements = function(){
  var thisc = this;
  this.elements = $('#' + this.ticker_container + ' li');
  var width_li = 0;
  this.elements.each(function(){
    width_li += $(this).width();
    $(this).unbind('mouseenter');
    $(this).unbind('mouseleave');
    $(this).mouseenter(function(){
      thisc.Clear_Timeout();
      $(this).mouseleave(function(){
        thisc.Set_Timeout(thisc.speed);
        $(this).unbind('mouseleave');
      });
    });
  });
  if(this.direction == "orizzontale"){
    width_li += 100;
    $('#' + this.ticker_container).css('width', width_li + 'px');
  }
};

TickerHelper.prototype.Set_Timeout = function(p_speed){
  if(this.timeout == 0){
    if(this.direction == "verticale"){
      this.timeout = setTimeout($.proxy(this.Scroll_V, this), p_speed);
    } else if(this.direction == "orizzontale"){
      this.timeout = setTimeout($.proxy(this.Scroll_H, this), p_speed);
    }
  }
}

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

TickerHelper.prototype.Scroll_V = function(){
  this.Clear_Timeout();
  this.LoadElements();
  if(this.elements.length>1){
    var thisc = this;
    var altezza = $(thisc.elements[0]).outerHeight(true) + 'px';
    var margintop_succ = $(thisc.elements[1]).css('margin-top');
    $(thisc.elements[0]).animate(
      {'margin-top': '-' + altezza},
      this.animation_speed,
      function(){
        $(thisc.elements[0]).remove();
        $('#' + thisc.ticker_container).append($(thisc.elements[0]))
        $(thisc.elements[0]).css('margin-top', margintop_succ);
        thisc.Set_Timeout(thisc.speed);
      }
    );
  }
}

TickerHelper.prototype.Scroll_H = function(){
  this.Clear_Timeout();
  this.LoadElements();
  if(this.elements.length>1){
    var thisc = this;
    //var larghezza = $(thisc.elements[0]).css('width');
    var larghezza = $(thisc.elements[0]).outerWidth(true);
    var margintop_succ = $(thisc.elements[1]).css('margin-left');
    $(thisc.elements[0]).animate(
      {'margin-left': '-' + larghezza},
      this.animation_speed,
      function(){
        $(thisc.elements[0]).remove();
        $('#' + thisc.ticker_container).append($(thisc.elements[0]))
        $(thisc.elements[0]).css('margin-left', margintop_succ);
        thisc.Set_Timeout(thisc.speed);
      }
    );
  }
}


