function Billboard() {
  this.itemwidth = 941;
  this.maxmargin = 0;
  this.autoScroll = 1;
  this.autoScrollDelay = 7000;
  this.autoScrollDuration = 800;
  this.activePanel = 1;
  
  this.wrapper = null;
  this.carousel = null;
  this.items = null;
  this.logos = null;
  
  this.animation = null;
  this.timeout = null;
}

// Init function must be called FIRST!
Billboard.prototype.init = function(itemwidth) {
  this.itemwidth = itemwidth;

  // DOM elements that make the billboard work
  this.wrapper = $('#billboard'); // The outer wrapper
  this.carousel = $('#carousel'); // The inner wrapper
  this.items = $('#carousel > li'); // The different elements, this is an array
  this.logos = $('#media-logos'); // The media logos
  
  this.maxmargin = this.items.length * this.itemwidth - this.itemwidth;
    
  // Setup auto scrolling
  this.setAutoScroll(1);

}

Billboard.prototype.jumpToItem = function(n) {

  if (n < 0 || n > this.items.length) {
    return;
  }

  pos = (n-1) * -this.itemwidth;
      
  // No animation for this one
  this.carousel.css("left", pos+"px");
  
  // Make sure correct buttons are active
  this.activePanel = n;
  this.setControl(n);

}

/**
 *  Activate a panel button
 */
Billboard.prototype.setControl = function(n) {
    
  // Un-active old tabs
  var activeTab = $('#billboard-control').children('li.active');
  activeTab.addClass("inactive");
  activeTab.removeClass("active");
  var activeTab = $('#billboard-control').children('li.last-active');
  activeTab.addClass("last-inactive");
  activeTab.removeClass("last-active");
  
  // Active new tab
  var activeTab = $('#billboard-control li:eq(' + (n-1) + ')');
  if (n == this.items.length) {
    // Last tab - add special class
    activeTab.addClass("last-active");
    activeTab.removeClass("last-inactive");
  } else {
    activeTab.addClass("active");
    activeTab.removeClass("inactive");
  }
  
  // If first tab, then show media logos
  if (n == 1) {
    this.logos.css("display", "");
  } else {
    this.logos.css("display", "none");
  }
  
}

Billboard.prototype.setAutoScroll = function(flag) {
  this.autoScroll = flag;
  
  if (this.autoScroll) {
  
    // Clear old interval
    clearInterval(this.timeout);
  
    var thisBillboard = this;
    this.timeout = setInterval(function () { thisBillboard.nextItem(); }, this.autoScrollDelay);
  } else {
    clearInterval(this.timeout);
  }
  
}

Billboard.prototype.nextItem = function() {

  pos = parseInt( this.carousel.css("left") );

  if (pos == -this.maxmargin) {
    this.rotateToStart();
  } else {
    this.rotateRight();
  }
  
  this.setControl(this.activePanel);
  
}

Billboard.prototype.prevItem = function() {

  pos = parseInt( this.carousel.css("left") );

  if (pos == 0) {
    this.rotateToEnd();
  } else {
    this.rotateLeft();
  }
  
  this.setControl(this.activePanel);
}

Billboard.prototype.rotateToStart = function() {
  this.animation = this.carousel.animate({left: 0}, this.autoScrollDuration);
  this.activePanel = 1;
}

Billboard.prototype.rotateToEnd = function() {
  var pos = this.items.length * this.itemwidth;
  this.animation = this.carousel.animate({left: pos}, this.autoScrollDuration);
  this.activePanel = this.items.length;
}

Billboard.prototype.rotateRight = function() {
  var pos = this.itemwidth * this.activePanel;
  this.animation = this.carousel.animate({left: -pos}, this.autoScrollDuration);
  this.activePanel++;
  if (this.activePanel > this.items.length) {
    this.activePanel = 0;
  }
}

Billboard.prototype.rotateLeft = function() {
  var pos = this.itemwidth * this.activePanel;
  this.animation = this.carousel.animate({left: pos}, this.autoScrollDuration);
  this.activePanel--;
  if (this.activePanel < 0) {
    this.activePanel = this.items.length;
  }
}