/*--FUNCTIONS.JS--*/

function fieldFocus(element, value){
	if($(element).value == value){
		$(element).value = '';
	}
}

function fieldBlur(element, value){
	if($(element).value == ''){
		$(element).value = value;
	}
}

function carouselNewsGenerator_prototypeJS(highlightId, highlightTransition, highlightDefilement, highlightTimer){
	/*--------------------------------------------------------------------GENERATEUR DE CAROUSEL NEWS AVEC PAGINATION ET DEFILEMENT AUTO sous PtrototypeJS-----------------------------------------------------------------------------*/
	
	//	v1.0
	
	/*------Paramètres-----*/
		//[highlightId]				==> ID du parent
		//[highlightTimer]			==> Durée de la transition
		//[highlightDefilement]			==> [optionel] TRUE ou FALSE : FALSE par défaut
		//[highlightTimer]			==> [optionel] intervalle de défilement en secondes
	/*------Paramètres-----*/
	/*------Spécifications-----*/
	/*
		La taille du <ul> est définie à 10.000px par défaut.
	*/
	/*------Spécifications-----*/
	/*------Structure HTML requise-----*/
	/*
		div id="highlighter">					//BLOCK HIGHLIGHTER		CSS : libre
			<ul class="contentHlg"></ul>				//BLOCK CONTENT			CSS : libre
			<ul class="pagination"></ul>			//BLOCK PAGINATION		CSS : libre
		</div>
	*/
	/*------Structure HTML requise-----*/
	var i = 0;
	$$('#'+highlightId+' ul.pagination li').each(function(el){
		el.writeAttribute('pagination_id',i);
		Event.observe(el, 'click', function(){
			if(!this.hasClassName('active')){
				this.ancestors()[1].writeAttribute('current_id',this.readAttribute('pagination_id'))
				$$('#'+highlightId+' ul.contentHlg li').each(function(el){
					el.setStyle({'display':'none'});
				});
				$$('#'+highlightId+' ul.pagination li.active').each(function(el){
					el.removeClassName('active');
				});
				this.addClassName('active');
				var selectedContent = $$('#'+highlightId+' ul.contentHlg li')[parseInt(el.readAttribute('pagination_id'))];
				selectedContent.appear({duration:highlightTransition});
			}
		});
		i++;
	});
	var i = 0;
	$$('#'+highlightId+'').each(function(el){
		el.writeAttribute('current_id',0);
		if(highlightDefilement){
			new PeriodicalExecuter(function(pe){
				var currentId = parseInt(el.readAttribute('current_id')) + 1;
				var selectedContent = $$('#'+el.id+' ul.contentHlg li')[currentId];
				if(selectedContent){
					$$('#'+el.id+' ul.contentHlg li')[currentId-1].setStyle({'display':'none'});
					el.writeAttribute('current_id',currentId);
				}
				else{
					selectedContent = $$('#'+el.id+' ul.contentHlg li')[0];
					$$('#'+el.id+' ul.contentHlg li')[currentId-1].setStyle({'display':'none'});
					currentId = 0;
					el.writeAttribute('current_id',currentId);
				}
				$$('#'+highlightId+' ul.pagination li.active').each(function(el){
					el.removeClassName('active');
				});
				$$('#'+highlightId+' ul.pagination li')[currentId].addClassName('active');
				selectedContent.appear({duration:highlightTransition});
			}, highlightTimer);
		}
		i++;
	});
}

function diaporamaTrigger(movement, ulIndex, speed){

	var whatLength = parseInt($('diapo').readAttribute('length'));
	var whatItem = parseInt($('diapo').readAttribute('current_id')) + movement;
	
	$('diapo').writeAttribute('current_id', whatItem);
	
	if(movement == 1){
		if(whatItem >= whatLength){
			$('diapo').writeAttribute('current_id', 0);
			whatItem = 0;
			var whatFormerLI = $$('#diapo li')[whatLength-1];
		}
		else{
			var whatFormerLI = $$('#diapo li')[whatItem-1];
		}
	}
	else if(movement == -1){
		if(whatItem < 0){
			$('diapo').writeAttribute('current_id', whatLength-1);
			whatItem = whatLength-1;
			var whatFormerLI = $$('#diapo li')[0];
		}
		else{
			var whatFormerLI = $$('#diapo li')[whatItem+1];
		}
	}
	var whatLI = $$('#diapo li')[whatItem];
	$('diapo').immediateDescendants()[ulIndex+3].innerHTML = parseInt($('diapo').readAttribute('current_id')) + 1;
	
	new Effect.Opacity(whatFormerLI, {
		from:1,
		to:0,
		duration:speed,
		afterFinish:function(){
			whatFormerLI.setStyle({'display':'none'});
		}
	});
	new Effect.Opacity(whatLI, {
		from:0,
		to:1,
		duration:speed,
		afterSetup:function(){
			whatLI.setStyle({'display':'block'});
		}
	});
}
function diaporama_generator(ulIndex, speed, autoDefil, autoDefilTimer, autoDefilResume){

	/*--------------------------------------------------------------------DIAPORAMA_GENERATOR-----------------------------------------------------------------------------*/
	
	//	!!! Requiers		=> 	diaporamaTrigger()
	//	v1.0
	
	/*------Paramètres-----*/
		//[sliderUlIndex]				==> Index de profondeur du <BLOCK DÉPLACÉ> par rapport au <BLOCK SLIDER> (slider exclu)
		//[speed]					==> Durée de la transition en secondes
		//[autoDefil]				==> Défilement auto : TRUE / FALSE (FALSE par défaut)
		//[autoDefilTimer]			==> Intervalle du défilement auto
		//[autoDefilResume]			==> Intervalle de reprise du défilement auto après l'arrêt par click sur les boutons
	/*------Paramètres-----*/
	/*------Structure HTML requise-----*/
	/*
		<div class="slider">					//BLOCK SLIDER				CSS : libre
			<ul>						//BLOCK DÉPLACÉ			CSS : libre
				<li></li>				//ITEM					CSS : position:absolute;
				<li></li>				//ITEM					CSS : position:absolute;
				<li></li>				//ITEM					CSS : position:absolute;
			</ul>
			<span class="buttonLeft off"></span>		//BOUTON PRÉCÉDENT		CSS : libre
			<span class="buttonRight"></span>		//BOUTON SUIVANT			CSS : libre
		</div>
	*/
	/*------Structure HTML requise-----*/

	$('diapo').writeAttribute('current_id', 0);
	$('diapo').writeAttribute('length', $$('#diapo li').length);
	
	$$('#diapo .buttonLeft').each(function(el){
		Event.observe(el, 'click', function(){
			$('diapo').addClassName('manual');
			diaporamaTrigger(-1, ulIndex, speed);
		});
	});
	$$('#diapo .buttonRight').each(function(el){
		Event.observe(el, 'click', function(){
			$('diapo').addClassName('manual');
			diaporamaTrigger(1, ulIndex, speed);
		});
	});
	
	if(autoDefil){
		new PeriodicalExecuter(function(pe){
			if($('diapo').hasClassName('manual')){
				$('diapo').removeClassName('manual');
			}
		}, autoDefilResume);
		new PeriodicalExecuter(function(pe){
			if(!$('diapo').hasClassName('manual')){
				diaporamaTrigger(1, ulIndex, speed);
			}
		}, autoDefilTimer);
	}
	
}

function toolbar_bottom_ie6fix(divName){
	/*--------------------------------------------------------------------toolbar_bottom_ie6fix-----------------------------------------------------------------------------*/
	
	//	v1.1
	
	/*------Description-----*/
		// Permet de corriger le positionnement "FIXED" sur IE6 pour garder une TOOLBAR positionnée en bas indépendemment du scrolling
	/*------Description-----*/
	
	/*------Paramètres-----*/
		//[divName]			==> Nom de l'ID du div à positionner
		//[toolbarHeight]			==> Hauteur de la TOOLBAR
	/*------Paramètres-----*/
	if(Prototype.Browser.IE6=Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==6){
		Event.observe(window,'scroll',function(){
			if($(divName)){
				var offset = $$('body')[0].cumulativeScrollOffset().top + document.viewport.getDimensions().height - $(divName).getDimensions().height;
				$(divName).setStyle({'position':'absolute','top':offset+'px'});
			}
		});
		Event.observe(window,'resize',function(){
			if($(divName)){
				var offset = $$('body')[0].cumulativeScrollOffset().top + document.viewport.getDimensions().height - $(divName).getDimensions().height;
				$(divName).setStyle({'position':'absolute','top':offset+'px'});
			}
		});
	}
}

//greyBoxShow() : OUVERTURE GREYBOX
function greyBoxShow(mode, url, imgPath, width, height, overflow){
	//Si OVERLAY n'existe pas
	if(!$('overlay')){
		// On l'ajoute dynamiquement
		$$('body')[0].insert({bottom:'<div id="overlay" onclick="greyBoxHide();"></div>'});
	}
	//Si GREYBOX n'existe pas
	if(!$('greyBox')){
		// On l'ajoute dynamiquement
		// greyBox			==> Positionnement en absolute centré
		// greyBoxContent	==> Contenus de la box
		// greyBoxLoader		==> Gif annimé de chargement
		// close			==> Div de fermeture
		$$('body')[0].insert({bottom:'<div id="greyBox"><div class="content" id="greyBoxContent"></div><div onclick="greyBoxHide();" class="close"><img src="'+imgPath+'close.gif" alt="" /></div></div>'});
	}
	
	// On dimensionne la box suivant les paramètres passés
	$('greyBoxContent').setHTML = '<div id="greyBoxLoader"></div>';
	$('greyBoxContent').writeAttribute('style', '');
	if(typeof(overflow)!='undefined' && overflow==true){
		$('greyBoxContent').setStyle({'width':width+'px', 'height':height+'px'});
	}
	else{
		$('greyBoxContent').setStyle({'width':width+'px'});
	}
	// On centre la box suivant les dimensions passés
	$('greyBox').writeAttribute('style', '');
	$('greyBox').setOpacity(1);
	$('greyBox').setStyle({'left':'-'+(Math.ceil(width/2))+'px'});
	
	// On dimensionne la taille de l'overlay sur IE6
	var testIE6 = Prototype.Browser.IE6=Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==6;
	if(testIE6){
		windowHeightIeFix = document.viewport.getHeight();
		if(document.viewport.getHeight() == 0){
			var windowHeightIeFix = 670;
		}
		$('overlay').setStyle({'height':windowHeightIeFix+'px'});
		$$('select').each(function(el){el.setStyle({'display':'none'});});
	}
	
	// Mode de contenu à afficher
	switch(mode){
		// AJAX		==> Page ajax à charger
		case 'ajax':
			if(testIE6){
				new Ajax.Request(url, {
					onComplete: function(transport){
						$('overlay').setStyle({'display':'block'});
						$('greyBox').setStyle({'display':'block'});
						$('greyBoxContent').update(transport.responseText);
					}
				});
			}
			else{
				$('overlay').setOpacity(0);
				$('overlay').setStyle({'display':'block'});
				$('overlay').fade({
					duration:0.5,
					from:0,
					to:0.6,
					afterFinish:function(){
						$('greyBox').setStyle({'display':'block'});
						new Ajax.Request(url, {
							onComplete: function(transport){
								$('greyBoxContent').update(transport.responseText);
							}
						});
					}
				});
			}
			break;
		// IFRAME		==> Iframe ajax à afficher
		case 'iframe':
			$('greyBoxContent').innerHTML = '<iframe src="'+url+'" border="0" frameborder="0" width="'+width+'" height="'+height+'" allowTransparency="true"></iframe>';
			if(testIE6){
				$('overlay').setStyle({'display':'block'});
				$('greyBox').setStyle({'display':'block'});
			}
			else{
				$('overlay').setOpacity(0);
				$('overlay').setStyle({'display':'block'});
				$('overlay').fade({
					duration:0.5,
					from:0,
					to:0.6,
					afterFinish:function(){
						$('greyBox').setStyle({'display':'block'});
					}
				});
			}
			break;
		// YOUTUBE	==> Code video youtube à afficher
		case 'youtube':
			$('greyBoxContent').innerHTML = '<object width="'+width+'" height="'+height+'"><param name="movie" value="http://www.youtube.com/v/'+url+'&hl=fr_FR&fs=1&rel=0&color1=0x006699&color2=0x54abd6"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'+url+'&hl=fr_FR&fs=1&rel=0&color1=0x006699&color2=0x54abd6" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+width+'" height="'+height+'"></embed></object>';
			if(testIE6){
				$('overlay').setStyle({'display':'block'});
				$('greyBox').setStyle({'display':'block'});
			}
			else{
				$('overlay').setOpacity(0);
				$('overlay').setStyle({'display':'block'});
				$('overlay').fade({
					duration:0.5,
					from:0,
					to:0.6,
					afterFinish:function(){
						$('greyBox').setStyle({'display':'block'});
					}
				});
			}
			break;
		// DAILYMOTION	==> Code video dailymotion à afficher
		case 'dailymotion':
			$('greyBoxContent').innerHTML = '<object width="'+width+'" height="'+height+'"><param name="movie" value="http://www.dailymotion.com/swf/video/'+url+'"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed type="application/x-shockwave-flash" src="http://www.dailymotion.com/swf/video/'+url+'" width="'+width+'" height="'+height+'" allowfullscreen="true" allowscriptaccess="always"></embed></object>';
			if(testIE6){
				$('overlay').setStyle({'display':'block'});
				$('greyBox').setStyle({'display':'block'});
			}
			else{
				$('overlay').setOpacity(0);
				$('overlay').setStyle({'display':'block'});
				$('overlay').fade({
					duration:0.5,
					from:0,
					to:0.6,
					afterFinish:function(){
						$('greyBox').setStyle({'display':'block'});
					}
				});
			}
			break;
		// IMAGE		==> Image à afficher
		case 'image':
			$('greyBoxContent').innerHTML = '<img src="'+url+'" alt="" />';
			$('greyBoxContent').setStyle({'width':width+'px', 'height':height+'px', 'overflow':'hidden'});
			if(testIE6){
				$('overlay').setStyle({'display':'block'});
				$('greyBox').setStyle({'display':'block'});
			}
			else{
				$('overlay').setOpacity(0);
				$('overlay').setStyle({'display':'block'});
				$('overlay').fade({
					duration:0.5,
					from:0,
					to:0.6,
					afterFinish:function(){
						$('greyBox').setStyle({'display':'block'});
					}
				});
			}
			break;
		default:
			break;
	}
	
	// On remonte le scroll en haut de page
	$$('body')[0].scrollTo();
	
	return false;
}
//greyBoxHide() : FERMETURE GREYBOX
function greyBoxHide(){
	var testIE6 = Prototype.Browser.IE6=Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==6;
	if(testIE6){
		$('overlay').setStyle({'display':'none'});
		$('greyBox').setStyle({'display':'none'});
		$('greyBoxContent').innerHTML = '';
		$$('select').each(function(el){el.setStyle({'display':'block'});});
	}
	else{
		$('greyBox').fade({
			duration:0.4,
			from:1,
			to:0,
			afterFinish:function(){
				$('greyBoxContent').innerHTML = '';
				$('overlay').fade({
					duration:0.4,
					from:0.6,
					to:0
				});
			}
		});
	}
}

function displaySortList(element){
	if($(element).hasClassName('collapse')){
		$(element).removeClassName('collapse');
		$(element).next(0).blindUp({
			duration:0.7
		});
	}
	else{
		element.siblings().each(function(el){
			if(el.hasClassName('collapse')){
				el.removeClassName('collapse');
				el.next(0).blindUp({
					duration:0.7
				});
			}
		});
		$(element).addClassName('collapse');
		$(element).next(0).blindDown({
			duration:0.7,
			afterFinish: function(){
				// On remonte le scroll en haut de page
				$$('body')[0].scrollTo();
			}
		});
	}
}

function expandCollapseFooter(toggler){
	if($('footerMore').hasClassName('collapse')){
		$('footerMore').removeClassName('collapse');
		$(toggler).removeClassName('collapse');
		//$('moreInformationSign').innerHTML = '+';
	}
	else{
		$('footerMore').addClassName('collapse');
		$(toggler).addClassName('collapse');
		//$('moreInformationSign').innerHTML = '-';
	}
}

function collapseSections(){
	$$('.col_25 .title_4.collapse').each(function(el){
		el.removeClassName('collapse');
		el.next(0).blindUp({
			duration:0.7
		});
	});
}

function resetFilters(){
	$$('.selectList li.active, .mediaType:not(.dates) .item.active').each(function(el){
		el.removeClassName('active');
	});
	var i = 0;
	$$('.sortedList li').each(function(el){
		el.removeClassName('lastLineChild');
		el.removeClassName('lineFirstChild');
		el.removeClassName('lineLastChild');
		if(i%3 == 0){
			el.addClassName('lineFirstChild');
		}
		else if(i%3 == 2){
			el.addClassName('lineLastChild');
		}
		else{
			el.removeClassName('lineFirstChild');
		}
		var theLength = $$('.sortedList li').length;
		var reste = theLength%3;
		switch(reste){
			case 0:
				if(i >= theLength-3){
					el.addClassName('lastLineChild');
				}
				break;
			case 1:
				if(i >= theLength-1){
					el.addClassName('lastLineChild');
				}
				break;
			case 2:
				if(i >= theLength-2){
					el.addClassName('lastLineChild');
				}
				break;
		}
		el.setStyle({'display':'block'});
		i++;
	});
}
function applyFilter(trigger, type, value){
	if(!$(trigger).hasClassName('active')){
	
		resetFilters();
		$(trigger).addClassName('active');
		
		$$('.sortedList li:not(['+type+'="'+value+'"])').each(function(el){
			el.setStyle({'display':'none'});
		});
		var i = 0;
		$$('.sortedList li['+type+'="'+value+'"]').each(function(el){
			el.removeClassName('lastLineChild');
			el.removeClassName('lineFirstChild');
			el.removeClassName('lineLastChild');
			if(i%3 == 0){
				el.addClassName('lineFirstChild');
			}
			else if(i%3 == 2){
				el.addClassName('lineLastChild');
			}
			var theLength = $$('.sortedList li['+type+'="'+value+'"]').length;
			var reste = theLength%3;
			switch(reste){
				case 0:
					if(i >= theLength-3){
						el.addClassName('lastLineChild');
					}
					break;
				case 1:
					if(i >= theLength-1){
						el.addClassName('lastLineChild');
					}
					break;
				case 2:
					if(i >= theLength-2){
						el.addClassName('lastLineChild');
					}
					break;
			}
			i++;
		});
		
	}
	else{
	
		$(trigger).removeClassName('active');
		$$('.sortedList li').each(function(el){
			el.setStyle({'display':'block'});
		});
		
	}
}

function showVehiculeType(type){
	hideVehiculeType();
	$$('.vehiculeType span.'+type).each(function(el){
		el.addClassName('active');
	});
}
function hideVehiculeType(){
	$$('.vehiculeType span').each(function(el){
		el.removeClassName('active');
	});
}

function adjustToolbarPosition(){
	if(document.viewport.getDimensions().height < 677 && document.viewport.getDimensions().height > 600){
		$('toolbar').setStyle({'top':(document.viewport.getDimensions().height-77)+'px'});
	}
	else if(document.viewport.getDimensions().height <= 600){
		$('toolbar').setStyle({'top':'523px'});
	}
}

Event.observe(window, 'load', function(){
	
	if($$('body.section00').length == 1){
		adjustToolbarPosition();
	}
	
	var testIE6 = Prototype.Browser.IE6=Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==6;
	if(testIE6 && $('toolbar')){
		toolbar_bottom_ie6fix('toolbar');
	}
	
	var bodyBg = $$('body')[0];
	if(!bodyBg.hasClassName('section00')){
		if(typeof(bodyBg.readAttribute('bgimage')) == 'undefined' || bodyBg.readAttribute('bgimage') == ''){
			bodyBg.setStyle({'backgroundImage':'url('+absolute_root+'/media/image/_img/range/default.jpg)'});
		}
		else{
			bodyBg.setStyle({'backgroundImage':'url('+absolute_root+'/media/image/_img/range/'+bodyBg.readAttribute('bgimage')+'.jpg)'});
		}
	}

});

Event.observe(window,'resize',function(){
	if($$('body.section00').length == 1){
		adjustToolbarPosition();
	}
});