/*
 * 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
 *
 * Further hacked by Charlie at UNT
 */
(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
 *	});
 * @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() {
		
	$(this).each(function(){
			var container = this;
			var options = {};
			// get options from tag attributes
			
			
			var defaults = {
				items: 3,
				descLength: 15
			};


			options.targeturl = $(this).attr('url');
			options.items = $(this).attr('items');
			options.descLength = $(this).attr('descLength');
					
			var opts = $.extend(defaults, options);
			
			if(!opts.targeturl)	return false;
			
			// filter url through the proxy to convert xml to json		
			opts.targeturl = 'http://web3.unt.edu/urcm/cstools/proxy.php?filter=rss2json&url=' + opts.targeturl;		
			
			$.ajax({
				url:opts.targeturl,
				dataType:'jsonp',
				success:function (json){
					var posts=json.items;
					//console.log(posts);
					writeposts(container,posts,opts.items);						
				}

			});
	});	
	
};

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

function writeposts(container,posts,max){
	$(container).empty().removeClass('loading');
	var html = '';
	for(var k in posts){
		if (k < max)
		html+=format(posts[k]);
	}
	$(container).append(html);
}

function format(post){
	if (!post.description)
	{
		post.description = post.summary;
	}
	if (!post.pubdate)
	{
		post.pubdate=post.published;
	}
	var date_string = format_date(post.pubdate);
	//console.log(date_string);
	
	var html='<li class="post"><span class="post_title"><a href="'+post.link+'">'+post.title+'</a></span><br />';
	html+='<span class="post_date">'+date_string+'</span>';
	html+='<br /><br />'+post.description+'</li>';
	return html;	
}

//js dates are the worst
function format_date(date)
{
	//console.log(date);
	var daysofweek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
	var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
	var date_obj = new Date(date);

	// ISO 8601 Dates won't parse correctly, here's a fix.
	if (!date_obj.getDay())
	{
		date = date.replace(/\D/g, " ");
		var tmp_a = date.split(" ");
		date_obj = new Date(tmp_a[0], (tmp_a[1]-1), tmp_a[2], tmp_a[3], tmp_a[4], tmp_a[5]);
	}
	
	var hour = date_obj.getHours();
	//console.log(hour);
	var meridian = 'a.m.';
	if (hour > 11) meridian = 'p.m.';
	hour = hour % 12;
	if (hour == 0) hour = 12;
	
	var date_string = daysofweek[date_obj.getDay()]+', '+months[date_obj.getMonth()]+' '+date_obj.getDate()+', '+date_obj.getFullYear()+' ';
	date_string += hour+':'+date_obj.getMinutes()+' '+meridian;
	
	//console.log(date_string);
	return date_string;
}

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



})(jQuery);