window.addEvent('domready', function() {	

	// Execution automatique
	(function () {
		String.prototype.getRoot = function () {
			return this.replace(/^.*\/([^\/]+)$/g,"$1");
		}
	
		var widgetCfe = $$('#widgetCfe div.slider');
		if (widgetCfe[0] != null) {
			var slider = new Fx.Scroll.Carousel(widgetCfe[0],{
				mode: 'horizontal'
			});
	
			var show = function () { slider.toNext(); };
			$('wdgNext').addEvent('click', function(e) { e.stop(); if(slider.currentIndex == 0) slider.currentIndex = 2;  slider.toNext(); });
			$('wdgPrevious').addEvent('click', function(e) { e.stop(); slider.toPrevious2(); });
			
			widgetCfe[0].getElements("a").each(function (elt, i) {  
				var p = elt.getParent("p");
/*				p.addEvent('click', function() {  
					if ((elt.get('href')).indexOf('http\://www\.cnpl\.fr') == -1 && (elt.get('href')).substr(0,1) != "/") {	
 						window.open(elt.get('href'));
					}
					else document.location = elt.get('href');
				});*/
			});
		}
		
		
		$$('p.visuel').each(function(elt, i) { if (elt.get('html') == "") elt.destroy(); })
		
		if ($('partenaireSlide') != null) {
			var sliderPart = new Fx.Scroll.Carousel($('partenaireSlide'),{
				mode: 'horizontal'
			});
	
			var showPart = function () { sliderPart.toNext(); };
			showPart.periodical(2000);
		}
		
		if ($('launchDiaporama')) {
			var diapo = [];
			$$('.album a[rel=lightbox-slideshow-page]').each(function(e,i) { diapo.push([e.get('href')]) });
			$('launchDiaporama').addEvent('click', function(e) { e.stop(); Slimbox.open(diapo, 0, {loop: true}); });
		}
		
		if ($('submenuNav') != null) {
			var urlRef = document.URL.getRoot();
			$$('#submenuNav a').each(function(elt, i) {
				if (elt.get('href') != null && elt.get('href').getRoot() == urlRef) {
					var li = elt.getParent('li');
					li.set('class','over');
				}
			});
			
			if ($('newsBar') != null)
				$('newsBar').destroy();
		}
		
		if ($('searchExpList') != null) {
			var reqNews = new Request({
				method : 'get', 
				url : '../include/exposants.php?action=searchLst',
				onRequest: $('searchExpList').set('value', 'en cours de chargement...'),
				onComplete: function (response) { 
					$('searchExpList'). set('html', response);
				}
			}).send(null);
		}
		
		if ($('newsletterEvents') != null) {
			var reqNews = new Request({
				method : 'get', 
				url : '../include/devspe.php?action=newsletter_evt&l=3',
				onRequest: $('newsletterEvents').set('value', 'en cours de chargement...'),
				onComplete: function (response) { 
					$('newsletterEvents'). set('html', response);
				}
			}).send(null);
		}
			
		if ($('content-form') != null) {
			var myForm = new Forms('content-form');
		}


		// Cible d'affichage des liens externes au site
		$$($(document.body).getElements('a')).each(function(elt, i) {
				if (elt.get('href') != null) {
					if ((elt.get('href')).indexOf('http\://www\.vinidome\.com') == -1 && (elt.get('href')).substr(0,1) != "/") {	
 						elt.setProperty('target', '_blank');
					}
					if ((elt.get('href')).indexOf('http\://www\.vinidome\.com/download/') != -1 || (elt.get('href')).indexOf('/download/') != -1) {	
 						elt.setProperty('target', '_blank');
					}
				}
		});



		
	})();
	
	
});


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();

			$('valid-form').addEvent('click', function(event) {
				event.stop();
				this.obl.empty();
				this.parseInput(this.container); 
				if (this.obl.length > 0) {
					event.preventDefault(); 
					this.displayObl();
				}else $('content-form').submit();
			}.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.get('value') == 0) 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 rose</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');
			}
		}
	});

