/*
Script: Comment.js v0.1
	Contains the Comment Class.
	27-8-2008 by sb, Floor SA

License:
	MIT-style license.

Copyright:
	copyright (c) 2007 Jerome Vial, <http://www.floor.ch>	 
*/

var Comment = new Class({
	Implements: [Events, Options],
	
	// class options
	options: {
		url 	: '/cgi/app',
		form	: 'comment'
	},
	
	// constructor
	initialize: function(options){
		this.setOptions(options);
		
		this.build();
	},
	
	// inits the button to post a comment
 	build: function (){
		//we add a formcheck
		$$('.addcomment').each(function(el) {
			// new formcheck
			new FormCheck(this.options.form, {
				submitByAjax : true,
				//ajaxResponseDiv :
				onAjaxRequest : function() {
		
				
					$('comments_submit').setProperty('value', 'Please wait');
					$('comments_submit').setProperty('disabled', 'disabled');
					
				}.bind(this),
				onAjaxSuccess : function(response){
					//inject the comment
					var comment = new Element('div', {'class' : 'comment', html : $('comments_content').getProperty('value')});
					var div = new Element('div', {'html' : ' says :'}).inject(comment, 'top');
					new Element('div', {'class' : 'date', 'html' : 'now'}).inject(comment, 'top');
					new Element('span', {'class' : 'nick', 'html' : $('comments_nickname').getProperty('value')}).inject(div, 'top');
					comment.inject(this.options.form, 'before');
					
					
					var fieldset = $(this.options.form).getElement('fieldset');
					var legend = fieldset.getFirst().clone().set('html', 'Comment added');
					fieldset.empty();
					legend.inject(fieldset);
					new Element('p', {html : 'Thank you for your comment'}).inject(fieldset);
					
				}.bind(this),
				onAjaxFailure : function() {
					alert('An error occured\nPlease try again later');
					$('comments_submit').setProperty('value', 'Add comment');
					$('comments_submit').setProperty('disabled', 'disabled');
				}
			});
			// set url
			$('comments_content').addEvent('focus', function(){
				$(this.options.form).setProperty('action', this.options.url);
			}.bind(this));
		},this);
	}
});
