(function($){

	$.fn.displayFeed = function(url, limit, options) {

		return this.each(function(i, target) {

			var _timer		= false;
			var _proxy_url	= 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=';
			var _limit		= limit || 5;
			var _templates 	= {
			
				wrapper : function(contents) {
					return contents;
				}
				
				, post : function(post) {
					
					var link 			= post.link;
					var title 			= post.title;
					var author			= post.author || '';
					var contentSnippet	= post.contentSnippet || '';
					var date			= post.publishedDate || '';
					var tracking		= '';
					
					if (options && options.link_prepend) {
						link 	= (options.link_prepend+link);
					}
					if (options && options.title_strip) {
						title 	= title.replace(options.title_strip, '');
					}
					
					if (options && options.tracking) { 
						tracking = 'rel="' + options.tracking + '"';
					}
					
					
					var data = '<div class="article">'
							 + '<img width="147" height="97" alt="green-explorer-article-img" src="img/green-explorer-article-img.jpg">'
							 + '<h6><a href="'+link+'" target="_blank" '+tracking+'>'+title+'</a></h6>'
							 + '<p class="subhead">Article By '+author+' &nbsp; '+date.substring(0, 16)+'</p>'
							 + '<p>'+contentSnippet+' <br/><a class="readMore" href="'+link+'" target="_blank" '+tracking+'><span style="position: relative; top: -1px;">»</span>Read More</a></p>'
							 + '</div>';

					return data;
	
				}	
	
				, error : function(verbose) {
						
					var msg 	= (options && options.error)  ? options.error : ('Apologies, but this feed is currently unavailable.<br/>Please check back soon.');
					var data 	= '<div class="item_error">' + msg	+ '</div>';

					return data;
	
				}	
			
			};
	
			var _init = function() {

					var feed_data;
					
					// Make sure user specified a url to load
					if (!url) {
						_updateElem(_templates.error('No feed url provided.'));
						return false;
					}

					// Create Google feed proxy url
					var load_url = _proxy_url + escape(url);
					
					// Set timeout to check for error incase getJSON fails after 10 seconds
					_timer = setTimeout(function(){
						_updateElem(_templates.error());
						}, 10000);
					
					// Load remote data and handle error
					$.getJSON(load_url, function(data){

						if (data.responseStatus == 200) {
							// Success
							_updateFeed(data.responseData.feed);
						} else {
							// Fail
							_updateElem(_templates.error('Feed proxy error.'));
							return false;
						};
					});				
			
			};			


			// Update entries
			var _updateFeed	= function (feed) {
			
					var data = '';
					
					// Cancel error timer, it worked!
					clearTimeout(_timer);

					$(feed.entries).each(function(i,p) {
						if (i >= _limit) return false;
						data += _templates.post(p);
					});

					_updateElem(data);
			
			};
			
			var _updateElem	= function (data) {
					$(target).html(_templates.wrapper(data));
			}
			
			// Call constructor
			return _init();
			
		});

	};

})(window.jQuery);

