var RSSPipe = new Class({

	// extensions & implementations
	Implements: [Events, Options],

	// instance properties
	container: null,
	feedUrl: '/rsspipe/',
	feedSites: null,
	// feedSites: $H({
	// 			'noimpactman.typepad.com': $H({ label:'No Impact Man', url:'http://noimpactman.typepad.com/' }),
	// 			'www.ecorazzi.com': $H({ label:'Ecorazzi.com', url:'http://www.ecorazzi.com/' }),
	// 			'www.ecogeek.org': $H({ label:'Ecogeek.org', url:'http://www.ecogeek.org/' }),
	// 			'www.treehugger.com': $H({ label:'Treehugger.com', url:'http://www.treehugger.com/' })
	// 			}),
	feedTokens: null,
	
	// constructor
	initialize: function (container) {
		this.container = container;
		this.loadFeed();
	},
	
	// methods
	loadFeed: function () {
		new Request.JSON({ url:this.feedUrl, onSuccess:this.onFeedLoaded.bind(this) }).send();
	},
	
	displayFeed: function (items) {
		
		// empty the container before adding items
		this.container.empty();
		
		// start adding the items
		items.each(function (item, index) {
			
			// extract the source
			var uri;
			var link;
			if (item['feedburner:origLink']) {
				link = item['feedburner:origLink'];
				uri = item['feedburner:origLink'].match(/https?:\/\/([^\/]+)/)[0].toURI().get('host');
			} else if (item['y:id']) {
				link = item['y:id'].value;
				uri = item['y:id'].value.match(/https?:\/\/([^\/]+)/)[0].toURI().get('host');
			 } else if (item['link']) {
				link = item['link'];
				uri = item['link'].match(/https?:\/\/([^\/]+)/)[0].toURI().get('host');
			}
						
			// create the container	
			var itemBlock = new Element('div', { 'class':'item' });
			itemBlock.inject(this.container);
			
			// create the linked title
			var itemTitle = new Element('a', { 'class':'article',
											   'html':item.title,
											   'href':link,
											   'target':'_blank' });
			itemTitle.inject(itemBlock);
			
			// create the source label
			var itemLabel = new Element('a', { 'class':'label',
											   'text':[this.tokens.from, this.feedSites.get(uri).label].join(' '),
											   'href':this.feedSites.get(uri).url });
			itemLabel.inject(itemBlock, 'top');
			
		}.bind(this));
		
	},
	
	// event callbacks
	onFeedLoaded: function (json, text) {
				
		// store the sites
		this.feedSites = $H(json.sites);
		
		// store the tokens
		this.tokens = json.tokens;
		
		// display the items
		this.displayFeed(json.value.items);
		
	}
	
});