/*
 * A simple JQuery plugin for an RSS-fed news box
 *
 * @author Francesco Vivoli <f.vivoli@gmail.com> - http://atalayasec.org
 * Based on code found on the JQuery mailing list
 */
(function($) {
	
/**
 * Configure the news box container with url, maximum number of posts
 * to be fetched and their text length.
 * @example $('#newsbox').feedreader({
 *		targeturl: 'http://blogs.atalayasec.org/atalaya/?feed=rss2',
 *		items: 3,
 *		descLength: 15,
 *		showDate: true
 *	});
 * @desc fill the #newsbox element with at most 3 posts taken from the above url, and showig
 * a teaser of at most 15 words.
 *	
 */	
$.fn.feedreader = function(options) {
	var defaults = {
		targeturl: 'http://blogs.atalayasec.org/atalaya/?feed=rss2',
		items: 3,
		descLength: 15,
		showDate: true
	}
	if(!options.targeturl)	return false;
	var opts = $.extend(defaults, options);
	$(this).each(function(){
			var container = this;
			$.get(opts.targeturl,function(xml){
					var posts=[];
					var i=0;
					$("item", xml).each(function(){
									if(i>opts.items-1)	return;
									var post={};
									$(this).find("link").each(function(){
										post.link=getNodeText(this);
									});
									$(this).find("title").each(function(){
										post.title=getNodeText(this);
									});
									if(opts.showDate == true) {
										$(this).find("pubDate").each(function(){
											var d=getNodeText(this);
											post.date=formatDate(d);
										});
									}
									$(this).find("description").each(function(){
										var t=getNodeText(this);
										post.desc=trimtext(t,opts.descLength);
									});
									posts[i++]=post;
					 });
					writeposts(container,posts);					
				
			})
	});	
	
};

$.fn.jsonreader = function(options) {
	var defaults = {
		targeturl: 'http://blogs.atalayasec.org/atalaya/?feed=rss2',
		items: 3,
		descLength: 15,
		showDate: true
	}
	if(!options.targeturl)	return false;
	var opts = $.extend(defaults, options);
	$(this).each(function(){
		var container = this;
		$.getJSON(opts.targeturl,function(json){
			var posts=[];
			var i=0;
			
		  $.each(json.entries, function(index, item){
				if(i>opts.items-1)	return;
				var post={};
				post.link=item.link;
				post.title=item.title;
				if(opts.showDate == true) {
					post.date=formatDate(item.pubDate);
				}
				post.desc=trimtext(item.description,opts.descLength);
				posts[i++]=post;
			});
			writeposts(container,posts);
		})
	});
};

function formatDate(date){
	var d = date.split(' ');
	d = d.slice(0,4);
	return d[0]+' '+d[2]+' '+d[1]+' '+d[3];
}

function trimtext(text,length){
	if(length<=0){return ''};
	
	var t = text.replace(/\s/g,' ');
	var words = t.split(' ');

	if(words.length<=length){
		return text;
	}
	
	var ret = words.slice(0,(length+1));
	return ret.join(' ')+'. . . . ';
}

function writeposts(container,posts){
	//$(container).empty();
	var html = '<dl>';
	for(var k in posts){
		html+=format(posts[k])
	}
	html += '</dl>';
	$(container).append(html);
}

function format(post){
	var html='<dt><a href="'+post.link+'" class="post-title">'+post.title+'</a>';
	if(post.date!=undefined){
		html+='<br /><span class="date">'+post.date+'</span></dt>';
	}
	html+='<dd class="post-desc">'+post.desc+'<div class="more"><a href="'+post.link+'"><span>More</span>&nbsp;&#187;</a></div></dd>'
	return html;	
}

function getNodeText(node)
{
        var text = "";
        if(node.text) text = node.text;
        if(node.firstChild) text = node.firstChild.nodeValue;
        return text;
}

})(jQuery);
