NewsFeed = Class.create(); 
NewsFeed.prototype = {
  initialize: function() {
    this.callbacks = [];
    this._isReady = false;
    this.load_data();
  },
  
  isReady: function() {
    return this._isReady;
  },

  onReady: function(callback) {
    if (this._isReady) {
      callback(this.articles);
    }
    else {
      this.callbacks.push(callback);
    }
  },
  
  fireReady: function() {
    this._isReady = true;
    this.callbacks.each(function(callback) {
      callback(this.articles);
    }.bind(this));
  },
  
  getArticles: function() {
    return this.articles;
  },
  
  load_data: function() {
    new Ajax.Request('/load_feed.php', {
      method: 'get',
      evalJSON: 'force',
      onSuccess: this.handle_load_data_success.bind(this),
      onFailure: this.handle_load_data_failure.bind(this)
    });
  },
  
  handle_load_data_success: function( req ) {
    this.articles = $A(req.responseJSON.value.items);
    this.fireReady();
  },
  
  handle_load_data_failure: function() {
  }

}
newsfeed = new NewsFeed();

HeadlineController = Class.create();
HeadlineController.prototype = {
  initialize: function() {
    if (!newsfeed) {
      newsfeed = new NewsFeed();
    }
    this.headline_container = $('headlines');
    this.headline_text = $('headline_text');
    this.current_headline_index = 0;
    this.cycle_interval = 6; // in seconds
    newsfeed.onReady(this.handle_feed_ready.bind(this));
  },
  handle_feed_ready: function(articles) {
    this.headlines = $A(articles).map(function(article) {
      return {
        text: article.title.truncate(150),
        link: article.link
      };
    });
    this.switch_headline(this.headlines[this.current_headline_index]);
    this.cycle.bind(this).delay(this.cycle_interval);
  },
  switch_headline: function(headline) {
    this.fade_out(function() {
      this.update_text(headline.text, headline.link);
      this.fade_in();
    }.bind(this))
  },
  cycle: function() {
    this.current_headline_index ++;
    if (this.current_headline_index >= this.headlines.length) {
      this.current_headline_index = 0;
    }
    this.switch_headline(this.headlines[this.current_headline_index]);
    this.cycle.bind(this).delay(this.cycle_interval);
  },
  fade_out: function(completeCallback) {
    this.headline_animation = new YAHOO.util.Anim(this.headline_text, {
      opacity: { from: 1, to: 0 }
    });
    this.headline_animation.duration = 0.3;
    if (completeCallback) {
      this.headline_animation.onComplete.subscribe(completeCallback);
    }
    this.headline_animation.animate();    
  },
  fade_in: function(completeCallback) {
    this.headline_animation = new YAHOO.util.Anim(this.headline_text, {
      opacity: { from: 0, to: 1 }
    });
    this.headline_animation.duration = 0.3;
    if (completeCallback) {
      this.headline_animation.onComplete.subscribe(completeCallback);
    }
    this.headline_animation.animate();
  },
  update_text: function(text, link) {
    var a_tag = '<a href="' + link + '" target="_BLANK">' + text + '</a>';
    this.headline_text.update(a_tag);
  }
};

YAHOO.util.Event.onDOMReady(function() {
  headline_controller = new HeadlineController();
});

