var cbr250 = function() {
	var _self = {},
		_constants = {
			POSTS_CONTAINER:"#pageContent",
			POSTS_FEED_URL:"forum/syndication.php",
			POSTS_TITLE:"Recent Forum Posts",
			RESOURCES_LIST:"resources/manifest.xml",
			RESOURCES_TITLE:"Resources"
		};
	
	_self.loadPostsToPage = function() {
		_self.loadPostsToContainer(_constants.PAGE_CONTAINER);
	};
	
	_self.loadPostsToContainer = function(el) {
		el = $(el);
		_getFeed(_constants.POSTS_FEED_URL,
			function(data, textStatus, XMLHttpRequest) {
				$("<h3></h3>").text(_constants.POSTS_TITLE).appendTo(_constants.POSTS_CONTAINER);
				
				var sanitise = function(txt) {
					return txt.replace(/<img[^>]+>/ig,"[Click through to see images]");
				}				
				
				
				$(data).find("item").each(function(idx, el) {
					el = $(el);
					var link = $("<a></a>").attr("href",el.find("link").text()).text(el.find("title").text());
					$("<div class=\"forum-post\"></div>").append(link).append(" - " + sanitise(el.find("description").text())).appendTo(_constants.POSTS_CONTAINER);
				});
			});
	};
	
	var _handleXhrError = function(XMLHttpRequest, textStatus, errorThrown) {
		if (window.console && window.console.error) {
			console.error(errorThrown.message);
		}
	};
	
	var _getFeed = function(url, callback) {
		var options = {
			url:url,
			dataType:"xml",
			success:callback,
			error:_handleXhrError
		};
		
		$.ajax(options);
	};
	
	_self.initResources = function() {
		_getFeed(_constants.RESOURCES_LIST,
			function(data, textStatus, XMLHttpRequest) {
				$(_constants.POSTS_CONTAINER).html("<h3>"+_constants.RESOURCES_TITLE+"</h3>");
				$(data).find("resources>resource").each(function(idx,el) {
					el=$(el);
					$(_constants.POSTS_CONTAINER).append("<p class=\"resource\"><a target=\"_blank\" href=\"" + el.attr("src") + "\">" + el.attr("title") + "</a></p>");
				});
			});
	};
	
	return _self;
}();

$(document).ready(function() {
	cbr250.loadPostsToPage();
});