var NODE_TEMPLATE	= '';

google.load("feeds", "1");
google.setOnLoadCallback(initialize);

function initialize() {
	loadFeed('http://forum.world-dating-partners.com/index.php?act=rssout&id=81', '#ulFeatures', {count:3, template:'<li><a href="{1}"><em>{0}</em> <span>{2}</span></a></li>'});
	
	if($("#ulAnnouncements").length > 0) {
		$("#ulAnnouncements").append('<ul></ul>');
		loadFeed('http://www.worlddatingpartners.com/news/feed/', '#ulAnnouncements ul', {count:5, template:'<li>{3}&nbsp;<a href="{1}" target="_blank">&gt;&gt;</a></li>'}, function(placeholder) {
			$("#ulAnnouncements ul").newsTicker();
		});
	}
}

function loadFeed(url, placeholder, options, callback) {
	options	= options || {}
	options.template	= options.template	|| NODE_TEMPLATE;
	options.count		= options.count		|| 5;
	
	var feed = new google.feeds.Feed(url);
	feed.setNumEntries(options.count);
	feed.load(function(result) {
		if(!result.error) {
			//$(placeholder).html('');
			
			for (var i = 0, length = result.feed.entries.length; i < length; i++) {
				var entry = result.feed.entries[i];
				
				$(placeholder).append(String.format(options.template, 
					entry.title,
					entry.link,
					entry.publishedDate,
					entry.contentSnippet
				));
			}
			
			if(typeof callback == 'function') {
				callback(placeholder);
			}
		}
	});
}

String.format = function(tmpl) {
	for(var i=1; i<arguments.length; i++) {
		re = new RegExp('\\\{' + (i-1) + '\\\}', 'gi');
		tmpl = tmpl.replace(re, arguments[i]);
	}
	return tmpl;
}
