window.addEvent('domready',function() {
	if ( $('slideshow-container')) {
		/* settings */
		var showDuration = 3000;
		var wrap = $('slideshow-container');
		var container = $('slideshow');
		var elem = container.getElements('li');
		var images = container.getElements('img');
		var currentIndex = 0;
		var interval;
		var toc = [];
		var tocActive = 'toc-active';
		var thumbOpacity = 0.3;
		
		/* new: starts the show */
		var start = function() {  $clear(interval); interval = show.periodical(showDuration); };
		var stop = function() { $clear(interval); };
		
		/* worker */
		var show = function(to) {			
			elem[currentIndex].fade('out');
			toc[currentIndex].removeClass(tocActive).fade(thumbOpacity);
			elem[currentIndex = ($defined(to) ? to : (currentIndex < elem.length - 1 ? currentIndex+1 : 0))].fade('in');
			toc[currentIndex].addClass(tocActive).fade(1);
			var increment 			= 0;
			var totIncrement		= 0;
			var maxRightIncrement	= increment*(-(elem.length-3));
			var fx = new Fx.Tween($('slideshow-nav'), {duration: 400});
			var currentTocHeight = toc[currentIndex].height;
			
			if (currentIndex > 2) {
				fx.pause();
				fx.start('margin-top', -((currentIndex - 2)* (currentTocHeight + 9)));				
			} else {
				fx.pause();
				fx.start('margin-top', 0);
			}
		};
		initMultiBox = new multiBox({
			mbClass: ['.exclu_link'],//class you need to add links that you want to trigger multiBox with (remember and update CSS files)
			container: $(document.body),//where to inject multiBox
			// path: './Files/',//path to mp3 and flv players
			useOverlay: true,//use a semi-transparent background. default: false;
			maxSize: {w:800, h:800},//max dimensions (width,height) - set to null to disable resizing
			addDownload: false,//do you want the files to be downloadable?
			// pathToDownloadScript: './Scripts/ForceDownload.asp',//if above is true, specify path to download script (classicASP and ASP.NET versions included)
			addRollover: false,//add rollover fade to each multibox link
			addOverlayIcon: false,//adds overlay icons to images within multibox links
			addChain: false,//cycle through all images fading them out then in
			recalcTop: false,//subtract the height of controls panel from top position
			addTips: false,//adds MooTools built in 'Tips' class to each element (see: http://mootools.net/docs/Plugins/Tips)
			showNumbers: false,
			showControls: false,
			openFromLink: false,
			useAjax: false
		});
		
		/* new: create preview area */
		var preview = new Element('div',{
			id: 'slideshow-controls'
		}).inject('slideshow-container-controls');
		var nav = new Element('div',{
			id: 'slideshow-nav'
		}).inject(preview);
		
		
		/* new: control: table of contents */
		elem.each(function(elements,i){
			var img = elements.getElements('img');
			/* add to table of contents */
			toc.push(new Element('img',{
				src: img.get('src'),
				title: img.get('alt'),
				styles: {
					opacity: thumbOpacity
				},
				events: {
					click: function(e) {
						if(e) e.stop();
						$clear(interval);
						show(i);
						start();
					},
					mouseenter: function() {
						this.fade(1);
					},
					mouseleave: function() {
						if(!this.hasClass(tocActive)) this.fade(thumbOpacity);
					}
				}	
			}).inject(nav));
			if(i > 0) { elements.set('opacity',0); }
			
		});
			/* new: control: next and previous */
		var next = new Element('a',{
			href: '#',
			id: 'next',
			text: '>>',
			events: {
				click: function(e) {
					if(e) e.stop();
					$clear(interval); show();
				}
			}
		}).inject(preview);
		
		var previous = new Element('a',{
			href: '#',
			id: 'previous',
			text: '<<',
			events: {
				click: function(e) {
					if(e) e.stop();
					$clear(interval); show(currentIndex != 0 ? currentIndex -1 : images.length-1);
				}
			}
		}).inject(preview);
	
		/* control: start/stop on mouseover/mouseout */
		container.addEvents({
			mouseenter: function() { $clear(interval); },
			mouseleave: function() { start(); }
		});
		
		$('slideshow-container-controls').addEvents({
			mouseenter: function() { $clear(interval); },
			mouseleave: function() { start(); }
		});
		
		/* start once the page is finished loading */
		window.addEvent('load',function(){ show(0); interval = show.periodical(showDuration); });
		
	}
});

window.addEvent('domready',function() {
	if ( $('proposition-container')) {
		/* settings */
		var showDuration = 3000;
		var wrap = $('proposition-container');
		var container = $('proposition-nav');
		var elem = container.getElements('img');
		var images = container.getElements('img');
		var currentIndex = 0;
		var interval;
		var toc = [];
		var tocActive = 'toc-active';
		var thumbOpacity = 0.3;
		
		/* new: starts the show */
		var start = function() { $clear(interval); interval = show.periodical(showDuration); };
		var stop = function() { $clear(interval); };
		
		/* worker */
		var show = function(to) {
			elem[currentIndex].fade('out');			
			toc[currentIndex].removeClass(tocActive).fade(thumbOpacity);
			elem[currentIndex = ($defined(to) ? to : (currentIndex < elem.length - 1 ? currentIndex+1 : 0))].fade('in');
			toc[currentIndex].addClass(tocActive).fade(1);
			var increment 			= 0;
			var totIncrement		= 0;
			var maxRightIncrement	= increment*(-(elem.length-3));
			var fx = new Fx.Tween($('proposition-nav'), {duration: 400});
			var currentTocHeight = toc[currentIndex].height;
			
			if (currentIndex > 3) {
				fx.pause();
				fx.start('margin-top', -((currentIndex - 3)* (currentTocHeight + 9)));				
			} else {
				fx.pause();
				fx.start('margin-top', 0);
			}
		};
		initMultiBox = new multiBox({
			mbClass: ['.exclu_link'],//class you need to add links that you want to trigger multiBox with (remember and update CSS files)
			container: $(document.body),//where to inject multiBox
			// path: './Files/',//path to mp3 and flv players
			useOverlay: true,//use a semi-transparent background. default: false;
			maxSize: {w:800, h:800},//max dimensions (width,height) - set to null to disable resizing
			addDownload: false,//do you want the files to be downloadable?
			// pathToDownloadScript: './Scripts/ForceDownload.asp',//if above is true, specify path to download script (classicASP and ASP.NET versions included)
			addRollover: false,//add rollover fade to each multibox link
			addOverlayIcon: false,//adds overlay icons to images within multibox links
			addChain: false,//cycle through all images fading them out then in
			recalcTop: false,//subtract the height of controls panel from top position
			addTips: false,//adds MooTools built in 'Tips' class to each element (see: http://mootools.net/docs/Plugins/Tips)
			showNumbers: false,
			showControls: false,
			openFromLink: false,
			useAjax: false
		});
		
		/* new: create preview area */
		var preview = $('proposition-controls'); 
		var nav = $('proposition-nav'); 
		
		
		/* new: control: table of contents */
		images.each(function(elements,i){
			/* add to table of contents */
			
			
			images[i].style.opacity = thumbOpacity;
			


			images[i].addEvent('mouseenter', function() {
						this.fade(1);
					});
			images[i].addEvent('mouseleave', function() {
						if(!this.hasClass(tocActive)) this.fade(thumbOpacity);
					});
			
			if(i == 0) { images[i].style.opacity=0};
			toc.push(images[i]);
			
			

			
		});
			/* new: control: next and previous */
		var next = new Element('a',{
			href: '#',
			id: 'next',
			text: '>>',
			events: {
				click: function(e) {
					if(e) e.stop();
					$clear(interval);
					show();
					start();
				}
			}
		}).inject(preview);
		
		var previous = new Element('a',{
			href: '#',
			id: 'previous',
			text: '<<',
			events: {
				click: function(e) {
					if(e) e.stop();
					$clear(interval);
					show(currentIndex != 0 ? currentIndex -1 : images.length-1);
					start();
				}
			}
		}).inject(preview);
	
		/* control: start/stop on mouseover/mouseout */
		container.addEvents({
			mouseenter: function() { $clear(interval); },
			mouseleave: function() { start(); }
		});
		
		$('proposition-container').addEvents({
			mouseenter: function() { $clear(interval); },
			mouseleave: function() { start(); }
		});
		
		/* start once the page is finished loading */
		window.addEvent('load',function(){ show(0); interval = show.periodical(showDuration); });
		
	}
});


function searchTabs() {
$$('#tabs').each(function(tabList) {
	//get the content list
	var tabContentList = tabList.getNext('#search'),
		//get the name of the cookie, which is the "title" attribute of the tab list
		cookie = 'demo-list',
		//the start tab index
		startIndex = Cookie.read(cookie) || 0,
		//get the actual tab LI items
		tabs = tabList.set('title','').getElements('li'),
		tabsIn = tabs.getElements('span'),
		//get the content LI items
		lis = tabContentList.getElements('.search-box');
		lis.setStyles({opacity: 0, display:'none', position:'relative'});
		//the tab (LI) that is currently active
		activeTab = tabs[startIndex].addClass('selected'),
		activeTabIn = activeTab.getElements('span').addClass('active'),
		//the content LI that is currently active
		activeContent = lis[startIndex].setStyles({opacity: 1, display:'block'});
	//for every tab within this tab/content relationship...
	tabs.each(function(tab,i) {
		//stopper
		//if(e) e.stop();
		//calculate the respective content item's height
		var content = lis[i];
		//add the click event to the tab which...
		tab.addEvent('click',function() {
			//if it's not the currently activated tab...
			if(tab != activeTab) {
				//add and remove the active class from old vs. new tab
				activeTab.removeClass('selected');
				activeTabIn.removeClass('active');
				(activeTab = tab).addClass('selected');
				(activeTabIn = tab.getElements('span')).addClass('active');
				//start the wipe up, wipe down effect
				activeContent.set('tween',{
					onComplete:function() {
						activeContent.setStyle('display','none')
						activeContent = content.set('tween',{ onComplete: $empty }).tween('opacity', 1).setStyle('display','block');
					}, duration: 'fast'
				}).tween('opacity', 0);
				//write to cookie
				Cookie.write(cookie,i);
				//fin!
			}
		});
	});
	//fire click event
	//activeTab.fireEvent('click');
});
new MorphList('tabs');
new MorphList('tabsAgences');

}
window.addEvent('domready', searchTabs);

function firstAndLastEl() {
	if ($('list-products')) $$("#list-products li:first-child").addClass("first");	
	if ($('list-news')) $$("#list-news li:last-child").addClass("last");	
}
window.addEvent('domready', firstAndLastEl);

function hideMessages() {
	function hiding() { $$('.messages').fade('out'); }
	
	if($$('.messages')) {
		$$('.messages').addEvent('click', function() {
			hiding();
		});
	}
}
window.addEvent('domready', hideMessages);

/* Class pour la page produits.php - ScrollFollow */
var ScrollSidebar = new Class({
	
	Implements: [Options],
	
	options: {
		offsets: { x:0, y:0 },
		mode: 'vertical',
		positionVertical: 'top',
		positionHorizontal: 'right',
		speed: 400
	},
	
	initialize: function(menu,options) {
		/* initial options */
		this.setOptions(options);
		this.menu = $(menu);
		this.move = this.options.mode == 'vertical' ? 'y' : 'x';
		this.property = this.move == 'y' ? 'positionVertical' : 'positionHorizontal';
		/* ensure a few things */
		var css = { position: 'absolute', display:'block' };
		css[this.options.positionVertical] = this.options.offsets.y;
		css[this.options.positionHorizontal] = this.options.offsets.x;
		this.menu.setStyles(css).set('tween',{ duration: this.options.speed });
		/* start listening */
		this.startListeners();
	},
	
	startListeners: function() {
		var action = function() {
			this.setPosition($(document.body).getScroll()[this.move] + this.options.offsets[this.move]);
			
		}.bind(this);
		window.addEvent('scroll',action);
		window.addEvent('load',action);
	},
	
	setPosition: function(move) {
		this.menu.tween(this.options[this.property],move);
		return this;
	}
});

/* usage */
window.addEvent('domready',function() {
});


/* Colonnes de mÃªme hauteur */
var Equalizer = new Class({
	initialize: function(elements) {
		this.elements = $$(elements);
	},
	equalize: function(hw) {
		if(!hw) { hw = 'height'; }
		var max = 0,
			prop = (typeof document.body.style.maxHeight != 'undefined' ? 'min-' : '') + hw; //ie6 ftl
			offset = 'offset' + hw.capitalize();
		this.elements.each(function(element,i) {
			var calc = element[offset];
			if(calc > max) { max = calc; }
		},this);
		this.elements.each(function(element,i) {
			element.setStyle(prop,max - (element[offset] - element.getStyle(hw).toInt()));
		});
		return max;
	}
});

function equalize () {
	var equalize = new Equalizer('.services .blocks').equalize('height'); 
}
window.addEvent('load', equalize);

function initImages() {	
	document.imageOut = new Object();
	document.imageOver = new Object();

	var imageArray = $$("img", "input");
	imageArray.each(function(item){
		var image = item.src.substr(item.src.lastIndexOf("/")+1);
		var id = item.id || image.replace("_n.", "").replace("_N.", "");
		var hover = (image.toLowerCase().lastIndexOf("_n.") !=-1);
		//
		if (hover && document.imageOut && document.imageOver) {
			document.imageOut[id] = new Image();
			document.imageOut[id].src = item.src;
			document.imageOver[id] = new Image();
			document.imageOver[id].src = item.src.substr(0, item.src.lastIndexOf("/")+1)+image.replace("_n.", "_o.").replace("_N.", "_O.");
		}

		if (hover && document.imageOut && document.imageOver) {
			item.onmouseover = function(){
				//alert("mouseover");
				if (document.imageOver && document.imageOver[this.id]) setImage(this, document.imageOver[this.id].src);
			}
			item.onmouseout = function(){
				if (document.imageOut && document.imageOut[this.id]) setImage(this, document.imageOut[this.id].src);
			}
			item.id = id;
			//
			function setImage(imageObject, src) {
				if (window.ie && !window.ie7) {
					if (imageObject.filters[f] && imageObject.filters[f].src.test(pngRegExp)) {
						imageObject.filters[f].src = src;
					} else {
						imageObject.src = src;
					}
				} else {
					imageObject.src = src;
				}
			}
		}
	});
}

function initMultiSelect()
{
	
//	Options disponibles
//	   1. boxes - (string: defaults to 'input[type=checkbox']) Checkbox selector
//	   2. labels - (string: defauts to 'label') Label selector
//	   3. monitorText - (string: defaults to ' selected') The text to be shown in the dropdown monitor
//	   4. containerClass - (string: defaults to 'MultiSelect') CSS class (styling)
//	   5. monitorClass - (string: defaults to 'monitor') CSS class (styling)
//	   6. monitorActiveClass - (string: defaults to 'active') CSS class (styling)
//	   7. itemSelectedClass: - (string: defaults to 'selected') CSS class (styling)
//	   8. itemHoverClass: - (string: defaults to 'hover') CSS class (styling)
	
	new MultiSelect("#type", {
		monitorText : ' sélectionné(s)'
	});
}

function initHomeSearch()
{
	if($('search-result')) {
		$('search-result').set('html', unescape($('search-result').get('html')));
		$('type').getElements('li').addEvent('click', handleTypeClickEvent);
		$$('.chambre-nb').addEvent('click', handleChambreChangeEvent);
		
		$('search-form').addEvent('submit', function(ev){
			$$('.span-secteur').fade('out').dispose();
			$('sentence-content').set('value', escape($('search-result').get('html')));
		});
	}
}



function changePhraseChambre(nb)
{
	for(var i=1;i<7;i++)
	{
		removeNbChambre(i);
	}
	addNbChambre(nb);
}

function changePhraseChambre2(nb)
{
	for(var i=0;i<7;i++)
	{
		removeNbChambre(i);
	}
	addNbChambre(nb);
}


function handleChambreChangeEvent(ev)
{
	var nb = $(ev.target).get('value');
	switch($(ev.target).get('checked'))
	{
		case true : addNbChambre(nb);
			break;
		case false : removeNbChambre(nb);
			break;
	}
}

function addNbChambre(nb)
{
	$('span-chambres').morph({'display' : 'inline'});
	var spanNbChambre = new Element('span', {
		'id' 	: 'span-chambre-'+nb,
		'class' : 'span-chambre',
		'html'	: nb
	});
	spanNbChambre.injectInside($('span-chambres'), 'bottom');
	handleLastNbChambreElem();
}

function handleLastNbChambreElem()
{
	var lastElem = $$('.span-chambre').pop();
	$$('.span-chambre').each(function(elem){
		elem.set('html', elem.get('html').match('([0-9]+)')[0] + ',');
	});
	
	if(lastElem) {
		lastElem.set('html', lastElem.get('html').replace(',', '') + ' chambre(s) et plus');
	}
}

function removeNbChambre(nb)
{
	if ($('span-chambre-'+nb)) {
		$('span-chambre-'+nb).dispose();
	}
	if ($("span-chambres").getElements('span').length == 0) {
		$("span-chambres").setStyle('display', 'none');
	}
	handleLastNbChambreElem();
}

function handleTypeClickEvent(ev)
{
	ev.stop();
	var tag = $(ev.target).get('tag');
	if ($(ev.target).getParent('li') !== null) {
		var source = $(ev.target).getParent('li');
	} else {
		var source = $(ev.target);
	}
	var typeBien = source.getChildren('input').shift();
	var nom = typeBien.get('id');
	
	switch(typeBien.get('checked'))
	{
		case true 	: (tag == 'input') ? removeType(nom) : addType(nom);
			break;
		case false	: (tag == 'input') ? addType(nom) : removeType(nom);
			break;
	}
}

function removeType(nom)
{
	if ($('span-type-'+nom)) {
		$('span-type-'+nom).dispose();
	}
	if($('span-types-intro') && $$('.span-type').length == 1) {
		$('span-types-intro').dispose();
	}
}

function addType(nom)
{
	$('span-types').setStyle('display', 'inline');
	if (!$('span-types-intro')) {
		var spanTypesIntro = new Element('span', {id: 'span-types-intro', html: 'un(e)'});
		spanTypesIntro.injectInside($('span-types'), 'top').addClass('span-type');
	} 
	
	var typeSpan = new Element('span', {
		id		: 'span-type-' + nom,
		html	: nom + ', '
	});
	
	typeSpan.injectInside($('span-types'), 'bottom').addClass('span-type');
}


window.addEvent('domready', function(ev){
	initImages();
	initMultiSelect();
	initHomeSearch();
});


