var FeedMe = new Class({
	Implements : [Options, Events],
	options : {
		template : '<div class="item">#link##title#<div class="item-desc">#desc#</div></div>',
		container : 'FeedMeContainer'
	},
	initialize : function(options) {
		this.setOptions(options)
	},
	eat : function(url) {
		this.fireEvent('start');
		new Request({
			url : url,
			onSuccess : this._eat.bind(this),
			onFailure : function(response) {this.fireEvent('failure', response)}.bind(this),
			onException : function(headerName, error) {this.fireEvent('exception', [headerName, error])}.bind(this)
		}).send();
	},
	_eat : function(responseText, responseXml) {
		this.fireEvent('success', responseXml);
		//console.log(responseText+' '+responseXml)
		//try {
			this._digest(responseXml);
		
	},
	_digest : function(responseXml) {
		this.fireEvent('processing');
		this.channel = new FeedMe.Channel(responseXml);
		this.outputContainer = $(this.options.container);
		["title","link","description","pubDate","copyright"].each(function(item) {
			//console.log('chan-'+item);
			this.outputContainer.getElement('.chan-'+item).set('html',this.channel.get(item));
		}.bind(this));
		var img = '', image = this.channel.image, src = image.get('src');
		if (src != null) {
			var title = image.get('description');
			img = '<img src="'+src+'" alt="'+title+'" title="'+title+'" />';
		}
		this.outputContainer.getElement('.chan-image-link').set('html', img);
		
		this.itemsHtml = '';
		this.channel.items.each(function(item) {
			this.itemsHtml += this.options.template.replace(
			'#link#', '<div class="item-link">'+item.get('link')+'</div>').replace(
			'#title#', '<div class="item-title">'+item.get('title')+'</div>').replace(
			'#desc#', '<div class="item-desc">'+item.get('description')+'</div>')
		}.bind(this));
		this.outputContainer.getElement('.chan-items').set('html', this.itemsHtml);
		this.fireEvent('done');
	}
});

FeedMe.Node = new Class({
	initialize : function(element) {
		this.element = element;	
	},
	get : function(prop) {
		if (this.element) {
			if (prop != 'value') return this.element.get(prop);
			else this.element.childNodes[0].nodeValue;
		}
		return null;
	}
});
FeedMe.BaseItem = new Class({
	get : function(prop) {
		var elm = this.element.getElement(prop);
		if (elm != null) return elm.childNodes[0].nodeValue;
		return '';
	}
});
FeedMe.Item = new Class({
	Extends : FeedMe.BaseItem,
	initialize : function(element) {
		this.element = element;
		this.category = new FeedMe.Node(this.element.getElement('category'));
		this.enclosure = new FeedMe.Node(this.element.getElement('enclosure'));
		this.guid = new FeedMe.Node(this.element.getElement('guid'));
		this.source = new FeedMe.Node(this.element.getElement('source'));
	}
	
});
FeedMe.Channel = new Class({
	Extends : FeedMe.BaseItem,
	initialize : function(xmlDoc) {
		this.xmlDoc = xmlDoc;
		this.element = this.xmlDoc.getElement('channel');
		this.items = new Array();
		this.xmlDoc.getElements('item').each(function(item) {
			this.items.push(new FeedMe.Item(item));
		}, this);
		this.category = new FeedMe.Node(this.element.getElement('category'));
		this.image = new FeedMe.Node(this.element.getElement('image'));
	}
});