(function($){

	var current = null; 
	
	$.fn.rssfeed = function(url, options) {	
	
		// Set pluign defaults
		var defaults = {
			limit: 10,
			date: true,
			content: false,
			snippet: false,
			showerror: false,
			errormsg: '',
			key: null
		};  
		var options = $.extend(defaults, options); 
		
		// Functions
		return this.each(function(i, e) {
			var $e = $(e);
			
			// Add feed class to user div
			if (!$e.hasClass('rssFeed')) $e.addClass('rssFeed');
			
			// Check for valid url
			if(url == null) return false;

			// Create Google Feed API address
			var api = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=" + url;
			if (options.limit != null) api += "&num=" + options.limit;
			if (options.key != null) api += "&key=" + options.key;
			// Send request
			$.getJSON(api, function(data){
				
				// Check for error
				if (data.responseStatus == 200) {
	
					// Process the feeds
					_callback(e, data.responseData.feed, options);
				}
			});				
		});
	};
	
	// Callback function to create HTML result
	var _callback = function(e, feeds, options) {
		if (!feeds) {
			return false;
		}
		var html = '';	

		// Add body
		html += '<div class="rssBody">\n' +
			'<ul>\n';
		
		// Add feeds
		for (var i=0; i<feeds.entries.length; i++) {

			// Get individual feed
			var entry = feeds.entries[i];
			
			// Format published date
			var entryDate = new Date(entry.publishedDate);
			var pubDate = entryDate.toLocaleDateString();

			// Add feed row
			html += '<li>\n' +
			'<img src="images/Twitter_small.png" style="float:left; margin-right:10px; margin-bottom:65px;"><a href="'+ entry.link +'" title="View this feed at '+ feeds.title +'"><span style="color:#0a457b;">'+ pubDate +' : </span>'+ entry.title.replace('gdukrecruitment: ', '').replace('RT @gduknews: ','').replace('â','').replace('€','').replace('¦','') +'</a>\n'+
			'</li>\n';
		}
		html += '</ul>\n' +
		'</div>'
		$(e).html(html);
	};
})(jQuery);

