	window.addEvent('domready', function() {
		if ($('baseline') != null) {
			$('baseline-container').adopt($('baseline'));
		}

		if ($('stations-thermales') != null)
			var myTab = new StationsTab("stations-thermales");
		
		if ($('stations-carte') != null)
			var myMap = new StationsCarte("stations-carte", "stations-carte-target");

		if ($('station-plus') != null) {
			var cnt = new Element('div', {'class' : 'focus'});
			($('content').getParent()).grab(cnt.grab($('station-plus')));
		}
		
		if ($('focus-plus') != null) {
			var cnt = new Element('div', {'class' : 'focus'});
			($('content').getParent()).grab(cnt.grab($('focus-plus')));
		}
		
		if ($('pathologies-list') != null) {
			var pathList = new Pathologies('pathologies-list');
		}
		
		if ($('content-form') != null) {
			var myForm = new Formulaire('content-form');
			myForm.initDate('date-rappel', 'rappel');
			myForm.initDate('date-naissance', 'date_naissance');
			
			if (document.referrer.contains('minicure.com')) {
				var enableSejour = $('content-form').getElement('input[value=sejour-sante]');	
				enableSejour.set('checked', true);
			}
			
			if ($('orientation1') != null) {
				myForm.orientation('orientation1', 'orientation2');
			}
			
			if($('stationPathForm') != null) {
				$$('#stationPathForm input[type=checkbox]').each(function(elt, i) {
					elt.addEvent('click', function () {
						myForm.getStationPath('stationPathForm');
					});
				});
			}
			
			if($('stationPathFormHeb') != null) {
				$$('#stationPathFormHeb input[type=radio]').each(function(elt, i) {
					elt.addEvent('click', function () {
						myForm.getStationPath('stationPathFormHeb');
					});
				});
			}
		}
		
		if ($('newsletter-form') != null) {
			var myForm = new Formulaire('newsletter-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) {
			$$('#mainNav > 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'));
					}
				});
			});
			
			($('shortcuts').getFirst('li.lettre-information')).addEvents({
					'mouseenter' : function () { this.addClass('over'); },
					'mouseleave' : function () { this.removeClass('over'); }
			});
		}


		/*  Video 3 soins 30 euros */
		if ($('video3soins30euros') != null) {		
			var videoPub3soins = $('video3soins30euros');
			videoPub3soins.set('html',AC_FL_RunContent("codebase", "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0","width", "512","height", "335","src", "/download/thermauvergne","quality", "high","pluginspage", "http://www.macromedia.com/go/getflashplayer","align", "middle","play", "true","loop", "true","scale", "showall","wmode", "window","devicefont", "false","id", "video3soins30euros","bgcolor", "#ffffff","name", "thermauvergne","menu", "true","allowFullScreen", "false","allowScriptAccess","sameDomain","movie", "/download/thermauvergne","salign", ""));
		}
		
		if ($('videoStation') != null) {
			if (document.URL.match(/vichy/)) {
				$('videoStation').set('html', '<a href="http://www.auvergne-thermale.com/actualite/1/2010/09/video-sur-la-cure-thermale-a-vichy.php"><img src="/templates/02/images/video-thermes-vichy.png" width="160" height="43" alt="Vidéo des Thermes de Vichy" title="Visionnez la vidéo réalisée par la Compagnie des Eaux de Vichy" /></a>');
			}
		}
		
		/*
		if ($('rappelLink') != null) {
			$('rappelLink').addEvent('click', function(e) { 
				e.stop();
				alert("Le formulaire de demande de rappel est momentanément indisponible.\nMerci de votre compréhension.");
			});
			
			if(document.URL.match(/formulaire-rappel-telephone/)) alert("Le formulaire de demande de rappel est momentanément indisponible.\nMerci de votre compréhension.");
		}
		*/
		
		
		if ($('hebStation') != null) {
			$$('#hebStation td.link').each(function(elt, i) {
				elt.getParent('tr').addEvent('click', function (e) { e.stop(); e.preventDefault(); document.location = (elt.getElement('a')).get('href'); } );
			});
		}
		
		$$($(document.body).getElements('a')).each(function(elt, i) {
			if (elt.get('href') != null) {
				if ((elt.get('href')).indexOf('http\://www\.auvergne-thermale\.com') == -1 && (elt.get('href')).substr(0,1) != "/")
 					elt.setProperty('target', '_blank');
				
				if ((elt.get('href')).indexOf('http\://www\.auvergne-thermale\.com/download/') != -1 || (elt.get('href')).indexOf('/download/') != -1)
 					elt.setProperty('target', '_blank');
			}
		});

		
		String.prototype.getRoot = function () {
			return this.replace(/^.*\/([^\/]+)$/g,"$1");
		}
	});	


	var StationsTab = new Class ({
		Implements: Options,

		options	:	{
			ind1: "/templates/04/images/ind1.gif",
			ind2: "/templates/04/images/ind2.gif"
		},
		
		initialize : function (container, options) {
			this.setOptions(options);
			this.container = $(container);

			// Répartie les différentes parties du tableau
			this.initBody();

			// Initialise les évènements des pathologies et récupère les éléments this.pathologie
			this.pathologie = this.initPathologies();
			
			// Initialise les évènements liés aux stations et récupère les éléments dans le tableau this.stations
			this.stations = this.initStations();
			
			// Ajoute les évènements supplémentaires pour les pathologies
			this.pathologie.each(function (e,i) {
				if ($chk(e))
					e.addEvents({
						'mouseover' :	function () {
												this.pathologie[i].fireEvent("up");
												this.affStation(i, "on");
										}.bind(this),
						'mouseleave':	function () {
											this.pathologie[i].fireEvent("down");
											this.affStation(i, "off");
										}.bind(this)
					});
			}, this);
		},
		
		initBody: function () {
			(this.container.getElements('tbody')).each(function(item, index) { 
				if (item.get('class') == "head")
					this.pathologie = item;
				else this.stations = item;
			}, this);
		},
		
		initPathologies : function () {
			var path = [];

			(this.pathologie.getElements('td')).each(function(elt, index) { 
				if ($chk(elt.getElement("a"))) {
					elt.addEvents({
						'up' : function() { this.addClass('overp'); },
						'down':function() { this.removeClass('overp');}
					});
					path[index] = elt; 
				}
			}, this);

			return path;
		},
		
		initStations : function () {
			var tr = this.stations.getElements('tr');

			tr.each(function (elt, i) {  
				this.initStationsLib (elt);
			}, this);
			
			return tr;
		},

		initStationsPath : function (id, tr) {
			var temp = [];
			tr.getElements('td').each(function(elt, i) { 
				if (elt.getElement("a")) temp[i] = elt;  
			});

			return temp;
		},
		
		initStationsLib : function (elt) {
			elt.addEvents({
				'mouseover'	:	function () { 
									var tmp = elt.getElements("td"); 
									tmp.each(function(e,i) { 
										img = e.getElement("img");
										if ($chk(img)) {
											img.set('src',this.options.ind2); 
											this.pathologie[i].fireEvent("up");
										}
									}, this);
									elt.getFirst('td').addClass('over');
								}.bind(this),

				'mouseleave':	function () { 
									var tmp = elt.getElements("td"); 
									tmp.each(function(e,i) { 
										img = e.getElement("img");
										if ($chk(img)) {
											img.set('src',this.options.ind1); 
											this.pathologie[i].fireEvent("down");
										}
									}, this);
									elt.getFirst('td').removeClass('over');
								}.bind(this),
				
				'upCell'	:	function (index) {
									var bool = false;
									var tmp = elt.getElements("td");
									tmp.each(function(e,i) {
										img = e.getElement("img");
										if ($chk(img) && index == i) {
											img.set('src',this.options.ind2); 
											bool = true;
										}
									}, this);
									if (bool == true) elt.getFirst('td').addClass('over');
								}.bind(this)
			});
		},
		
		
		affStation : function (index, mode) {
			this.stations.each(function(e,i) {
				if (mode == "on")
					e.fireEvent("upCell", index);
				else e.fireEvent("mouseleave");
			});
		}
		
	});



	var StationsCarte = new Class({
		Implements: Options,
		
		options: {
			backgroundItem : "#3eb4b4",
			colorItem : "#fff",
			heightItem : "15px",
			paddingItem : "2px",
			puceItem : '/templates/03/images/carte-puce3.png'
		},
		
		initialize : function (container, target, options) {
			this.setOptions(options);
			this.container = $(container);
			this.target = $(target);
			this.area = [];
			this.puce = this.createPuce();
			
			this.target.grab(this.puce);
			
			var cntArea = this.container.getElements("area").length;
			(this.container.getElements("area")).each(function (e,i) {
				if (i < cntArea-1) {
					var temp = e.coords.split(',');
					var coordElt = {'x' : temp[0].toInt(), 'y' : temp[1].toInt()};

					this.area[i] = {'elt' : e, 'lib' : this.createLib(e.alt,coordElt), 'x' : coordElt.x, 'y' : coordElt.y};
					this.target.grab(this.area[i].lib);

					this.area[i].elt.addEvents({
						'mouseover': function () { 
							if (this.area[i].lib.getStyle('display') != 'block')
								this.area[i].lib.style.display = 'block'; 
							}.bind(this),
						'mouseleave': function () { 
							this.area[i].lib.style.display = 'none'; 
							this.puce.style.display = 'none';
							}.bind(this) }
						);
				}
			}, this);
		},
		
		createLib : function (libelle, coord) {
			return new Element ('div', {
				'html'		:	libelle,
				'styles'	:	{
					'position': 'absolute',
					'display' : 'none',
					'left' : (coord.x-20)+"px",
					'top' : ((coord.y > 100)?(coord.y-30):(coord.y+20))+"px",
					'background-color' : this.options.backgroundItem,
					'color' : this.options.colorItem,
					'height': this.options.heightItem,
					'padding': '2px 4px',
					'font-family' : 'Georgia, "Times New Roman", Times, serif', 
					'font-size' : '10px',
					'text-transform' : 'uppercase',
					'font-weight' : 'bolder',
					'z-index': 20
				}
			});
		},
		
		createPuce : function () {
			return new Element ('img', {
				'src' : this.options.puceItem,
				'class' : 'carte-puce'
			});
		}
	});


	var Pathologies = new Class ({
		initialize : function (container) {
			this.container = $(container);
			this.pathList = [];

			(this.container.getElements('a')).each(function (elt, i) {
				elt.addEvents({
					'mouseover' : function (event) {
						$('path-'+this.get('class')).toggleClass('on');
					},
					'mouseleave' : function () {
						$('path-'+this.get('class')).toggleClass('on');
					}
				});
			}); 
		}
	});


	var Formulaire = 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.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 qui apparaissent en <span class="orange">orange</span> contiennent des erreurs ou ne sont pas remplis correctement'})).inject(message);
//				this.obl.each(function(elt, i) { (new Element('p', {'html' : elt.title})).inject(div); }, this);
				(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]);
			});
		},
		
		
		orientation : function (cible, cible2) {
			var pathologies = [];
			$$('input[type=hidden]').each(function (e,i) {
				if (e.get('name').indexOf('pathologie_') != -1)
					pathologies[e.get('name').replace(/^[^0-9]+(.*)$/,'$1')] = e.get('value');
			});
			
			var stations = [];
			$$('input[name=type_station]').each(function (e, i) {
				$$('input[name=station_'+e.get('value')+']').each(function(e1, i1) {
					stations[e.get('value')] = {pathologies: e1.get('value'), periode: e1.get('title')};
				});
				
				e.addEvent('click', function () { 
					var temp = (stations[this.get('value')].pathologies).split('-');
					var dateCure = (stations[this.get('value')].periode).split('|');
					var currentDate = ($('key-gen').get('value')).split('-');
					var ouverture = dateCure[0].split('-');
					var fermeture = dateCure[1].split('-');
					
					$(cible).empty();
					for (var i=0; i < temp.length; i++)
						(new Element('p', {'html': '<input type="radio" name="orientation_1" value="'+pathologies[temp[i]]+'" class="orientation" /> '+pathologies[temp[i]]})).inject($(cible));
					
					$(cible2).empty();
					(new Element('p', {'html': '<input type="radio" name="orientation_2" value="---" class="orientation" checked /> Je ne souhaite pas de double orientation'})).inject($(cible2));
					for (var i=0; i < temp.length; i++)
						(new Element('p', {'html': '<input type="radio" name="orientation_2" value="'+pathologies[temp[i]]+'" class="orientation" /> '+pathologies[temp[i]]})).inject($(cible2));
					
					$$('#date-cure input[type=text]').each(function(e2,i2) {
						if (e2.get('name') == 'date_cure_d') e2.set('value', (ouverture[2].toInt() < currentDate[0].toInt())?currentDate[0]:ouverture[2]);
						else if (e2.get('name') == 'date_cure_m') e2.set('value', (ouverture[1].toInt() < currentDate[1].toInt())?currentDate[1]:ouverture[1]);
						else if (e2.get('name') == 'date_cure_y') e2.set('value', ouverture[0]);
					});
					
					$('info-date-cure').set('html','<b>Attention</b> La période d\'activité des thermes de cette station est du <b>'+ouverture[2]+'/'+ouverture[1]+'/'+ouverture[0]+'</b> au <b>'+fermeture[2]+'/'+fermeture[1]+'/'+fermeture[0]+'</b>');
				});
			});
		},
		
		getStationPath : function (container) {
			var path = [];
			
			if (container == "stationPathForm") {
				$$('#'+container+' input[type=checkbox]').each(function(elt, i) {
					if (elt.checked == true) 
						path.push(elt.get('name').replace(/^station_([0-9]+)$/,'$1'));
				});
			}
			
			if (container == "stationPathFormHeb") {
				$$('#'+container+' input[type=radio]').each(function(elt, i) {
					if (elt.checked == true) 
						path.push(elt.get('title').replace(/^station_([0-9]+)$/,'$1'));
				});
			}
			
			
			var reqNews = new Request({
					method : 'get', 
					url : '/include/devspe.php?action=getStationPathDoc&path='+path.toString(),
					onRequest: $('pathForm').set('html', 'en cours de chargement...'),
					onComplete: function (response) { $('pathForm'). set('html', response); }
			}).send(null);
		}
	});
