// JavaScript Document
google.load("feeds", "1") //Load Google Ajax Feed API (version 1)

function rssdisplayer(divid, url, feedlimit, showoptions){
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+'">Carregant 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 "" + itemdate
}


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="<div>"
for (var i=0; i<thefeeds.length; i++){ //loop through entries
var titol= thefeeds[i].title
var itemtitol=titol.substr(0, 37)
var itemtitle="<a href=\"" + thefeeds[i].link + "\">" + itemtitol + "</a>"
var itemlink= thefeeds[i].link
var itemdate=/date/i.test(this.showoptions)? this.formatdate(thefeeds[i].publishedDate) : ""
var dia=itemdate.substr(8, 2)
var mes=itemdate.substr(4, 3)
var any=itemdate.substr(11, 4)
var itemdescription=/description/i.test(this.showoptions)? "<br />"+thefeeds[i].content : /snippet/i.test(this.showoptions)? "<br />"+thefeeds[i].contentSnippet  : ""
rssoutput+="<h3>El mur de l'Ateneu</h3><h4>"+dia+" "+mes+" "+any+"</h4><p>" + itemdescription + "</p><p><span class='share'><img src='images/social/share.png'><a target='_blank' href='http://www.facebook.com/sharer.php?u=" + itemlink + "' class='image none'><img src='images/social/sharefacebook.png'></a>&nbsp;<a target='_blank' href='http://twitter.com/share?url=" + itemlink + "' class='image none'><img src='images/social/sharetweet.png' class='image none'></a></span><br>&nbsp;</p><hr>" 
}
rssoutput+="</div>"
this.feedcontainer.innerHTML=rssoutput
}
else //else, output error
alert("Error al cercar el feed: "+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")
