
var BlogEntry = new Class({
	initialize: function(manager, xml) {
		this.manager = manager;

		this.title = xml.find('title').text();
		this.content = xml.find('summary').text();
		this.link = xml.find('link').attr('href');
		this.date = xml.find('published').text().substr(0,10);

		this.createDOMElements();
	},

	createDOMElements: function() {
		this.dom = {}
		this.dom.listEntry = new Element('li');
		var link = new Element('a', {href: this.link});
		var shortTitle = (this.title.length > 35)? (this.title.substr(0,35)+'...') : this.title;
		link.grab(new Element('h4', {text: shortTitle}));
		link.grab(new Element('p', {'class': 'date', text: this.date}));
		link.grab(new Element('div', {'class': 'arrow'}));
		link.addEvent('click', function(event) {
			event.stop();
			this.manager.showEntry(this);
		}.bind(this));
		link.addEvent('mouseenter', function() { link.addClass('hover') });
		link.addEvent('mouseleave', function() { link.removeClass('hover') });
		this.dom.listEntry.grab(link);

		this.dom.post = new Element('div', {'class': 'post'});
		this.dom.post.grab(new Element('h4').grab(new Element('a', {href: this.link, text: this.title})));
		this.dom.post.grab(new Element('div', {'class': 'content', html: this.cleanPost(this.content)}));
		var info = new Element('div', {'class': 'info', html: '<div class="date"></div>'});
		info.getElement('div.date').set('text', 'Posted on ' + this.date);
		this.dom.post.grab(info);
	},

	cleanPost: function(html) {
		var result = html;
		result = result.replace(/<\/?[^>]+(>|$)/g, " ");
		if (result.length > 1000)
			result = result.substr(0,1000);
		result += ' <a href="'+this.link+'">Read more...</a>';
		return result;
	}
});

var BlogManager = new Class({
	ClassName: 'BlogManager',
	Extends: Base,
	initialize: function(container) {
		if (!$defined(container)) return;
		else if ($type(container) == 'element')	this.container = container;
		else if ($type(container) == 'array')	this.container = container[0];
		else return;

		if (!$defined(this.container)) return;

		this.entryList     = this.container.getElement('div#entries ul').empty();
		this.postContainer = this.container.getElement('div#preview');

		this.blog = {
			feed: this.container.getElement('div.feed a').get('href'),
			entries: []
		}

		this.request = new Request({
			method: 'get',
			url: this.blog.feed,
			onSuccess: function(text, responseXML) {
				this.parseFeed(responseXML);
			}.bind(this),
			onFailure: function() {
				console.warn('Blog feed could not be loaded.');
			}
		});
		this.request.send();
	},

	parseFeed: function(xml) {
		if (!$defined(xml)) {
			console.warn('Blog XML is empty.')
			return;
		}
		this.blog.entries = [];
		jQuery(xml).find('entry').each(function(index, entry) {
			if (this.blog.entries.length < 8)
				this.blog.entries.push(new BlogEntry(this, jQuery(entry)));
		}.bind(this));

		if (this.blog.entries.length) {
			$$('h2.blog')[0].show();
			this.populateEntryList();
		}
	},

	populateEntryList: function() {
		this.entryList.empty();
		this.blog.entries.each(function(entry, index) {
			this.entryList.grab(entry.dom.listEntry);
		}.bind(this));
		this.entryList.getChildren().getLast().addClass('last');

		this.showEntry(this.blog.entries[0]);
	},

	showEntry: function(entry) {
		this.entryList.getChildren().removeClass('selected');
		entry.dom.listEntry.addClass('selected');
		this.postContainer.getElements('div.post').dispose();
		this.postContainer.grab(entry.dom.post);
	}
});

new BlogManager($$('div.blog'));
