var month_names = new Array(12);
month_names[0] = "January";
month_names[1] = "February";
month_names[2] = "March";
month_names[3] = "April";
month_names[4] = "May";
month_names[5] = "June";
month_names[6] = "July";
month_names[7] = "August";
month_names[8] = "September";
month_names[9] = "October";
month_names[10] = "November";
month_names[11] = "December";

jQuery.fn.exists = function() {
  return jQuery(this).length > 0;
}

function writeDate() {
  var current_date = new Date();
  month_value = current_date.getMonth();
  year_value = current_date.getFullYear();
  $("#date").html(month_names[month_value] + " " + year_value);
}

function writeNoArticlesFound() {
  $('#latest_news').append('<li>No articles found.</li>');
}

function writeArticles(articles) {
  $.each(articles, function(key, value) {
    $('#latest_news').append('<li class="blog"><a href="/articles/' + value.article.id + '">' + value.article.title + '</a></li>');
  });
}

$(function() {
  if ($('#date').exists()) {
    writeDate();
  }

  if ($('#latest_news').exists()) {
    $.getJSON('/articles.json', function(data) {
      if ($.isEmptyObject(data)) {
        writeNoArticlesFound();
      }
      else {
        writeArticles(data);
      }
    });
  }
});
