google.load("feeds", "1") //Load Google Ajax Feed API (version 1)

function rssdisplayer(divid, url, feedlimit, showoptions){
	
	//get string of options to show ("date" and/or "description")
	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+'">Loading feed...</div>');
	
	this.feedcontainer=document.getElementById(divid)
	
	//call Feed.load() to retrieve and output RSS feed
	var displayer=this
	feedpointer.load(function(r){displayer.formatoutput(r)}) 

}


rssdisplayer.prototype.formatdate=function(datestr){
	var itemdate=new Date(datestr)
	return "<span style='color:#787878; font-weight: bold; font-size: 12px;'>"+itemdate.toLocaleString()+"</span>"
}


rssdisplayer.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="";
		for (var i=0; i<thefeeds.length; i++){ //loop through entries
			
			var itemtitle='<a href="' + thefeeds[i].link + '" class="fancy"><br />&raquo;&nbsp;<span style="font-weight:normal;"  >' +  thefeeds[i].title + '</span></a>';
			
			var itemdate=/date/i.test(this.showoptions)? ""+ this.formatdate(thefeeds[i].publishedDate) : "";

			
			//var itemdate=/date/i.test(this.showoptions)? ""+ this.formatdate(thefeeds[i].publishedDate) : "";
			//
			var itemdescription=/description/i.test(this.showoptions)? "<br />"+thefeeds[i].content : /snippet/i.test(this.showoptions)? ""+thefeeds[i].contentSnippet  : "";
			
			rssoutput+="<div id='hackRSS' style='padding-left:15px; padding-right:15px; padding-bottom:0px; padding-top:0px;'>" + itemtitle +"<br />"+ itemdate +"<br />"+ itemdescription +"</div>";
			
		}
	rssoutput+="";
	this.feedcontainer.innerHTML=rssoutput;
	//startFancy();
	}
	else //else, output error
	alert("Error fetching feeds: "+result.error.message);
}

//USAGE SYNTAX: new rssdisplayer("divid", "rssurl", numberofitems, "displayoptions")
//new rssdisplayer("adiv", "http://www.cssdrive.com/index.php/news/rss_2.0/", 5, "date, description")

