// ----------------------------------------------------------------------------
// -- Google RSS Disaplyer ----------------------------------------------------
// ----------------------------------------------------------------------------

google.load("feeds", "1"); //Load Google Ajax Feed API (version 1)

function rssdisplayer(divid, url, feedlimit, showoptions) {
	return rssdisplayer(divid, url, feedlimit, showoptions, null);
}

function rssdisplayer(divid, url, feedlimit, showoptions, loadingMessage) {
	var d = new Date().getTime();
	divid = divid + '_' + d;
	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 + '">' + (loadingMessage == null ? 'Loading feed' : loadingMessage) + ', </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.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 class='sindicacion'>";

		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) ? thefeeds[i].content : "";
            var itemsnippet = /snippet/i.test(this.showoptions) ? thefeeds[i].contentSnippet : "";
            
            rssoutput += "<li style=\"clear:both; margin-bottom:10px;\"><span class=\"itemTitle\">" + itemtitle + "<br/>" + "</span>";

            if (/date/i.test(this.showoptions)){
            	rssoutput += "<span class=\"itemDate\">" + itemdate + "</span>";
            }

            if (/description/i.test(this.showoptions)){
            	 rssoutput += "<span class=\"itemdescription\">" + itemdescription + "</span>";
          	}
          	
          	if (/snippet/i.test(this.showoptions)){
          		rssoutput += "<span class=\"itemsnippet\">" + itemsnippet + "</span>";
          	}

            rssoutput += "</li>";
        }

        rssoutput += "</ul>";
        this.feedcontainer.innerHTML = rssoutput;
	}
	else { //else, output error
         alert("Error fetching feeds: " + result.error.message);
	}
 };
