
/* Configuration: */

var tabs = {
	"@shevisions" : {
		"feed"		: "http://twitter.com/statuses/user_timeline/17093617.rss",
		"function"	: twitter
	},
	
	"Wordpress": {
		"feed"		: "http://shevisionsfilm.wordpress.com/feed/",
		"function"	: rss
	},
	
	"Facebook": {
		"feed"		: "http://www.facebook.com/feeds/page.php?format=rss20&id=116680831687515",
		"function"	: rss
	},
	
	"TigerLily" : {
		"feed"		: "http://tigerlilyscabaret.wordpress.com/feed/",
		"function"	: rss
	},
	

}

var totalTabs;
totalTabs=0;


function Load(dattab , tabid , conid , actab , descr){
//$('#feedWidget').show()
showTab(dattab ,  tabid , conid, actab , descr);
}

function showTab(dattab , key , conid , actab , descr)
{
	var obj = tabs[key];
	if(!obj) return false;
	
	var stage = $(conid);
	
	/* Forming the query: */
	var query = "select * from feed where url='"+obj.feed+"' LIMIT 3";
	
	/* Forming the URL to YQL: */
	var url = "http://query.yahooapis.com/v1/public/yql?q="+encodeURIComponent(query)+"&format=json&callback=?";
	
	$.getJSON(url,function(data){

		stage.empty();

		/* item exists in RSS and entry in ATOM feeds: */
		$.each(data.query.results.item || data.query.results.entry,function(){
			try{
				/* Trying to call the user provided function, "this" the rest of the feed data: */
				stage.append(obj['function'](this));
				
			}
			catch(e){
				/* Notifying users if there are any problems with their handler functions: */
				var f_name =obj['function'].toString().match(/function\s+(\w+)\(/i);
				if(f_name) f_name = f_name[1];
				
				stage.append('<div>There is a problem with your '+f_name+ ' function</div>');
				return false;
			}
		})
	});
	
	$(actab).text(key);
}



function twitter(item)
{
	/* Formats the tweets, by turning hashtags, mentions an URLS into proper hyperlinks: */
	return $('<div>').html(
			formatString('<span class="date">')+(item.pubDate)+('</span><br/>')+
			(item.description || item.title)+
			' <a href="'+(item.link || item.origLink)+'" target="_blank">[tweet]</a>'
	);
}


function rss(item)
{
	
	return $('<div>').html(
			formatString('<span class="date">')+(item.pubDate)+('</span><br/>')+
			('<a href="'+(item.origLink || item.link[0].href || item.link)+'" target="_blank">')+(item.title)+('</a>')+
			('<p>')+(item.description)+('</p>')
	);
}



function formatString(str)
{
	/* This function was taken from our Twitter Ticker tutorial - http://tutorialzine.com/2009/10/jquery-twitter-ticker/ */
	str = str.replace(/<[^>]+>/ig,'');
	str=' '+str;
	str = str.replace(/((ftp|https?):\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?)/gm,'<a href="$1" target="_blank">$1</a>');
	str = str.replace(/([^\w])\@([\w\-]+)/gm,'$1@<a href="http://twitter.com/$2" target="_blank">$2</a>');
	str = str.replace(/([^\w])\#([\w\-]+)/gm,'$1<a href="http://twitter.com/search?q=%23$2" target="_blank">#$2</a>');
	return str;
}
