var Comments = new Class({
	Implements: Options,
	options: {
	},
	initialize: function(form, list, options){
		this.setOptions(options);
				
		this.form = $(form);
		this.list = $(list);
		this.alert = this.form.getElement('.alert');
		
		this.form.get('send').addEvent('success', this.onSuccess.bind(this));
		this.form.addEvent('submit', function(e){
			e.stop();
			this.send();
		}.bind(this));
	},
	
	send: function(rsp){
		this.alert.setStyle('display', 'block').addClass('neutral').set('html', '<em>Sending...</em>');
		this.form.send();
	},
	
	onSuccess: function(rsp){
		rsp = JSON.decode(rsp);
		if(rsp.status == "ok"){
			this.insertComment(rsp.comment);
			this.alert.set('class', 'alert positive').set('html', 'Your comment has been added!');
			this.form.reset();
		}else{
			this.alert.set('class', 'alert error').set('html', rsp.msg);
		}
	},
	
	insertComment: function(comment){
		var li = new Element('li').inject(this.list, 'top');
		new Element('span', {'class': 'meta', 'html': '<span class="author">'+comment.author+'</span> <span class="date">'+comment.date_added+'</span>'}).inject(li);
		new Element('p', {'text': comment.comment}).inject(li);
	}
});