// menu
/*window.addEvent('domready', function() {
	//récupération menu courant
	// if( $('number') ) {
		// var number = parseInt($('number').get('text'));
	// }
	// else{
		// number = 'none';
	// }
	var myAccordion = new Accordion($('menu'), 'span.toggler', 'ul.elements', {
		opacity: false,
        //show: -1,
        alwaysHide: true
	});

});*/

if( $('accordion') ) {
window.addEvent('domready', function() {
	var myAccordion2 = new Accordion($('accordion'), 'div.toggler', 'div.elements', {
		opacity: false,
        //show: -1,
        alwaysHide: true
	});

});
}

var scroll;
function fAccordion() {
// if( $('content') ) { alert('aa'); }
// if( $$('toggler') ) { alert('bb'); }
// if( $$('elements') ) { alert('bb'); }
    //récupération menu courant
    if( $('number') ) {
        var number = parseInt($('number').get('text'));
    }
    else{
        number = 'none';
    }
    
	var scroll = new Fx.Scroll(window, {duration: 800, transition: Fx.Transitions.Quad.easeOut, fps: 60});
	var accordion = new Accordion('.toggler', '.elements', {
		duration: 500,
		show: number,
		opacity: false,
		onActive: function(toggler, element){
			//toggler.addClass('backSlider');
			//(function() {scroll.toElement(toggler);}).bind(toggler).delay(600);
			(function() {
				element.setStyles({'overflow': 'hidden', 'height': 'auto'});
			}).bind(element).delay(600);
		},
		onBackground: function(toggler, element){
			//toggler.removeClass('backSlider');
			element.setStyles({'height': element.getSize().y + 'px'});
			element.setStyles({'overflow': 'hidden'});
		}
	});
}

function fSlide() {
if( $$('.elements2') && $$('.toggler2') ) {
	var slides = [];
	$$('.elements2').each(function(el, index){
		slides[index] = new Fx.Slide(el, {wait: true}).hide();
        // alert(index);
	});
    
    // $$('.elements2').each(function(el, index){
        // var children = el.getChildren();
        // var urls = [];
        // children.each(function(el2, index){
            // urls[index] = el2.getProperty('href');
            // alert(el2.getProperty('href'));
        // });
    // });
	
	$$('.toggler2').each(function(el, index){
		el.addEvent('click', function(e){
            $$('.toggler2').each(function(el2, index2){
                slides[index2].hide();
            });
			var e = new Event(e).stop();
			if (slides[index]) {
				slides[index].toggle();
                // el.toggleClass('readWorkOpen');
				// if (el.hasClass('readWorkOpen')) {
					// el.removeClass('readWork');
				// } else {
					// el.addClass('readWork');
				// }
			}
		});
	});
    
    if( $('numsousmenu') ) {
        var numslide = parseInt($('numsousmenu').get('text'))-1;
        //alert( numslide );
        slides[numslide].toggle();
    }
    // slides[1].toggle();
}
}

function fFontSize() {
	var body = $(document).getElement('body');
	var cookie = Cookie.read('DocumentFontSize');
    var defaultSize = 0.6;
	
	if (body && cookie) {
        // console.log('yy');
		body.setStyle('font-size', Cookie.read('DocumentFontSize').toFloat()+"em");
		Cookie.write('DocumentFontSize', Cookie.read('DocumentFontSize').toFloat(), {duration: 30, path:'/'});
	}
    else{
        // console.log('zz');
        body.setStyle('font-size', defaultSize+"em");
		Cookie.write('DocumentFontSize', defaultSize, {duration: 30, path:'/'});
    }
}

function fFontSizeUp() {
	var body = $(document).getElement('body');
	if (body) {	
		var size = fGetFontSize();
		if (size < 1.1) {
			size = size + 0.1;
		}
		body.setStyle('font-size', size+"em");
		Cookie.write('DocumentFontSize', size, {duration: 30, path:'/'});
	}
}

function fFontSizeDown() {
	var body = $(document).getElement('body');
	if (body) {	
		var size = fGetFontSize();
		if (size > 0.6) {
			size = size - 0.1;
		}
		body.setStyle('font-size', size+"em");
		Cookie.write('DocumentFontSize', size, {duration: 30, path:'/'});
	}
}

function fGetFontSize() {
	var body = $(document).getElement('body');
	var size = 0.6;
	
	if (body) {
		size = body.getStyle('font-size').toFloat();
        // console.log(size);
		if (0.6 > size && size < 1.1) {
			size = 0.6;
		}
	}
	
	return size;
}

window.addEvent('domready', fFontSize);

function fInit() {
    if( $('menu') ) {
        fSlide();
        fAccordion();
    }

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

function showMap() {
if($('recherche-geo')) {
	//Ajout d'une image transparente sur laquelle on réapplique la map pour éviter le flicker au rollover
	var map_overlay  = new Element('img', {id: 'map-overlay', src: 'images/bgd_map_overlay.gif', usemap: '#Map', alt: ''});
	$('cont-map').adopt(map_overlay);
	$$('#cont-map div').setStyle('opacity','0');
	
		$$('#cont-map area').each(function(el){
			el.addEvents( {
				'mouseenter' : function(e){
					var id_map = el.get('class');
					$$('#cont-map #'+ id_map).fade('in');
				},
				'mouseleave' : function(e){
					var id_map = el.get('class');
					$$('#cont-map #'+ id_map).fade('out');
				}
			});	
		});	
}
if($('implantations-geo')) {
	$$('#cont-map-detail .tip-tick').setStyle('opacity','0');
		
		$$('#cont-map-detail area').each(function(el){
			el.addEvents( {
				'mouseenter' : function(e){
					var id_map = el.get('class');
					$$('#cont-map-detail #'+ id_map).fade('in');
				},
				'mouseleave' : function(e){
					var id_map = el.get('class');
					$$('#cont-map-detail #'+ id_map).fade('out');
				}
			});	
		});	
}
}
window.addEvent('domready', showMap);


function initSlideshow() {
	if($('slideshow-home')) {
		/* settings */
		  var showDuration = 5000;
		  var container = $('slideshow-home');
		  var images = container.getElements('li');
		  var currentIndex = 0;
		  var interval;
		  /* opacity and fade */
		  images.each(function(img,i){ 
			if(i > 0) {
			  img.set('opacity',0);
			  img.getChildren('span').set('opacity',0);
			}
		  });
		  /* worker */
		  var show = function() {
			images[currentIndex].set('tween', {duration: 1000});
			images[currentIndex].getChildren('span').set('tween', {duration: 1000});
			
			images[currentIndex].tween('opacity', '0'); 
			images[currentIndex].getChildren('span').tween('opacity', '0')
			
			images[currentIndex = currentIndex < images.length - 1 ? currentIndex+1 : 0].set('tween', {onComplete: function(){
				images[currentIndex].getChildren('span').set('tween', {duration: 1000});
				images[currentIndex].getChildren('span').tween('opacity', '1');  								
			}, duration:1000}).tween('opacity', '1');
			
			/*images[currentIndex].fade('out');
			images[currentIndex].getChildren('span').fade('out');
			images[currentIndex = currentIndex < images.length - 1 ? currentIndex+1 : 0].set('tween', {onComplete: function(){
				images[currentIndex].getChildren('span').fade('in');								
			}}).fade(1);*/
		  };
		  /* start once the page is finished loading */
		  window.addEvent('load',function(){
			interval = show.periodical(showDuration);
		  });
	}
}
window.addEvent('domready', initSlideshow);

sfHover = function() {
	var sfEls = document.getElementById("home-nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

// function fAutoCompleteCommunes(){
	// if($('commune-search')){
		// var indicatorL = $('commune-search');		
		// var completerL = new Autocompleter.Ajax.Json($('commune-search'), 'js/cities.js.php', {
			// 'minLength': 3,
			// 'postVar': 'city',
			// 'inheritWidth': false,
			// 'maxChoices': 18,
			// 'onRequest': function(el){
				// indicatorL.addClass('autocompleter-loading');
			// },
			// 'onComplete': function(el){
				// indicatorL.removeClass('autocompleter-loading');
			// }
		// });
	// }
// }
