// 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 checkForTag(tagToCheckFor, entryTags)
{
	for(currentTag in entryTags)
	{
		if(entryTags[currentTag] == tagToCheckFor)
		{
			return "true";
		}
	}
	
	return "false";
}

function initialize() {
    var feed = new google.feeds.Feed("http://blog.kingdomsecurity.co.uk/feeds/posts/default"); 
  feed.setNumEntries(100);
  feed.load(function(result) {
      if (!result.error) {
          var container = document.getElementById("feedlist");

          var header = document.createElement('h3');
          header.className += "first_heading";
          header.appendChild(document.createTextNode("Latest News"));
          container.appendChild(header);

          var postlist = document.createElement('div');
          postlist.className += "headline";
          container.appendChild(postlist);

          for (var i = 0; i < result.feed.entries.length; i++) {
              var entry = result.feed.entries[i];
			  var entryTags = entry.categories;
              var entryDate = new Date(entry.publishedDate).toDateString();

              var postRow = document.createElement('div');
			  postRow.className += "headline-row";

              var postDateCell = document.createElement('div');
			  postDateCell.className += "headline-date";
              postDateCell.appendChild(document.createTextNode(entryDate));

              var postTitleCell = document.createElement('div');
			  postTitleCell.className += "headline-headline";
			  
			  var postLink;
			  
			  if(checkForTag("News", entryTags) == "true")
			  {
				  postLink = document.createElement('a');
				  postLink.appendChild(document.createTextNode(entry.title));
	              postLink.href = entry.link;
			  }
			  else
			  {
				  postLink = document.createTextNode(entry.title);
			  }

              postTitleCell.appendChild(postLink);

              postRow.appendChild(postDateCell);
              postRow.appendChild(postTitleCell);

              postlist.appendChild(postRow);
          }
      }
  });
}

google.setOnLoadCallback(initialize);
