google.load("feeds", "1") //Load Google Ajax Feed API (version 1)

function viewer(divid, url, feedlimit, showoptions){
    //get string of options to show ("date" and/or "description")
    
    //make sure it is not null, by appending an empty string (trick it).
    this.showoptions=showoptions || ""
    
    //create new instance of Google Ajax Feed API
    var feedpointer=new google.feeds.Feed(url)
    
    //set number of items to display
    feedpointer.setNumEntries(feedlimit)
    document.write('<div id="'+divid+'">No news found.</div>')
    this.feedcontainer=document.getElementById(divid)
    var displayer=this
    //call Feed.load() to retrieve and output RSS feed
    feedpointer.load(function(r){displayer.formatoutput(r)})
}

//create a 'closure' called formatdate
viewer.prototype.formatdate = function(datestr){
    var itemdate=new Date(datestr)
    return itemdate.toLocaleDateString();
}


viewer.prototype.formatoutput = function(result){
if (!result.error){ //if RSS feed successfully fetched
var thefeeds=result.feed.entries //get all feed entries as a JSON array

var rssoutput=""
//check if the description option is provided, if no, make a bulleted list.
if (this.showoptions.indexOf('description') > 0){
  rssoutput="";
}else{
  rssoutput="<ul>";
}

for (var i=0; i<thefeeds.length; i++){ //loop through entries

var itemtitle="";

if (this.showoptions.indexOf('titlePlainText') > 0){
    itemtitle="<span class='bold'>" + thefeeds[i].title + "</span>"
}else{
    itemtitle="<a href=\"" + thefeeds[i].link + "\">" + thefeeds[i].title + "</a>"
}

var itemdate=/date/i.test(this.showoptions)? this.formatdate(thefeeds[i].publishedDate) : ""
var itemcreator=/author/i.test(this.showoptions)? ""+thefeeds[i].author : ""
var itemdescription=/description/i.test(this.showoptions)? thefeeds[i].content : /snippet/i.test(this.showoptions)? thefeeds[i].contentSnippet  : ""


if (this.showoptions.indexOf('description') <= 0){
    rssoutput+="<li><b>" + itemtitle + "</b>&nbsp;&nbsp;<span style='font-size:smaller;color:gray'>" + itemdate  + "&nbsp;" + itemcreator + "</span></li>"    
}else{
rssoutput+="<p><b>" + itemtitle + "</b><br><span style='font-size:smaller;color:gray'>" + itemdate  + "&nbsp;" + itemcreator + "</span><br>" + itemdescription +"</p>"    
}

}

if (this.showoptions.indexOf('description') > 0){
  rssoutput+="";
}else{
  rssoutput+="</ul>";
}

this.feedcontainer.innerHTML=rssoutput
}
}