/* Contact 1.0 - 2011.04.20
/*
/* Created by Luca Pillonel
/* copyright www.oxima.ch
--------------------------------------*/

var Contact = new Class({
	initialize : function(){
		this.opened = false;
	
		this.setBehaviour();
		this.setCheckForm();
	},
	
	setBehaviour : function(){
		var fxHeight = new Fx.Tween(document.body.getElement('#address .container'), {
			property : 'height',
			link : 'cancel',
			transition: 'quad:in:out',
			duration: 300
		});
	
		$('address').getElement('address').addEvents({
			mouseenter : function(){
				if(!this.opened) fxHeight.start(100);
			}.bind(this),
			mouseleave : function(){
				if(!this.opened) fxHeight.start(91);
			}.bind(this),
			click : function(e){
				e.preventDefault();
				if(this.opened = !this.opened) {
					$('address').addClass('open');
					fxHeight.start(500);
				} else {
					$('address').removeClass('open');
					this.formValidator.reset();
					$('contact-form').getElements('.block').tween('marginTop', 0);
					fxHeight.start(91);
				}
				
			}.bind(this)
		});
		
		$('contact-form').getElement('input[type="button"]').addEvent('click', function(e){
			this.opened = false;
			$('address').removeClass('open');
			this.formValidator.reset();
			$('contact-form').getElements('.block').tween('marginTop', 0);
			fxHeight.start(91);
		}.bind(this));
	},
	
	setCheckForm : function(){
		Locale.define('fr-FR', 'FormValidator', {
			required: 'Ce champ est requis.',
			email: 'Veuillez saisir une adresse email valide.',
			url: 'Veuillez saisir une URL, comme http://www.example.com.'
		});

		Locale.use('fr-FR');
		
		var form = $('contact-form').getElement('form');
		
		this.formValidator = new Form.Validator.Inline(form, {
			errorPrefix : '',
			scrollToErrorsOnSubmit : false,
			onElementPass : function(element){
				element.getParent().tween('marginTop', 0);
			},
			onElementFail : function(element, errors){
				element.getParent().tween('marginTop', 10);
			},
			showError: function(errorElement){
				errorElement.setStyles({
					top : 2,
					opacity : 0,
					display : 'block'
				}).morph({
					top : -8,
					opacity : 1
				});
			},
			hideError: function(errorElement){
				errorElement.morph({
					top : 2,
					opacity : 0
				});
			}
		});
		
		var result = new Element('div#contactSuccess').setStyles({
			opacity : 0
		}).inject(form, 'after');
		
		new Form.Request(form, result, {
			onSuccess : function(){
				form.tween('left', -500);
				result.tween('opacity', 1);
				
				//btn close qui reset la chose
				new Element('input#closeContact[type="button"][value="Fermer"]')
				.setStyle('opacity', 0)
				.addEvent('click', function(){
					//reset
					form.tween('left', 0);
					$('closeContact').tween('opacity', 0);
					result.tween('opacity', 0);
					$('address').getElement('address').fireEvent('click', new Event());
					(function(){
						$('closeContact').destroy();
						result.empty();
					}).delay(500)
				})
				.inject(result, 'after')
				.tween('opacity', 1);
			}
		});
	}
}); 
