/*
obj =
{
	"print": function(entries)
	{
		Function is passed argument of mashed feed entries sorted by date descending
		entries =
		[
			{
				"entry": The google feed entry object,
				"feed":
				{
					"url": feed url,
					"dat": whatever was passed as the dat param in addURL() 
				}
			}
		];
	},
	
	"get": function()
	{
		var m = new liltype.FeedMasher(this, "print");
		m.addURL("http://twitter.com/statuses/user_timeline/15140355.rss", {some object or function});
		m.addURL("http://commonknow.typepad.com/blog/atom.xml", {some object or function});
		m.mashit();
	}
}
*/
if (!window.liltype)
{
	liltype = {};
}

liltype.FeedMasher = function (obj, method)
{
	if (obj)
	{
		this.callback =
		{
			"obj": obj,
			"method": method ? method : "callback"
		};
	}
}
new liltype.FeedMasher();
liltype.FeedMasher.prototype = 
{
	"loadingInd": 0,
	"maxEntries": 6,
	"entries": [],
	"feedURLs": [],
	"callback": {"obj": null, "method": null},
	"addURL": function(url, dat)
	{
		this.feedURLs.push({"url": url, "result": null, "dat": dat ? dat : "", "loaded": false});
	},
	"sortEntries": function(entries)
	{
		var ts, entryIdx = [], entryTgt = [], entries, i, c, ii, cc, entryCount = 0;
		for (ts in this.entries)
		{
			entryIdx.push(ts);
		}
		entryIdx.sort(function(a, b){ return b - a; });

		for (i = 0, c = entryIdx.length; i < c; i++)
		{
			entries = this.entries[entryIdx[i]];
			for (ii = 0, cc = entries.length; ii < cc; ii++)
			{
				entryTgt.push(entries[ii]);
				if (++entryCount == this.maxEntries)
				{
					break;
				}
			}
			if (entryCount == this.maxEntries)
			{
				break;
			}
		}
		this.entries = entryTgt;
	},
	"processEntries": function(feedURL)
	{
		var i, c, entry, ts, entries = feedURL.result.feed.entries;
		for (i = 0, c = entries.length; i < c; i++)
		{
			entry = entries[i];
			ts = (new Date(entry.publishedDate)).getTime();
			if (!this.entries[ts])
			{
				this.entries[ts] = [];
			}
			this.entries[ts].push({"entry": entry, "feed": feedURL});
		}
		feedURL.result = null;
	},
	"onLoad": function(result, ind)
	{
		var url, i, c, ready = true, feedURLs = this.feedURLs;
		url = feedURLs[ind];
		url.result = result;
		url.loaded = true;

		for (i = 0, c = feedURLs.length; i < c; i++)
		{
			ready = ready & feedURLs[i].loaded;
		}

		if (ready)
		{
			for (i = 0; i < c; i++)
			{
				this.processEntries(feedURLs[i]);
			}

			this.sortEntries();
			this.callback.obj[this.callback.method](this.entries);
		}
	},
	"onLoadW": function(result)
	{
		this.masher.onLoad(result, this.ind);
		this.feed = null;
	},
	"mashitAnon": function(i, url)
	{
		var obj =
		{
			"masher": this,
			"ind": i,
			"feed": new google.feeds.Feed(url),
			"onLoad": this.onLoadW
		};
		obj.feed.setResultFormat(google.feeds.Feed.JSON_FORMAT);
		obj.feed.setNumEntries(this.maxEntries);
		obj.feed.load(function(result) { obj.onLoad(result); });
	},
	"mashit": function()
	{
		var i, c;

		for (i = 0, c = this.feedURLs.length; i < c; i++)
		{
			this.mashitAnon(i, this.feedURLs[i].url);
		}
	}
};

