/****************************/
/* slideshows               */
/****************************/
function initSlideShows()
{
	if($('bgd_quoi_de_neuf')) {
		var navs = $$('#bgd_quoi_de_neuf #slideshow-nav li').getElements('a');
		
		var navSlideShow = new SlideShow('header-slideshow', {
			autoplay: false,
			 transition: 'pushLeft',
			 onShow: function(data){
				// update navigation elements' class depending upon the current slide
				navs[data.previous.index].removeClass('active');
				navs[data.next.index].addClass('active');
			}
		});
		navs.each(function(item, index){
			// click a nav item ...
			item.addEvent('click', function(event){
				event.stop();
				// pushLeft or pushRight, depending upon where
				// the slideshow already is, and where it's going
				var transition = (navSlideShow.index < index) ? 'pushLeft' : 'pushRight';
				// call show method, index of the navigation element matches the slide index
				// on-the-fly transition option
				navSlideShow.show(index, {transition: transition});
			});
		});
	}
	if($('bgd_en_bref')) {
		var navs = $$('#bgd_en_bref #slideshow-nav li').getElements('a');
		
		var navSlideShow = new SlideShow('header-slideshow', {
			autoplay: false,
			 transition: 'pushLeft',
			 onShow: function(data){
				// update navigation elements' class depending upon the current slide
				navs[data.previous.index].removeClass('active');
				navs[data.next.index].addClass('active');
			}
		});
		navs.each(function(item, index){
			// click a nav item ...
			item.addEvent('click', function(event){
				event.stop();
				// pushLeft or pushRight, depending upon where
				// the slideshow already is, and where it's going
				var transition = (navSlideShow.index < index) ? 'pushLeft' : 'pushRight';
				// call show method, index of the navigation element matches the slide index
				// on-the-fly transition option
				navSlideShow.show(index, {transition: transition});
			});
		});
	}
	if($('bgd_actu')) {
		var navs2 = $$('#bgd_actu #slideshow-actu-nav li').getElements('a');
		
		var navSlideShow2 = new SlideShow('actu-slideshow', {
			autoplay: false,
			 transition: 'pushLeft',
			 onShow: function(data){
				// update navigation elements' class depending upon the current slide
				navs2[data.previous.index].removeClass('active');
				navs2[data.next.index].addClass('active');
			}
		});
		navs2.each(function(item, index){
			// click a nav item ...
			item.addEvent('click', function(event){
				event.stop();
				// pushLeft or pushRight, depending upon where
				// the slideshow already is, and where it's going
				var transition = (navSlideShow2.index < index) ? 'pushLeft' : 'pushRight';
				// call show method, index of the navigation element matches the slide index
				// on-the-fly transition option
				navSlideShow2.show(index, {transition: transition});
			});
		});
	}
	
	/*
	if($('bgd_calendrier')) {
		var Tabs = new Class({
			Extends: SlideShow,
			options: {
				selector: '.tab',
				tabSelector: '.navs li img',
				transition: 'none',
				duration: 0
			},
			initialize: function(element, options){
				this.parent(element, options);
				this.bound = { tabClick: this.tabClick.bind(this) };
				this.navs3 = this.element.getElements(this.options.tabSelector);
				this.addEvent('show', function(data){
					this.navs3[data.previous.index].removeClass('current');
					this.navs3[data.next.index].addClass('current');
				});
				this.attach();
			},
			tabClick: function(event){
				if (this.navs3.contains(event.target)){
					event.preventDefault();
					this.show(this.navs3.indexOf(event.target));
				}
			},
			attach: function(){
				this.element.addEvent('click', this.bound.tabClick);
				return this;
			},
			detach: function(){
				this.element.removeEvent('click', this.bound.tabClick);
				return this;
			}
		});
		new Tabs('tabs-slideshow');
		
		$$('.itemHasItem .toggler').addEvents({
			'mouseenter': function(){
				this.getChildren('.tip').setStyle('display','block');
			},
			'mouseleave' : function(){
				this.getChildren('.tip').setStyle('display','none');
			}
		});
	}*/
}
/****************************/
/* equalizer               */
/****************************/
function initEqualizer()
{
	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;
		}
	});

	var columnizer = new Equalizer('.desc').equalize('height'); //call .equalize() as often as you want!
	var columnizer = new Equalizer('.news h3').equalize('height'); //call .equalize() as often as you want!
	var columnizer = new Equalizer('.news').equalize('height'); //call .equalize() as often as you want!
	var columnizer = new Equalizer('.news .center').equalize('height'); //call .equalize() as often as you want!
	var columnizer = new Equalizer('.wrap-address').equalize('height'); //call .equalize() as often as you want!
	var columnizer = new Equalizer('.agenda .event-desc').equalize('height'); //call .equalize() as often as you want!
	var columnizer = new Equalizer('.agenda .event-desc h3').equalize('height'); //call .equalize() as often as you want!
}

/****************************/
/* cerabox               */
/****************************/
function initCeraBox()
{
	var box = new CeraBox({
		group:			true,
		errorLoadingMessage:	'Le contenu ne peut être chargé, réessayez plus tard.'
	});
	if($$('.format a.box').length) {
		box.addItems('.format a.box', {
			displayTitle: false,
			displayOverlay: false,
			clickToClose: true
		});
	}
}
/****************************/
/* menu suckerfish               */
/****************************/
sfHover = function() {
	var sfEls = document.getElementById("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);
/****************************/
/* align vertical*/
/****************************/
function initVerticalAlign() 
{
    vAlign = new Class({

	initialize: function(element){
            this.element = $(element);
        
                var div = new Element('div', {
                    'class': 'nitinh-vAlign',
                    'styles': {
                        'position': 'relative'
                    }
                });

                div.set({ 'html': this.element.get('html') })
                this.element.set({'html':''})
                div.inject(this.element);

                var ph = this.element.getSize().y;
                var dh = div.getSize().y;
                var mh = (ph - dh) / 2;
                div.set('styles',{
                    'top':mh
                });
            }
        });
       if($$('ul#nav li .second-level  a').shift()) {
           $$('ul#nav li .second-level  a').each(function(el){
                var vertical = new vAlign(el);
           });
        }
       if($$('.format .news h3').shift()) {
           $$('.format .news h3').each(function(el){
                var vertical = new vAlign(el);
           });
        }
}
/****************************/
/* initialiser le moteur de recherche */
/****************************/
function initSearchForm() 
{
	if($$('#recherche input.text')) {
		var inputSearch = $$('#recherche input.text');
		inputSearch.addEvents({
			focus : function(inputSearch) {
				this.set('value', '');
			},
			blur : function(inputSearch) {
				this.set('value', 'Mots clés');
			}
		})
	}
}

/****************************/
/* Function image hover */
/****************************/
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_i18n.", "").replace("_n.", "");
		var hover = ((image.toLowerCase().lastIndexOf("_n.") !=-1) || (image.toLowerCase().lastIndexOf("_n_i18n.") !=-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_i18n.", "_o_i18n.").replace("_n.", "_o.");
		}

		if (hover && document.imageOut && document.imageOver) {
			item.onmouseover = function(){
				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;
				/*}*/
			}
		}
	});
}
document.addEvent('domready', function(){
	initSlideShows();
	initEqualizer();
	initCeraBox();
	sfHover();
	initVerticalAlign();
	initSearchForm();
	initImages();
});
