google.load('feeds', '1');

RSS_DEFAULT_TEMPLATE = {
	first: '',

	item: [
		'<div class="content-row">',
			'<div class="content-date">{date}</div>',
			'<a class="content-link" target="_blank" href="{link}">{title}</a>',
			'<div class="content-snippet">{snippet}</div>',
		'</div>'
	].join(''),

	last: [
		'<div class="powered-by-google">',
			'powered by ',
			'<a target="_blank" href="http://news.google.com">',
				'<img src="http://www.google.com/uds/css/small-logo.png" />',
			'</a>',
		'</div>'
	].join('')
};

function InsertRssHere(id, url, limit, date_format, tpl)
{
	function add_zero(num)
	{
		return ((num < 10) ? ('0' + String(num)) : num);
	}

	function on_feed_loaded(res)
	{
		if (res.error)
		{
			document.getElementById(id).innerHTML = 'No headlines found';
		}
		else
		{
			var html = tpl.first;

			for (var i = 0; i < res.feed.entries.length; i++)
			{
				var item = res.feed.entries[i];

				var dt = new Date();
				dt.setTime(Date.parse(item.publishedDate));

				var dt_str = date_format;

				dt_str = dt_str.replace(/\{full\}/g, dt.toLocaleString().replace(/(\d\d:\d\d:\d\d).*$/, '$1'));
				dt_str = dt_str.replace(/\{hour_ampm\}/g, add_zero((dt.getHours() % 12) == 0 ? 12 : (dt.getHours() % 12)));
				dt_str = dt_str.replace(/\{min\}/g, add_zero(dt.getMinutes()));
				dt_str = dt_str.replace(/\{ampm\}/g, (dt.getHours() < 12 ? 'AM' : 'PM'));
				dt_str = dt_str.replace(/\{mon\}/g, add_zero(dt.getMonth() + 1));
				dt_str = dt_str.replace(/\{day\}/g, add_zero(dt.getDate()));
				dt_str = dt_str.replace(/\{short_year\}/g, add_zero(dt.getFullYear() % 100));

				var mt, snip;

				if (mt = item.content.match(/<div\s+style="width:80%">\s*([^<]+)<\/div>/))
				{
					snip = mt[1].replace(/\s\s*$/, '');
				}
				else if (mt = item.content.match(/<font\s+size="-1">([^<]+)<b>\.\.\.<\/b>\s*<\/font>/))
				{
					snip = mt[1] + '...';
					if (snip.indexOf('--') >= 0) { snip = snip.substr(snip.indexOf('--') + 2).replace(/^\s\s*/, ''); }
				}
				else
				{
					snip = item.contentSnippet;
				}

				if (snip.substr(snip.length-3, 3) == '...') {
					snip = snip.substr(0, snip.length-3) + '<a target="_blank" href="' + item.link + '">...</a>';
				}

				var item_str = tpl.item;

				item_str = item_str.replace(/\{link\}/g, item.link);
				item_str = item_str.replace(/\{title\}/g, item.title);
				item_str = item_str.replace(/\{date\}/g, dt_str);
				item_str = item_str.replace(/\{snippet\}/g, snip);

				html += item_str;
			}

			html += tpl.last;

			document.getElementById(id).className = '';
			document.getElementById(id).innerHTML = html;
		}

		if (typeof(fix_anchor_pos) == 'function') {
			fix_anchor_pos();
		}
	}

	if (typeof(date_format)=='undefined' || date_format==null) {
		date_format = '{full}';
	}

	if (typeof(tpl)=='undefined' || tpl==null) {
		tpl = RSS_DEFAULT_TEMPLATE;
	}

	var feed = new google.feeds.Feed(url);
	feed.setNumEntries(limit);

	document.write('<div id="'+id+'" class="widget_frame_loading">Loading...</div>');
	feed.load(on_feed_loaded);
}
