﻿Date.prototype.setISO8601 = function(dString){

	var regexp = /(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))/;

	if (dString.toString().match(new RegExp(regexp))) {
		var d = dString.match(new RegExp(regexp));
		var offset = 0;

		this.setUTCDate(1);
		this.setUTCFullYear(parseInt(d[1],10));
		this.setUTCMonth(parseInt(d[3],10) - 1);
		this.setUTCDate(parseInt(d[5],10));
		this.setUTCHours(parseInt(d[7],10));
		this.setUTCMinutes(parseInt(d[9],10));
		this.setUTCSeconds(parseInt(d[11],10));
		if (d[12])
			this.setUTCMilliseconds(parseFloat(d[12]) * 1000);
		else
			this.setUTCMilliseconds(0);
		if (d[13] != 'Z') {
			offset = (d[15] * 60) + parseInt(d[17],10);
			offset *= ((d[14] == '-') ? -1 : 1);
			this.setTime(this.getTime() - offset * 60 * 1000);
		}
	}
	else {
		this.setTime(Date.parse(dString));
	}
	return this;
};

function generateLI(article, user)
{
	var articleDate = new Date().setISO8601(article.published_at)
	return "<li><a href='http://www.legalrss.co.uk/" + user.site_name + "/" + article.slug +
		"' title='" + article.summary + "'>" + 
		article.title + "</a> <span class='newssynd-date'>(" + 
		articleDate.getDate() + "/" + (articleDate.getMonth() + 1) + "/" + articleDate.getFullYear() + ")</span></li>";
}

/*
 * JavaScript newsSyndFeed
 * Copyright (c) 2008 GoTripod Ltd
 */

function newsSyndFeed(data)
{
	if (typeof(window['newssynd_headings']) == "undefined" || newssynd_headings != false) {
		newssynd_headings = true;
	}
	var json_data = eval(data);
	if (json_data.status == 0) return;
	var feed = json_data.feed;
	var categories = json_data.categories;
	var articles = json_data.articles;
	var user = json_data.user;
	var content = "";
	var noCatArticle = 0;
	var noArticle = 0;
	if (categories) {
		for (i = 0; i < categories.length; i++) {
			content +=	"<div class='newssynd-category'>"
			if (newssynd_headings)
				content += "<h2>" + categories[i].name + "</h2>";
			if(categories[i].articles.length < 5)
				noCatArticle = categories[i].articles.length;
			else
				noCatArticle = 5;
			content +=	"<ol class='newssynd-items'>";
			for (ii = 0; ii < noCatArticle; ii++) {
				content += generateLI(categories[i].articles[ii], user);
			}
			content += "</ol></div>"
		}
	}
	else if (articles) {
		content +=	"<ol class='newssynd-items'>";
		if(articles.length < 5)
			noArticle = articles.length;
		else
			noArticle = 5;	
		for (i = 0; i < noArticle; i++) {
			content += generateLI(articles[i], user);
		}
		content += "</ol>"
	}
	document.getElementById("newssynd-feed-" + feed).innerHTML = content;
}

