var Contact = new Class({
	Implements: [Events, Options],
	
	initialize: function(){
		if (typeOf($$('div.sidebar_contact')[0]) == 'element') {
			this.divContact = $$('div.sidebar_contact')[0]; 
			this.form = this.divContact.getElement('div.form_contact');
			this.formFX = new Fx.Morph(this.form, {duration:500, link: 'cancel', transition: Fx.Transitions.linear.easeOut});
			divSBNext = this.divContact.getParent();
			this.divSBNextFX = new Fx.Morph(divSBNext, {duration:500, link: 'cancel'});
			this.divResponse = this.form.getElement('div.response');
			
			this.divContact.getElements('a.toCatch, a.close').addEvent('click', function(e){
				e.stop();
				this.toggle();
			}.bind(this));
			
			this.inputs();
			this.formAJAX();
			
			this.form.setStyles({'height' : 0}).addClass('closed');
			divSBNext.setStyles({'padding-top' : 160});
		}
	},
	
	toggle: function(){
		if (this.form.hasClass('closed')){
			this.formFX.start({'height' : 405});
			this.divSBNextFX.start({'padding-top' : 530});
			this.form.removeClass('closed');
		} else {
			this.formFX.start({'height' : 0});
			this.divSBNextFX.start({'padding-top' : 160});
			this.form.addClass('closed');
		}
	},
	
	inputs: function(){
		this.form.getElements('input.text, textarea').each(function(element){
			element.store('value', element.get('value'));
			element.addEvents({
				focus: function(){
					if (this.get('value') == this.retrieve('value'))
						this.set('value', '');
					this.getParent().getElement('span.error_notif').setStyles({'display' : 'none'});
				},
				
				blur: function(){
					if (this.get('value') == '')
						this.set('value', this.retrieve('value'));
				}
			});
		});
	},
	
	formAJAX: function(){
		this.formContact = this.form.getElement('form');
		this.formContact.addEvent('submit', function(e){
			e.stop();
			var request = new Request.JSON({
				data: {
					'reload_contact' : 'true', 
					'render' : 'json', 
					'message_author_name' : this.formContact.getElement('input[name=message_author_name]').get('value'),
					'message_author_email' : this.formContact.getElement('input[name=message_author_email]').get('value'),
					'message_content' : this.formContact.getElement('textarea').get('value')
				},
				onSuccess: function(xhr){
					var wrapper = this.divResponse.getChildren()[0];
					wrapper.empty();
					
					if (xhr.state == 'success'){
						var title = new Element('span', {
							'class' : 'title',
							'html' : slsBuild.langs.GENERIC_CONTACT_SUCCESS_TITLE
						}).inject(wrapper);
						
						var description = new Element('span', {
							'class' : 'success',
							'html' : xhr.message
						}).inject(wrapper);
					} else {
						var title = new Element('span', {
							'class' : 'title',
							'html' : slsBuild.langs.GENERIC_CONTACT_ERROR_TITLE
						}).inject(wrapper);
						
						var description = new Element('ul', {
							'class' : 'error'
						}).inject(wrapper);
						
						if (xhr.errors.message_author_name){
							new Element('li', {
								'html' : xhr.errors.message_author_name
							}).inject(description);
							$$('input[name=message_author_name]')[0].getParent().getElement('span.error_notif').setStyles({'display' : 'block'});
						}
						
						if (xhr.errors.message_author_email){
							new Element('li', {
								'html' : xhr.errors.message_author_email
							}).inject(description);
							$$('input[name=message_author_email]')[0].getParent().getElement('span.error_notif').setStyles({'display' : 'block'});
						}
						
						if (xhr.errors.message_content){
							new Element('li', {
								'html' : xhr.errors.message_content
							}).inject(description);
							$$('textarea[name=message_content]')[0].getParent().getElement('span.error_notif').setStyles({'display' : 'block'});
						}
					}
					
					this.divResponse.morph({'height' : 100, 'padding-top' : 10});
					(function(){
						this.divResponse.morph({'height' : 0, 'padding-top' : 0});
						
						if (xhr.state == 'success'){
							this.toggle();
							this.form.getElements('input.text, textarea').set('value', '');
							this.form.getElements('input.text, textarea').fireEvent('blur');
						}
						
					}.bind(this)).delay(4000, this);
				}.bind(this)
			}).post();
		}.bind(this));
	}
});
