google.load("feeds", "1") //Load Google Ajax Feed API (version 1)

function rssdisplayer(divid, url, feedlimit, showoptions, category, year, entriestoshow){
	this.showoptions=showoptions || "" //get string of options to show ("date" and/or "description")
	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
	entries = entriestoshow
	categoryselection = category
	yearselection = year
	feedpointer.load(function(r){displayer.formatoutput(r)}) //call Feed.load() to retrieve and output RSS feed
}

function isIn(arr, obj) {
    for(var i=0; i<arr.length; i++) {
        if (arr[i] == obj) return true;
    }
}

rssdisplayer.prototype.formatdate=function(datestr){
	var itemdate=new Date(datestr)
	var itemdate_yr=itemdate.getFullYear()
	var itemdate_mon=itemdate.getMonth()
	var month=new Array(12);
		month[0]="January";
		month[1]="February";
		month[2]="March";
		month[3]="April";
		month[4]="May";
		month[5]="June";
		month[6]="July";
		month[7]="August";
		month[8]="September";
		month[9]="October";
		month[10]="November";
		month[11]="December";
	var itemdate_day=itemdate.getDate()
	var output= month[itemdate_mon] + " " + itemdate_day + ", " + itemdate_yr //format: month/day/year
	return "<span style='color:gray; font-size: 90%'>"+output.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="<ul>"
		var count = 1

		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 />"+thefeeds[i].content : /snippet/i.test(this.showoptions)? "<br />"+thefeeds[i].contentSnippet  : ""
			
			if (count <= entries) { //only output entry if feed has not been filled
				if (isIn(thefeeds[i].categories, categoryselection) || categoryselection == "all") {  //only output entry if in correct category
					if (itemdate.indexOf(yearselection) >= 0 || yearselection == "every") { //only output entry if in correct year
						rssoutput+="<li>" + itemtitle + "<br /> " + itemdate + itemdescription + "</li> <br />"
						count++
					}
				}
			}
		}
		
		rssoutput+="</ul>"
		this.feedcontainer.innerHTML=rssoutput
	}
	else //else, output error
		document.write('');
}

//USAGE SYNTAX: new rssdisplayer("divid", "rssurl", numberofitems, "displayoptions", "category", "year", itemstodisplay)
//new rssdisplayer("adiv", "http://www.cssdrive.com/index.php/news/rss_2.0/", 999, "date, description", "all", "every", 3)
