google.load("feeds", "1") //Load Google Ajax Feed API (version 1)

function rssdisplayer(divid, url, feedlimit, showoptions, categoryFilter, onAfterLoad)
{
	this.showoptions = showoptions || ""; //get string of options to show ("date" and/or "description")
	this.onAfterLoad = onAfterLoad;
	
	// append the category filters to the end of the url if specified
	if( categoryFilter && categoryFilter != "" )
	{
		url += "?category=" + categoryFilter;
	}
	
	var feedpointer = new google.feeds.Feed(url); //create new instance of Google Ajax Feed API
	feedpointer.setNumEntries(feedlimit); //set number of items to display
	document.write('<div id="'+divid+'">Loading feed...</div>');
	this.feedcontainer=document.getElementById(divid);
	var displayer=this;
	feedpointer.load(function(r){displayer.formatoutput(r)}); //call Feed.load() to retrieve and output RSS feed
}


rssdisplayer.prototype.formatdate=function(datestr)
{
	var itemdate=new Date(datestr);
	return "<span style='color:gray; font-size: 90%'>"+itemdate.toLocaleDateString()+"</span>";
}


rssdisplayer.prototype.formatoutput=function(result)
{
	var rssoutput="<ul>";
	
	if (!result.error) //if RSS feed successfully fetched
	{ 
		var thefeeds=result.feed.entries ;//get all feed entries as a JSON array
		
		if (thefeeds.length > 0)
		{
			for (var i=0; i<thefeeds.length; i++) //loop through entries
			{ 
				var itemtitle="<a href=\"" + thefeeds[i].link + "\">" + thefeeds[i].title + "</a>";
				var itemdate=/date/i.test(this.showoptions)? this.formatdate(thefeeds[i].publishedDate) : "";
				var itemdescription=/description/i.test(this.showoptions)? "<br /><div>"+thefeeds[i].content+"</div>" : /snippet/i.test(this.showoptions)? "<br /><div>"+thefeeds[i].contentSnippet+"</div>"  : "";
				rssoutput+="<li>" + itemtitle + " " + itemdate + itemdescription + "</li>";
			}
		}
		else
		{
			rssoutput+="<li>No News and Events available.</li>"
		}
	}
	else //else, output error
	{
		rssoutput+="<li>Error fetching feeds: " + result.error.message + "</li>";
	}
	
	rssoutput+="<li><a href='http://dauntlessfire.blogspot.com/'>more...</a></li></ul>";
	this.feedcontainer.innerHTML=rssoutput;
	
	// Call the method to be run after the results are displayed.
	if( this.onAfterLoad )
	{
		this.onAfterLoad();
	}
}

//USAGE SYNTAX: new rssdisplayer("divid", "rssurl", numberofitems, "displayoptions", "category1,category2")
//new rssdisplayer("adiv", "http://www.cssdrive.com/index.php/news/rss_2.0/", 5, "date, description")