// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var sizeCaptions = function() {
  $$('span.caption').each(function(item) {
    var width = item.up().getDimensions().width * 0.9,
        marginRight = width * 0.05;
    item.setStyle({width: (width - marginRight) + 'px'});
  });
};

var populateRecentNews = function(element) {
  new Ajax.Request('/recent_news_links',
    {
      method:'get',
      onSuccess: function(transport){
        var items = transport.responseJSON || [{post_title: "No posts found", guid: '#'}],
            text = '';

        items.each(function(item) {
          item = item.news_post;
          text += "<li><a href='" + item.guid + "'>" + item.post_title + "</a></li>";
        });

        element.update(text);
      },
      onFailure: function(){ element.update("Couldn't retrieve posts"); }
    });
}

document.observe("dom:loaded", function() {
  sizeCaptions();

  var recentNewsLinks = $('recent-news-links');
  if (recentNewsLinks) {
    populateRecentNews(recentNewsLinks);
  }
});

