// JavaScript Document

//RssDataItem
function RssDataItem() { 
	this.ParseXml = function(d) {
		var item = this;
		var xml = $(d);	
		this.title = xml.find('title').text();
		this.url = xml.find('link').text();
		this.description = xml.find('description').text();
		this.date = xml.find('pubDate').text();
	};
	return this; 
}
RssDataItem.prototype.title;
RssDataItem.prototype.url;
RssDataItem.prototype.description;
RssDataItem.prototype.date;

//RssData
function RssData() { 
	this.Items = new Array();
	this.count = 0;
	this.add = function(item) {
		var self = this;
		Items[self.count] = item;
		self.count += 1;
	}

	this.ParseXml = function(d, maxNum) {
		var self = this;
		var items = $($(d).find('item'));
		for(var i=0; i<maxNum; ++i) {
			var newItem = new RssDataItem();
			var xml = items[i];
			newItem.ParseXml(xml);
			self.add(newItem);
		}
	};
	return this;
}
RssData.prototype.Items;
RssData.prototype.count;


//jQuery Object extension method
$.fn.extend({
	RssFill: function(url, numItems, templateUrl, callback) {
		var div = $(this);
		
		div.empty();
	
		//use jQuery get to grab the url and put arg in function for parsing
		$.get('scripts/proxy.php?url=' + url, function(d) {
			//TODO: use jTemplate instead of the html
			var data = RssData();
			data.ParseXml(d, numItems);
			div.setTemplateURL(templateUrl);
			//div.setParam('count', numItems);
			div.processTemplate(data); 
			
			if(callback != 'undefined' && callback != null) {
				callback();	
			}
		
		});
	}
});

