
// JavaScript Document for Affaires fiscalité

	/* William Jautée 
		http://www.seyartdesign.fr
	*/

/* Menu de navigation */
var affSousMenu = function(elts){
	$$(elts).each(function(elt){
		if (! elt.getParent('li').getElement('ul'))
			return;
		var timer;
		var sousmenu = elt.getParent('li').getElement('ul')
		var effect =  new Fx.Tween(sousmenu, {duration: 200, link : 'cancel' });
		//Test si élément actif
		if (elt.get('class')=='rubprincip active'){
			elt.addEvents({
				'activedisappear': function(){	effect.start('opacity',[1,0.2])},
				'activeappear':function(){	effect.start('opacity',[0.2,1])}
			});
			return;
		}
		elt.addEvents({
			'mouseover': afficherMenu,
			'stop': cacheMenu
		});
		function afficherMenu(){
			sousmenu.set('opacity',0);
			sousmenu.setStyle('display','block');
			effect.start('opacity',[0,1]);
			// Style en plus
			if (elt.get('class')!='rubprincip active')
				elt.setStyle('border-bottom','3px solid #7b6e5c');
			elt.getElement('span').setStyle('display','block');
			$$(elts).each(function(l){
				if (l!=elt)
					l.fireEvent('stop');	
			});
			sousmenu.addEvent('mouseout',declanchetimer);
			this.addEvent('mouseout',declanchetimer);
			// fireEvent activedisappear pour que le sous menu de l'élément actif disparaisse (si risque de superposition de sous menu)
			if ((elt.getParent('li').getNext('li') && elt.getParent('li').getNext('li').getElement('a').hasClass('active')) ||  elt.getParent('li').getPrevious('li').getElement('a').hasClass('active'))
				$$('.rubprincip.active').fireEvent('activedisappear');
		}
		function declanchetimer() {
			elt.removeEvent('mouseover',afficherMenu);
			elt.addEvent('mouseover',arretertimer);
			sousmenu.addEvent('mouseover',arretertimer);
			timer = setInterval(cacheMenu, 500);
		};
		function cacheMenu() {
			arretertimer();
			var op= sousmenu.getStyle('opacity');
				effect.start('opacity',[op,0]);
			// Retrait Style en plus
			elt.setStyle('border-bottom','none');
			if (elt.get('class')!='rubprincip active')
				elt.getElement('span').setStyle('display','none');
				
			elt.addEvent('mouseover',afficherMenu);
			elt.removeEvent('mouseout',declanchetimer);
			sousmenu.removeEvent('mouseout',declanchetimer);
			// fireEvent activedisappear pour que le sous menu de l'élément actif réapparaisse
			if ($$('.rubprincip.active').getParent('li').getElement('ul').get('opacity')<=0.2)
				$$('.rubprincip.active').fireEvent('activeappear');
		}
		function arretertimer(){
			clearInterval(timer);
			elt.removeEvent('mouseover',arretertimer);
			sousmenu.removeEvent('mouseover',arretertimer);
		}
		
	});
	// Affichage du sous-menu si élement actif
	$$('.rubprincip.active').getParent('li').getElement('ul').setStyles({'display':'block','opacity':1,'visibility':'visible','z-index':1});
}

/* Fonction nouvelle fenêtre */
var nouvelleFenetre = function(ev) {
	ev.stop();
	if (ev.target.tagName == 'A') {
		lien=ev.target.href;
	} else {
		lien=ev.target.getParent('a').href;
	}
	window.open(lien);
}

/* Fonction Slider */
var partSlider = function(){ 
	// Declaring increment vars
	var nbElementLi=$('slider-list').getElements('li').length;
	var nbElementLiAffiche=5;
	var totIncrement = 0;
	var increment = ($('slider-list').getParent('div').getStyle('width').toInt()/nbElementLiAffiche);
	var maxRightIncrement = increment*(-(nbElementLi-nbElementLiAffiche));
	// FX var
    var fx = new Fx.Tween($('slider-list'), {duration: 500, transition: 'back:in:out'});
	if ($('previous')) {
    	$('previous').addEvent('click', function(ev) {
			ev.stop();
        	if(totIncrement < 0) {
            	totIncrement += increment;
            	fx.start('margin-left', totIncrement);
        	}
    	});
	}
	if ($('next')) { // rajout du if sinon erreur sur les autres pages
    	$('next').addEvent('click', function(ev) {
			ev.stop();
        	if(totIncrement > maxRightIncrement) {
           		totIncrement -= increment;
            	fx.start('margin-left', totIncrement);
        	}
    	});
	}
}

/* Galerie témoignages code de base tiré de "Create a Simple Slideshow Using MooTools " par David Walsh */
var slideShow = function(){
	/* settings */
	var showDuration = 10000;
	var container = $('containertemoignage');
  
	var images = container.getElements('div.bloctemoignage');
	var maxH=container.getElement('div.bloctemoignage').getSize().y.toInt();
	var currentIndex = 0;
	var interval;
	/* opacity and fade */
	images.each(function(img,i){ 
	  	if(i > 0) {
			img.set('opacity',0);
	  	}
		maxH = img.getSize().y.toInt() > maxH ? img.getSize().y.toInt() : maxH;
	});
	container.setStyle('height',maxH+"px");
	/* worker */
	var show = function() {
	  	images[currentIndex].fade('out');
	  	images[currentIndex = currentIndex < images.length - 1 ? currentIndex+1 : 0].fade('in');
	};
	/*Start et Show */
	var start = function() { 
		interval = show.periodical(showDuration);
	};
	var stop = function() {
		clearInterval(interval); 
	};
  
   /* Start/Stop aux passages de la souris */
	container.addEvents({
		mouseenter: function() { stop(); },
		mouseleave: function() { start(); }
	});
  
	/* start once the page is finished loading */
	window.addEvent('load',function(){
	  	start();
	});
}


