window.addEvent('domready', function() {	
		if ($('content-form') != null) {
			var myForm = new Forms('content-form');
		}
		
		// Fix email
		var anchors = $$("a.mail");
		if ($type(anchors) == 'array') {
			anchors.each(function(elt, i) {
				elt.set('href', elt.get('text').replace(/^(.*)\s+?\[at\]\s+?(.*)\s+?\[dot\]\s+?(.*)/,"mailto:$1@$2.$3"));
			});	
		}
		
		if (Browser.Engine.name == 'trident' && Browser.Engine.version == 4) {
			$$('#main_navigation > li').each(function(i, e) {
				i.addEvents({
					'mouseenter': function () {
						this.set('class', this.get('class')+'_over over');
					},
					'mouseleave' : function () {
						this.set('class', this.get('class').replace(/^(.*)_over\sover$/,'$1'));
					}
				});
			});
		}
		
		$('selectMinisite').addEvent('change', function () { document.location = this.value; });
	});


var Forms = new Class ({
		Implements: Options,
		
		options : {
			formValidInput : 'valid-form',
			errorClass : 'error'
		},

		initialize : function (container, options) {
			this.setOptions(options);
			this.validForm = $(this.options.formValidInput);
			
			this.container = $(container);

			this.obl = [];
			this.initUrl();
			this.initPopup();

			
			this.container.addEvent('submit', function(event) { 
				this.obl.empty();
				this.parseInput(this.container); 
				if (this.obl.length > 0) {
					event.preventDefault(); 
					this.displayObl();
				}
			}.bind(this));
		},
		
		initUrl : function () {
			this.action = this.container.get('action');
		},
		
		initPopup : function () {
			this.popup = new Element ('div', {'id' : 'popup'});
			this.popup.inject(document.body, 'top');
			
			this.popup.addEvents({
				'show' : function () { this.toggleClass('show'); },
				'hide' : function () { this.toggleClass('show'); }
			});
		},
		
		parseInput : function (container) {
			var bool = true;
			(container.getElements('input, textarea, select')).each(function (elt, i) {
				if (elt.hasClass('obligatoire') && this.testInput(elt) == false) {
					elt.addClass(this.options.errorClass);
					this.obl.push(elt);
				}else if (elt.hasClass('error')) elt.removeClass('error');
			}, this);
		},
		
		testInput : function (tag) {
			var result =  true;

			switch (tag.get('tag')) {
				case 'input'	:	if (tag.get('type') == 'text') {
										if (tag.get('value').trim() == '')
											result = false;
										tag.addEvent('click', function() { this.select(); });
									}
									else if (tag.get('type') == 'checkbox' && tag.checked == false) result = false;
									else if (tag.get('type') == 'radio' && tag.checked == false) result = false
									break;

				case "textarea"	:	if (tag.get('value').trim() == '') result = false;
									break;

				case "select"	:	if (tag.getSelected() == null) result = false;
									break
			}
			
			return result;
		},

		displayObl : function () {
			this.popup.empty();
			
			var background = new Element('div', {'class': 'background'});
			background.inject(this.popup);
			
			var message = new Element('div', {'class': 'message'});
			message.addEvent('click', function () { this.popup.fireEvent('hide'); }.bind(this));

			if (this.obl.length > 0) {
				(new Element('p', {'html' : 'Les champs du formulaire <span class="rouge">en rouge</span> contiennent des erreurs ou ne sont pas remplis correctement'})).inject(message);
				(new Element('p', {'html' : '&gt;&gt; Revenir sur le formulaire', 'class' : 'link'})).inject(message);
				(new Element('p', {'class' : 'footer'})).inject(message);
	
				message.inject(this.popup);
				
				this.popup.fireEvent('show');
			}
		},
		
		initDate : function (container, radical) {
			var current = ($('key-gen').get('value')).split('-');

			$$('#'+container+' input').each(function (e, i) {
				if (e.get('name') == radical+'_d') e.set('value', current[0]);
				else if (e.get('name') == radical+'_m') e.set('value', current[1]);
				else if (e.get('name') == radical+'_y') e.set('value', current[2]);
			})

		}
	});