var bimboo_CommentFormSend = new Class({
	Implements: [Options],
	options: {
		method: 'post'
	},
	container: null,
	initialize: function(container,options) {
		options = $merge(this.options, options);
		this.setOptions(options);
		this.container = $(container);
		this.container.addEvent('submit', this.onSubmit.bind(this), this);
		this.container.set('send', { method: this.options.post });
		this.container.set('send', {
			onSuccess: function(response){
				this.container.getElements('.waiter_background').dispose();
				this.container.getElements('.waiter_foreground').dispose();
				var object = JSON.decode(response);
				this.container.getElements('.message_box').show();
				this.container.getElements('.message_box').set('html',object.message);
				this.container.getElements('.message_box').highlight('#FEBC9C');
				if (object.error) {
					if (object.field_with_error) {
						var input_name = "input[name=" + object.field_with_error + "]";
						var input_field = this.container.getElements(input_name)[0];
						if (input_field) {
							input_field.focus();
							input_field.select();
						}
					}
				} else {
					var input_name = "input[name=nome]";
					var input_field = this.container.getElements(input_name)[0];
					if (input_field) {
						input_field.value = '';
					}
					var input_name = "input[name=email]";
					var input_field = this.container.getElements(input_name)[0];
					if (input_field) {
						input_field.value = '';
					}
				}
			}.bind(this),
			onRequest: function() {
				// Any function to call on send
			}.bind(this)
		});
	},
	onSubmit: function(event) {
		event.preventDefault();

		this.container.setStyle('position','relative');

		var waiter_background = new Element('div',{
			'styles': {
				'background':'#FFF',
				'position':'absolute',
				'width':this.container.getWidth(),
				'height':this.container.getHeight(),
				'top':'0px',
				'left':'0px'
			},
			'class':'waiter_background',
			'html':'&nbsp;'
		});
		waiter_background.setOpacity(0.5);
		var waiter_foreground = new Element('div',{
			'styles': {
				'position':'absolute',
				'width':64,
				'height':64,
				'top':((this.container.getHeight()/2)-23) + 'px',
				'left':((this.container.getWidth()/2)-32) + 'px'
			},
			'class':'waiter_foreground',
			'html':'<img src="/components/bimboo/images/loader-big.gif" width="64" height="47" alt="" />'
		});
		this.container.grab(waiter_background);
		this.container.grab(waiter_foreground);

		var jsonRequest = new Request.JSON({url: '/components/form_filters/verify.php',
			onComplete: function(obj,text)
			{
				this.container.token.value = obj.token;
				if (obj.token != 0) {
					this.container.send();
				}
				return;
			}.bind(this)}).POST(
			{
				"formid": this.container.formid.value,
				"token": this.container.token.value
			}
		);
	}

});
