//functions de gestion de la pseudo pop up mot de passe perdu
function voirLostPass(){

	var closePass = function(){
		if ($('lostPassMask')){
			masqueappear.start('opacity', 0);
			(function(){ $('lostPassMask').dispose(); }).delay(800);
		}
		if ($('lostPassContent').getStyle('display') == "block"){
			lostPassAppear.start('opacity', 0);
			(function(){ $('lostPassContent').setStyle('display', 'none'); }).delay(800);
		}
	}

	masque = new Element('div', {
		'id'		: 'lostPassMask',
		'styles'	: {
			'height'	: document.getCoordinates().height,
			'width'		: document.getCoordinates().width,
			'position'	: 'absolute',
			'top'		: 0,
			'left'		: 0,
			'z-index'	: 50
		},
		'events'		: {
			'click'		: closePass
		}
	}).inject(document.body, 'top');

	masqueappear = new Fx.Tween (masque, { duration: 800 }).set('opacity', 0);
	masqueappear.start('opacity', 0, 0.8);

	var logpopHeight = $('lostPassContent').getSize().y;
	var logpopLeft = (window.getSize().x - 300) / 2;
	var logpopTop = '180px';
	
	$('lostPassContent').setStyles({
		'position'	: 'absolute',
		'display'	: 'block',
		'left'		: logpopLeft,
		'top'		: logpopTop,
		'z-index'	: 80
	});

	lostPassAppear = new Fx.Tween ($('lostPassContent'), { duration: 800 }).set('opacity', 0);
	lostPassAppear.start('opacity', 0, 1);

 	var lostPass = function(){
		if ($('lostPassForm')){
			$('lostPassForm').addEvent('submit', function(toto) {
				toto.stop();
				var passAj = new Request({
					url			: 'newPass.php',
					evalScripts	: true,
					onSuccess	: function(responseTxt){
						$('lostPassAj').set('html', responseTxt);
						lostPass(); //un petit coup de récursivité, frémissement de plaisir...
					}
				}).post($('lostPassForm'));
			});
		}
	}
	lostPass();

	if ($('lostCloseP')) {
		$('lostCloseP').addEvent('click', function(e){
			e.stop();
			closePass();
		});
	}
}

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

	//mot de passe perdu
	if ($('lostPassLink')) {
		$('lostPassLink').addEvent('click', function(e){
			e.stop();
			voirLostPass();
		});
	}

	//contenu alternatif du menu Flash
	if ($('menuFlashAltNav')){
		var ziLinks = $$('.altNavLink');
		ziLinks.each(function(el, i){
			var altLinkFx = new Fx.Morph(el, { link: 'cancel', duration: 400, transition: Fx.Transitions.Sine.easeOut, fps: 100 } );
			el.addEvents({
				'mouseenter'	: function(){
					altLinkFx.start({'font-size': '20px', 'color': '#d42727'});
				},
				'mouseleave'	: function(){
					altLinkFx.start({'font-size': '11px', 'color': '#ffffff'});
				}
			});
		});
	}

	//accordéon des actus
	if ($('homeActusBg')){
		var actusAcc = new Accordion($$('.tabsLink'), $$('.actuAcc'), { opacity: false, duration: 1000, transition: Fx.Transitions.Sine.easeOut });
	}

	//nav par onglets, ne lancer que s'il y a au moins deux onglets
	if ($$('.tabsLink').length > 1){

		var onglets		= $$('.tabsLink');
		var transp		= 0;

		var ongletsNav	= function(){

			var ongletNum	= onglets.length - 1;

			onglets.each(function(el, i){

				//les fonctions dans des variables
				var fadeout	= function(){ el.getChildren().fade(transp); }
				var fadein	= function(){ el.getChildren().fade(1); }
				var changeState	= function(){
					el.removeClass('tabOff');
					el.addClass('tabOn');
					el.removeEvent('mouseleave', fadeout);
					for (j = 0; j <= ongletNum; j ++){
						if (j != i){
							if (onglets[j].hasClass('tabOn')){
								onglets[j].removeClass('tabOn');
								onglets[j].getChildren().fade(transp);
								onglets[j].addClass('tabOff');
							}
						}
					}
				}

				//et les athéniens s'atteignent enfin
				el.addEvent('click', function(e){
					e.preventDefault();
				});

				if (el.hasClass('tabOff')){
					el.getChildren().set('opacity', transp);

					el.addEvents({
						'mouseenter'	: fadein,
						'click'			: function(){
							changeState();
							(ongletsNav).delay(500); //récursivité toujours, attention, ça frise l'hédonisme
						},
						'mouseleave'	: fadeout
					});
				}
			});
		}
		ongletsNav();
	}

	//page contacts
	if ($('contactWrapper')) {
		var contactScroll	= new Fx.Scroll($('contactWrapper'), { duration: 1500, wheelStops: false, transition: Fx.Transitions.Circ.easeOut } );
		var scrollTrig		= $$('.tabsLink');
		var scrollElements	= $$('.scrollElement');

		//contactScroll.toElement(scrollElements[0]);

		scrollTrig.each(function(el, i){
			el.addEvent('click', function(){
				contactScroll.toElement(scrollElements[i]);
			});
		});
	}
	
	//le même pour la page équipe
	if ($('teamWrapper')) {
		var teamScroll	= new Fx.Scroll($('teamWrapper'), { duration: 1500, wheelStops: false, transition: Fx.Transitions.Circ.easeOut } );
		var scrollTrig		= $$('.tabsLink');
		var scrollElements	= $$('.scrollElement');

		//teamScroll.toElement(scrollElements[0]);

		scrollTrig.each(function(el, i){
			el.addEvent('click', function(){
				teamScroll.toElement(scrollElements[i]);
			});
		});
	}

	//le même pour la page parrainage
	if ($('parrainageWrapper')) {
		var scrollTrig		= $$('.tabsLink');
		var scrollElements	= $$('.scrollElement');
		var heightFx	= new Fx.Tween($('parrainageWrapper'), { duration: 1200, transition: Fx.Transitions.Circ.easeOut, fps: 200 });

		var parrainScroll	= new Fx.Scroll($('parrainageWrapper'), {
			duration: 1500,
			wheelStops: false,
			transition: Fx.Transitions.Circ.easeOut,
			onComplete: function(i){
				
			}
		});

		//teamScroll.toElement(scrollElements[0]);
		scrollTrig.each(function(el, i){
			el.addEvent('click', function(){
				parrainScroll.toElement(scrollElements[i]);
				(function(){
					var wrapState	= $('parrainageWrapper').getCoordinates().height;
					var elemHeight 	= scrollElements[i].getScrollSize().y;
	
					if (elemHeight > wrapState){
						heightFx.start('height', elemHeight);
					}
					else if (elemHeight <= wrapState){
						heightFx.start('height', elemHeight);
					}
				}).delay(1500);
			});
		});
	}
	
	//page participation, triple salto arrière...
	if ($('partWrapper')) {

		//mettre la fiche hors champ d'entrée de jeu
		$('partFiche').setStyle('left', 860);

		var ficheMove		= new Fx.Tween($('partFiche'), { duration: 800, transition: Fx.Transitions.Circ.easeOut });

		//le bouton fermer de la fiche
		$('closeFiche').addEvent('click', function(e){
			e.stop();
			fichesL.removeClass('selecFiche');
			ficheMove.start('left', 860);
			(function(){ $('ficheContent').empty(); }).delay(800);
		});

		var scrollTrig		= $$('.tabsLink');
		var scrollElements	= $$('.partScrollElem');

		var partScroll	= new Fx.Scroll($('partWrapper'), {
			duration	: 2000,
			wheelStops	: false,
			transition	: Fx.Transitions.Circ.easeOut
		});

		scrollTrig.each(function(el, i){
			el.addEvent('click', function(){

				if ($('partFiche').getStyle('left').toInt() != 860){
					fichesL.removeClass('selecFiche');
					ficheMove.start('left', 860);
					(function(){ $('ficheContent').empty(); }).delay(800);
				}

				var elem = scrollElements[i];
				partScroll.toElement(elem);

				/*(function(){
					var heightFx	= new Fx.Tween($('partWrapper'), { duration: 1200, transition: Fx.Transitions.Circ.easeOut, fps: 200 });
					var wrapHeight	= 250;
					var wrapState	= $('partWrapper').getCoordinates().height;
					var elemHeight 	= elem.getScrollSize().y;

					if ( (elemHeight > 250) && (wrapState == 250) ){
						heightFx.start('height', elemHeight);
					}
					else if ( (elemHeight <= 250) && (wrapState > 250) ){
						heightFx.start('height', 250);
					}
				}).delay(2000);*/
			});
		});

		if ($$('.ficheLink').length > 0) {

			var fichesL = $$('.ficheLink');
			var fichesM	= fichesL.length - 1;

			var Ajaxerie = function(ajData){
				var CalAjax	= new Request({
					url			: 'ajaxFiche.php',
					method		: 'get',
					evalScripts	: true,
					onRequest	: function(){
						$('ficheContent').empty();
					},
					onSuccess	: function(responseText){
						$('ficheContent').set('html', responseText);
					}
				}).send(ajData);
			}

			fichesL.each(function(el, i){
				el.addEvents({
					'click': function(e){
						e.stop();
						//mettre le lien en cinémascope couleurs
						el.addClass('selecFiche');
						for (j = 0; j <= fichesM; j ++){
							if (j != i){
								fichesL[j].removeClass('selecFiche');
							}
						}

						//anim et remplissage de la Fiche
						var elUrl 	= el.getProperty('href');
						var ajData	= elUrl.split('?')[1].split('#')[0];
						if ($('partFiche').getStyle('left').toInt() != 860){
							ficheMove.start('left', 860);
							(function(){ Ajaxerie(ajData); }).delay(800);
						}
						else {
							Ajaxerie(ajData);
						}
					},
					'dblclick': function(e){
						e.stop();
					}
				})
			});
		}
	}
	
	//page investisseurs, même genre en nettement plus cool...
	if ( ($('fileWrapper')) && ($$('.fileScrollElem').length > 1) ) {

		var scrollTrig		= $$('.tabsLink');
		var scrollElements	= $$('.fileScrollElem');

		var fileScroll	= new Fx.Scroll($('fileWrapper'), {
			duration	: 1500,
			wheelStops	: false,
			transition	: Fx.Transitions.Sine.easeOut
		});

		scrollTrig.each(function(el, i){
			el.addEvent('click', function(){

				var elem = scrollElements[i];
				fileScroll.toElement(elem);

				/*(function(){
					var heightFx	= new Fx.Tween($('fileWrapper'), { duration: 800, transition: Fx.Transitions.Circ.easeOut, fps: 200 });
					var wrapHeight	= 250;
					var wrapState	= $('fileWrapper').getCoordinates().height;
					var elemHeight 	= elem.getScrollSize().y;

					if ( (elemHeight > 250) && (wrapState == 250) ){
						heightFx.start('height', elemHeight);
					}
					else if ( (elemHeight <= 250) && (wrapState > 250) ){
						heightFx.start('height', 250);
					}
				}).delay(1500);*/
			});
		});
	}
	
	//page équipe hors flash
	if ($$('.teamLink').length > 0) {

		var ziTeam		= $$('.teamLink');
		var teamLength	= ziTeam.length;

		ziTeam.each(function(el, i){
			el.addEvent('click', function(e){
				e.stop();
				el.addClass('selecTeam');
				for (j = 0; j < teamLength; j ++){
					if (j != i)
					ziTeam[j].removeClass('selecTeam');
				}
				var elUrl 	= el.getProperty('href');
				var ajData	= elUrl.split('?')[1];
				
				var CalAjax	= new Request({
					url			: 'teamContent.php',
					method		: 'get',
					evalScripts	: true,
					onRequest	: function(){
						$('equipeAjax').empty();
					},
					onSuccess	: function(responseText){
						$('equipeAjax').set('html', responseText);
					}
				}).send(ajData);
			});
		});
	}


});//ends domready