(function($) {

	$.extend(jQuery.fn,{
	
		feed : function(params){
		
			var $this = $(this);
			
			// Set the variables to be used within the class
			var $count = params.count || 1;
			var $vars = params.vars || ["title","description","link"];
			var $template = params.template || $this.html();
			var $xml = params.xml || null;
			
			$.replaceAll = function(needle, value, subject){
				var $subject = subject;
			
				while($subject.indexOf("@"+needle) != -1){
					$subject = $subject.replace("@"+needle, value);
				}
				
				return $subject;
			}
			
			// What to do after a request has been completed
			$this.complete = params.complete || function(html){
				$this.html(html);
			}
			
			// What to do while a request is in progress
			$this.loading = params.loading || function(){
				$this.html("<span class=\"loading\">Loading...</span>");
			}
		
			// Make a request
			$this.request = function(){
				
				$this.loading();
				
				var $v = [];
				var $t = [];
				var $tmp = "";
				
				// Grab values from the feed
				for(var i in $vars){
					$v[i] = $.xmlDOM($xml).find("rss > channel > item > "+$vars[i]);	
				}
				
				// Build the html
				for(i = 0; i < $count; i++){
					$t[i] = $template;
					
					for(var n in $v){
						$t[i] = $.replaceAll($vars[n], $($($v[n])[i]).text(), $t[i])
					}
					
					$tmp += $t[i];
					
				}
			
				$this.complete($tmp);	
								
			}
			
			$this.request();
			
		}		
		
	});

})(jQuery);
