/**
 * News
 */
var News = Class.create({
  /**
   * Current news
   *
   * @var array
   */
  news: null,

  /**
   * Current index
   *
   * @var integer
   */
  index: null,

  /**
   * PeriodicalExecuter instance
   *
   * @var PeriodicalExecuter
   */
  timer: null,

  /**
   * Category container
   *
   * @var Element
   */
  category: null,

  /**
   * Subtitle container
   *
   * @var Element
   */
  subtitle: null,

  /**
   * Description container
   *
   * @var Element
   */
  description: null,

  /**
   * Interval
   *
   * @var Numeric
   */
  interval: null,

  /**
   * Start the periodical executer
   *
   * @param news
   * @param options
   */
  initialize: function (news, options)
  {
    this.news = news;
    this.category = $(options.category);
    this.subtitle = $(options.subtitle);
    this.description = $(options.description);
    this.interval = options.interval;
    this.timer = new PeriodicalExecuter(this.showNext.bind(this), options.interval);
    this.showNext();
  },

  /**
   * Show the next news
   */
  showNext: function ()
  {
    // Start or increase the index
    if (Object.isNull(this.index))
    {
      this.index = 0;
    }
    else
    {
      ++this.index;
    }

    // Reset index
    if (this.index == this.news.length)
    {
      this.index = 0;
    }

    // Show the news
    var news = this.news[this.index];
    this.category.update(news[0] + ' - ' + news[1]);
    this.subtitle.update(news[2]);
    this.description.update(news[3] + (' <a href="' + news[4] + '">Veja mais &raquo;</a>'));
  }
});
