// -------------------------------------------------------------------
// gAjax RSS Feeds Displayer- By Dynamic Drive, available at: http://www.dynamicdrive.com
// Created: July 17th, 2007
// Updated June 14th, 10': Fixed issue in IE where labels would sometimes be associated with the incorrect feed items
// -------------------------------------------------------------------

var gfeedfetcher_loading_image="indicator.gif" //Full URL to "loading" image. No need to config after this line!!
google.load("feeds", "1") //Load Google Ajax Feed API (version 1)


function newWindow(url) {
var day= new Date();
var id = day.getTime();
var win = window.open(url,id,'width=486,height=465');
win.focus();
} 


function gfeedfetcher(divid, divClass, linktarget){
	this.linktarget=linktarget || "_blank" //link target of RSS entries
	this.feedlabels=[] //array holding lables for each RSS feed
	this.feedurls=[]
	this.feeds=[] //array holding combined RSS feeds' entries from Feed API (result.feed.entries)
	this.feedsfetched=0 //number of feeds fetched
	this.feedlimit=5
	this.showoptions="" //Optional components of RSS entry to show (none by default)
	this.viewstyle="lined" //Choose the view style of the RSS (avaiable values: "lined" by default, "tabled")
	this.sortstring="date" //sort by "date" by default
	document.write('<div id="'+divid+'" class="'+divClass+'"></div>') //output div to contain RSS entries
	this.feedcontainer=document.getElementById(divid)
	this.itemcontainer="<li>" //default element wrapping around each RSS entry item
}

gfeedfetcher.prototype.addFeed=function(label, url){
	this.feedlabels[this.feedlabels.length]=label
	this.feedurls[this.feedurls.length]=url
}

gfeedfetcher.prototype.filterfeed=function(feedlimit, sortstr){
	this.feedlimit=feedlimit
	if (typeof sortstr!="undefined") 
		this.sortstring=sortstr
}

gfeedfetcher.prototype.displayoptions=function(parts, view){
	this.showoptions=parts; //set RSS entry options to show ("date, datetime, time, snippet, label, description")
	if (typeof view!="undefined")
		this.viewstyle=view; //set the view style of the RSS	
}

gfeedfetcher.prototype.init=function(){
	this.feedsfetched=0 //reset number of feeds fetched to 0 (in case init() is called more than once)
	this.feeds=[] //reset feeds[] array to empty (in case init() is called more than once)
	this.feedcontainer.innerHTML='<img src="'+gfeedfetcher_loading_image+'" /> Retrieving RSS feed(s)'
	var displayer=this
	for (var i=0; i<this.feedurls.length; i++){ //loop through the specified RSS feeds' URLs
		var feedpointer=new google.feeds.Feed(this.feedurls[i]) //create new instance of Google Ajax Feed API
		var items_to_show=(this.feedlimit<=this.feedurls.length)? 1 : Math.floor(this.feedlimit/this.feedurls.length) //Calculate # of entries to show for each RSS feed
		if (this.feedlimit%this.feedurls.length>0 && this.feedlimit>this.feedurls.length && i==this.feedurls.length-1) //If this is the last RSS feed, and feedlimit/feedurls.length yields a remainder
			items_to_show+=(this.feedlimit%this.feedurls.length) //Add that remainder to the number of entries to show for last RSS feed
		feedpointer.setNumEntries(items_to_show) //set number of items to display
		feedpointer.load(function(label){
			return function(r){
				displayer._fetch_data_as_array(r, label)
			}
		}(this.feedlabels[i])) //call Feed.load() to retrieve and output RSS feed.
	}
}


gfeedfetcher._formatdate=function(datestr, showoptions){
	var itemdate=new Date(datestr)
	var parseddate=(showoptions.indexOf("datetime")!=-1)? itemdate.toLocaleString() : (showoptions.indexOf("date")!=-1)? itemdate.toLocaleDateString() : (showoptions.indexOf("time")!=-1)? itemdate.toLocaleTimeString() : ""
	return "<span class='datefield'>"+parseddate+"</span>"
}

gfeedfetcher._sortarray=function(arr, sortstr){
	var sortstr=(sortstr=="label")? "ddlabel" : sortstr //change "label" string (if entered) to "ddlabel" instead, for internal use
	if (sortstr=="title" || sortstr=="ddlabel"){ //sort array by "title" or "ddlabel" property of RSS feed entries[]
		arr.sort(function(a,b){
		var fielda=a[sortstr].toLowerCase()
		var fieldb=b[sortstr].toLowerCase()
		return (fielda<fieldb)? -1 : (fielda>fieldb)? 1 : 0
		})
	}
	else{ //else, sort by "publishedDate" property (using error handling, as "publishedDate" may not be a valid date str if an error has occured while getting feed
		try{
			arr.sort(function(a,b){return new Date(b.publishedDate)-new Date(a.publishedDate)})
		}
		catch(err){}
	}
}

gfeedfetcher.prototype._fetch_data_as_array=function(result, ddlabel){	
	var thisfeed=(!result.error)? result.feed.entries : "" //get all feed entries as a JSON array or "" if failed
	if (thisfeed==""){ //if error has occured fetching feed
		alert("Google Feed API Error: "+result.error.message)
	}
	for (var i=0; i<thisfeed.length; i++){ //For each entry within feed
		result.feed.entries[i].ddlabel=ddlabel //extend it with a "ddlabel" property
	}
	this.feeds=this.feeds.concat(thisfeed) //add entry to array holding all feed entries
	this._signaldownloadcomplete() //signal the retrieval of this feed as complete (and move on to next one if defined)
}

gfeedfetcher.prototype._signaldownloadcomplete=function(){
	this.feedsfetched+=1
	if (this.feedsfetched==this.feedurls.length) //if all feeds fetched
		this._displayresult(this.feeds, this.viewstyle) //display results
}


gfeedfetcher.prototype._displayresult=function(feeds, view){
	switch(view){
		case "lined":
			this.itemcontainer="<li>";
			var rssoutput="<ul>\n";
			gfeedfetcher._sortarray(feeds, this.sortstring)
			for (var i=0; i<feeds.length; i++){
				var itemtitle="<br><a href=\"#\" onclick=\"newWindow('" + feeds[i].link + "','Image Title',500,250);\" return false;\" class=\"titlefield\">" + feeds[i].title + "</a><hr>"
				var itemlabel=/label/i.test(this.showoptions)? '<span class="labelfield">['+this.feeds[i].ddlabel+']</span>' : " "
				var itemdate=gfeedfetcher._formatdate(feeds[i].publishedDate, this.showoptions)
				var itemdescription=/description/i.test(this.showoptions)? "<img width='120' src=\"" + feeds[i].content + "\">"+feeds[i].content : /snippet/i.test(this.showoptions)? "<a href=\"#\" onclick=\"newWindow('" + feeds[i].link + "','Image Title',500,250);\" return false;\" class=\"titlefield\"><img width='120' src=\"" + feeds[i].contentSnippet + "\" border=\"0\"></a>"   : ""
				rssoutput+=this.itemcontainer + itemdescription + " " + itemlabel + " " + itemdate + "\n" + itemtitle + this.itemcontainer.replace("<", "</") + "\n\n"
			}
			rssoutput+="</ul>";
		break;
		case "tabled":
			this.itemcontainer="<td>";
			var rssoutput="<table style=\"border-collapse: collapse;\">\n";
				gfeedfetcher._sortarray(feeds, this.sortstring)
				for (var i=0; i<feeds.length; i++){
					//reset the startitem and enditem values
					startitem = this.itemcontainer.replace(">", " class=\"class" + i%2 + "\" style=\"vertical-align: top; padding: 2px; text-align: center;\">");
					enditem = this.itemcontainer.replace("<", "</");	
					
					//check if the item number is even or odd
					if(i%2 == 0)
						startitem = "<tr>" + startitem; //even number -> left side
					else
						enditem += "</tr>"; //odd number -> right side
									
						var itemtitle="<br><a href=\"#\" onclick=\"newWindow('" + feeds[i].link + "','Image Title',500,250);\" return false;\" class=\"titlefield\">" + feeds[i].title + "</a>"
						var itemlabel=/label/i.test(this.showoptions)? '<span class="labelfield">['+this.feeds[i].ddlabel+']</span>' : " "
						var itemdate=gfeedfetcher._formatdate(feeds[i].publishedDate, this.showoptions)
						var itemdescription=/description/i.test(this.showoptions)? "<img width='120' src=\"" + feeds[i].content + "\">"+feeds[i].content : /snippet/i.test(this.showoptions)? "<a href=\"#\" onclick=\"newWindow('" + feeds[i].link + "','Image Title',500,250);\" return false;\" class=\"titlefield\"><img width='120' src=\"" + feeds[i].contentSnippet + "\" border=\"0\"></a>"   : ""
						rssoutput+=startitem + itemdescription + " " + itemlabel + " " + itemdate + "\n" + itemtitle + enditem + "\n\n"			
				}
				rssoutput+="</table>";			
		break;
	}
	this.feedcontainer.innerHTML=rssoutput
}
