// Copyright 2007 Google Inc.
// All Rights Reserved.

/*
 * @fileoverview Dynamically populate home page content for Google.org from official blog 
 * @author adowning@google.com (Anne Downing)
 */

// maximum number of posts to show on the homepage 
numposts = 4;

// number of characters to show in the preview
var numchars = 650;
 
/**
 * Parse feed from Blogger and populate container with that content
 * @param {json} the JSON object pulled from the Blogger service
*/
 
function listEntries(json) {
  // loop through entries and parse them
  for (var i = 0; i < numposts; i++) {
  // get entry i from feed
    var entry = json.feed.entry[i]
    // get the title
    var title = entry.title.$t;
    // get the URL
    var url = entry.link[2].href;
    // get the date posted
    var date = entry.published.$t.substring(0,10);
    // get the author
    var author = entry.author[0].name.$t;
    // get the content
    if ("content" in entry) {
      var content = entry.content.$t
    } else var content = "";
      // chop content to reasonable length
      content = abbrev(content, numchars);
      // display the results 
      var blogfeed = document.getElementById('blogfeed');
      var html = '<p><strong><a href="' + url + '">' + title + '</a></strong><br>';
	  html += '<span class="posted-by">Posted ' + date + '</span></p>';
      blogfeed.innerHTML += html;
  }
}

/**
 * Truncate a text string to a pre-determined number of characters, then chop off any partial words and append ellipses
 * @param {content} block of text to be truncated
 * @param {size} maximum number of characters to allow
 * @author thorl@google.com (Thor Lewis)
*/

function abbrev(content, size) {
  if ( content.length > size ) {
    content = content.substr(0,size); //chop string to length
    cs = content.split(" "); // split it by spaces
    content=""; //reset content
    for (var s = 0 ; s < cs.length-1 ; s++) {
      content = content + " " + cs[s];
    }
	content = content + "</a></b></i></font>"; // prevents formatting issues if truncated in the middle of a tag
    content = content + "...";
  }
  return content;
}
