// JavaScript Document
// http://www.quirksmode.org/dom/toc.html
// http://code.google.com/apis/ajaxfeeds/documentation/

// usage: 
// get a key from http://code.google.com/apis/ajaxfeeds/signup.html
//
// put the following in the head of the target document
/*
<script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAErLdnOLOpyGRGST73N1Z1xSLKJJB4muuuRc5w4eSq79-rx4hShTUAL5QAopB686QAqpomg9Wm4hyrg"></script>
*/
// add an empty div with an id of feedlist
//
// optionally populate the empty div with fallback content if js is disabled

google.load("feeds", "1");

function initialize() {
  var feed = new google.feeds.Feed("http://blog.chattertons.com/feeds/posts/default");
  feed.setNumEntries(8);
  
  feed.load(function(result) {
      if (!result.error) {

          // abort if no posts in blog
          if (result.feed.entries.length == 0) {
              return;
          }

          var container = document.getElementById("recent_news_container");

          // abort if container not found
          if (container == null) {
              return;
          }

          // empty out all children
          if (container.hasChildNodes()) {
              while (container.childNodes.length >= 1) {
                  container.removeChild(container.firstChild);
              }
          }

          /*var header = document.createElement('h3');
          header.className += "first_heading";
          header.appendChild(document.createTextNode("Latest News"));
          container.appendChild(header);*/

          var postlist = document.createElement('ul');
          postlist.className += "recent_news_list";

          for (var i = 0; i < result.feed.entries.length; i++) {
              var entry = result.feed.entries[i];

              var postlistitem = document.createElement('li');

              var postheader = document.createElement('p');
              postheader.className += "recent_news_title";

              var postheaderlink = document.createElement('a');
              postheaderlink.appendChild(document.createTextNode(entry.title));
              postheader.appendChild(postheaderlink);
              postheaderlink.href = entry.link;

              postlistitem.appendChild(postheader);

              var postitemcopy = document.createElement('p');
              postitemcopy.className += "recent_news_article";
              postitemcopy.appendChild(document.createTextNode(entry.contentSnippet));

              postlistitem.appendChild(postitemcopy)

              postlist.appendChild(postlistitem);
          }

          container.appendChild(postlist);

          // setup cross fade
          if ($(".recent_news_list").length) {
              $('.recent_news_list').innerfade(
            {
                speed: 'slow',
                animation: 'slide',
                timeout: 4000,
                type: 'sequence',
                containerheight: '70px'
            });
          }
      }
  });
}

// handled by jQuery instead
google.setOnLoadCallback(initialize);