﻿    
    function Display(container, ref, height, top, fading, interval)
    {
        this.fade=fading;
        this.headline_count=0;
        this.headline_interval;
        this.old_headline = 0;
        this.current_headline=0;
        this.headlines = new Array(); // an array of jQuery objects
        this.height=height;
        this.top=top;
        this.interval=interval;
        this.ref=ref;
        this.container=container;
        //this.InserzioneInit();
        var context=this;
        $(document).ready(function(){
            context.InserzioneInit();
        });
    }

    Display.prototype.InserzioneInit = function(){
        
      this.headline_count = $("div."+this.ref).size();
      for (var i=0; i < this.headline_count; i++) {
        this.headlines[i] = $("div."+this.ref+":eq("+i+")");
      }
      this.headlines[this.current_headline].css('top',this.top+'px');

      if (this.headline_count>1)
      {
          this.SetIntervallo();
          var context = this;
          $('#'+context.container).hover(function() {
            clearInterval(context.headline_interval);
          }, function() {
            context.SetIntervallo();
          });
      } 
      else if (this.headline_count==1)
      {
        var context=this;
        if (context.fade)
        {
          context.headlines[0].css('display','none');
          context.headlines[0].css('top',context.top+'px');
          context.headlines[0].fadeIn("slow");
        }
        else
        {
          context.headlines[0].show().animate({top: context.top},"slow");
        }
      }
    };

Display.prototype.SetIntervallo = function()
{
    var context=this;
    this.headline_interval = setInterval(function(){
      context.current_headline = (context.old_headline + 1) % context.headline_count;
      if (context.fade)
      {
          context.headlines[context.current_headline].css('display','none');
          context.headlines[context.current_headline].css('top',context.top+'px');
          context.headlines[context.old_headline].fadeOut("slow");
          context.headlines[context.current_headline].fadeIn("slow");
      }
      else
      {
         context.headlines[context.old_headline].animate({top: (context.height*-2)},"slow", function() {
            $(this).css('top', (context.height+10)+'px');
            });
          context.headlines[context.current_headline].show().animate({top: context.top},"slow");
      }
      context.old_headline = context.current_headline;
    },this.interval);
};
