/**
 * @author yohoo
 */
var rssnews = new Array();
var rsslatest = new Array();
var rsscb = '';

var rssnewsitem = Class.create();
rssnewsitem.prototype = 
{
	initialize : function(ti, li, de)
	{
		this.title = ti;
		this.link = li;
		this.desc = de;		
		debug("rssapi Creating new newsitem "+ti+", "+li+", "+de);
	}
}

function callRss(source, callback)
{
	debug("callRss with source="+source);
	var url = "http://www.mashupstation.com/station/rss-api/rss-api.php";
	var pars = "source="+source;
	cb = callback;
	var myAjax = new Ajax.Request(
			url, 
			{
				method: 'get',
				parameters: pars, 
				onComplete: rssShowResponse
			});
} 

function rssGetLatest()
{
	var a = rsslatest;
	rsslatest = new Array();
	debug("getLatest returning "+a.length+" items");
	return a;
}

function rssShowResponse(originalRequest)
{	
	var rssfeed = originalRequest.responseText;
	//debug("rssShowResponse called with "+rssfeed);
	
	// Split all elements between "|"
	var ta = rssfeed.split('|');
	debug("rssShowResponse handling "+ta.length+"items");	
	ta.each(function(t)
	{
		//debug("rssShowResponse handling "+t);
		var tb = t.split('#');
		var title = tb[0];
		//debug("rssShowResponse title = "+title);
		var link = tb[1];
		//debug("rssShowResponse link = "+link);
		var desc = tb[2];
		//debug("rssShowResponse desc = "+desc);		
		var n = new rssnewsitem(title, link, desc);
		rsslatest.push(n);	
		rssnews.push(n);
	});
	rsscb();
}

function rssContainsNewsItem(a1, i1)
{
	var rv = false;
	a1.each(function(i)
	{
		if (i.title == i1.title && i.link == i1.link)
		{
			rv = true;
		}
	});
	//debug("rssContainsNewsItem returns "+rv);
	return rv;
}

