

/**
 * @author Activitea
 */
var FormManager = Class.create({
	
	form: null,
	options: null,
	noticeTimeoutID: null,
	notice: null,
	
	initialize: function(form, options) {
		this.form = $(form);

		this.options = Object.extend({
			noticeID: 'notice'
		}, options || {});
		
		this.notice = new Element('div', {'class': 'notice'});
		this.notice.hide();
		form.insert({top: this.notice});
		
		this.addBehaviors();
	},
	
	send: function() {
		var inputs, errors = [], curForm = this.form;
		
		// Form validation:
		inputs = this.form.getElements();
		$A(inputs).each(function(input) {
			if (input.getAttribute('name').substr(-2,2) == '_1') {
				var originalValue = input.tagName.toLowerCase() == 'textarea' ? input.innerHTML : input.getAttribute('value');
				if (input.getValue() == originalValue) {
					var label = curForm.select('label[for="'+input.id+'"]')[0];
					errors.push("Vous devez remplir le champ '"+label.innerHTML+"'");
				}
			}
		});
		
		if (errors.length) {
			this.notify(errors.join('<br />'));
			return false;
		}
		
		this.form.request({
			onComplete: function(transport, json) {
				var msg = ((json && json.message) ? json.message : transport.responseText);
				var success = ((json && json.success) ? json.success : false);
				
				this.notify(msg);
				
				if (success)
					this.form.reset();
				
				this.form.enable();
			}.bind(this)
		});
		this.form.disable();
	},
	
	notify: function(content) {
		if (!this.notice) return false;
		
		clearTimeout(this.noticeTimeoutID);
		this.notice.update(content);
		
		if (this.notice.visible())
			new Effect.Pulsate(this.notice, {duration:1.5, pulses:3, queue:'end'});
		else
			new Effect.Appear(this.notice);
		
		this.noticeTimeoutID = setTimeout(function() {
			new Effect.Fade(this.notice);
		}.bind(this), 20*1000);
	},
	
	addBehaviors: function() {
		// Form behavior:
		this.form.observe('submit', function() {
			this.send();
		}.bindAsEventListener(this));
		this.form.onsubmit = function() {return false;};
		
		// Input behaviors:
		var inputs = this.form.getElements();
		$A(inputs).each(function(input) {
			if (input.tagName.toLowerCase() == 'textarea' || (input.tagName.toLowerCase() == 'input' && input.readAttribute('type') == 'text')) {
				var originalValue;
				switch (input.tagName.toLowerCase()) {
					case 'textarea':
						originalValue = input.innerHTML;
						break;
					
					default:
						originalValue = input.getAttribute('value');
						break;
				}
				
				input.observe('focus', function() {
					if (input.getValue() == originalValue)
						input.setValue('');
					
					$(this).addClassName('focus');
				});
				
				input.observe('blur', function() {
					if (input.getValue() == '')
						input.setValue(originalValue);
					
					$(this).removeClassName('focus'); 
				});
			}
		});
	}
});

// Static vars:
Object.extend(FormManager, {
	instances: []
});

document.observe('dom:loaded', function() {
	$$('form.content-form').each(function(form) {
		FormManager.instances.push(new FormManager(form));
	});
});
