Event.observe(window, 'load', function() {
	
	addLinkerEvents();
	
	// slideshow ***************************************************************
	var _slideShowBlock = $$('.slideshow');
	var _fadeDuration = 1.5;
	var _switchTime = 8;
	
	_slideShowBlock.each(function(sb,i){
		var _slideEl = sb.select('div > img');
		var _current = 0;
		
		_slideEl.each(function(slEl, j){
			if (!slEl.hasClassName('active')) {
				slEl.setStyle({'display':'none','opacity':'0'});	
			} else {_current = j;}
		});
		
		function nextSlide(){
			_current += 1;
			if (_current >= _slideEl.length) _current = 0;
			
			var _active = sb.select('.active')[0];
			new Effect.Morph(_active, {
				style: {'opacity':'0'},
				duration: _fadeDuration,
				afterFinish:function(){
					_active.setStyle({'display':'none'});
				}
			});
			_active.removeClassName('active');
			_slideEl[_current].addClassName('active');
			_slideEl[_current].setStyle({'display':'block'});
			
			new Effect.Morph(_slideEl[_current], {
				style: {'opacity':'1'},
				duration: _fadeDuration
			});
		}
		
		setInterval(function(){
			nextSlide();
		},_switchTime*1000)
	});
	
	// corusel *****************************************************************
	
	var _corusel = $$('div.carousel-columns .carousel');
	if (_corusel) {
		_corusel.wrapper = 'div.slider-holder';
		_corusel.ul = 'ul';
		_corusel.lis = 'ul li';
		_corusel.links = '#nav a';
		_corusel.prev = 'a.prev';
		_corusel.next = 'a.next';
		_corusel.duration = 1.5;
		initCorusel(_corusel);
	}
	var _coruselExemples = $$('div.more-photos .carousel');
	if (_coruselExemples) {
		_coruselExemples.wrapper = 'div';
		_coruselExemples.ul = 'ul';
		_coruselExemples.lis = 'ul li';
		_coruselExemples.links = '#nav a';
		_coruselExemples.prev = 'a.prev';
		_coruselExemples.next = 'a.next';
		_coruselExemples.duration = 1.5;
		initCorusel(_coruselExemples);
	}
	var _coruselCat = $$('div.category-content div.carousel');
	if (_coruselCat) {
		_coruselCat.wrapper = 'div.slide-width';
		_coruselCat.ul = 'div.slide-width ul';
		_coruselCat.lis = 'div.slide-width ul li';
		_coruselCat.links = 'ul.bullets a';
		_coruselCat.prev = 'a.prev';
		_coruselCat.next = 'a.next';
		_coruselCat.duration = 1.5;
		_coruselCat.nocircle = true;
		initCorusel(_coruselCat);
	}
	
	function initCorusel(_c) {
		_c.each(function(corusel, i){
			var _wraper = corusel.select(_c.wrapper)[0];
			if (_wraper) {
				var _lis = corusel.select(_c.lis);
				var _holder = corusel.select(_c.ul)[0];
				var _links = corusel.select(_c.links);
				var _prev = corusel.select(_c.prev)[0];
				var _next = corusel.select(_c.next)[0];
				var _step = _wraper.offsetWidth;
				var _animated = false;
				var _duration = _c.duration;
				var _maxMargin = _lis.length* (_lis[0].offsetWidth + parseInt(_lis[0].getStyle('marginLeft')) + parseInt(_lis[0].getStyle('marginRight'))) - _step;
				var _length = Math.floor(_maxMargin/_step)+1;
				var _current = 0;
				var _margin = 0;

				// clone items
				for (var m=0; m<2; m++)
				for (var k=0; k<_lis.length; k++) {
					var newItem = document.createElement("li");
					newItem.innerHTML = _lis[k].innerHTML;
					_holder.appendChild(newItem);
				}
				var _slideWidth = _lis[0].offsetWidth + parseInt(_lis[0].getStyle('marginLeft')) + parseInt(_lis[0].getStyle('marginRight'));
				var _sumWidth = _lis.length * _slideWidth;
				var _animationFlag = false;
				_holder.setStyle({'marginLeft':-_sumWidth+'px'});
				_maxMargin += _sumWidth;


				if (_maxMargin%_step == 0) _length -= 1;
				if (_links.length) {
					_links.each(function(link, i){
						link.onclick = function(){
							//_prev.removeClassName('prev-disable');
							_p = true;
							//_next.removeClassName('next-disable');
							_n = true;
							if (!_animated) {
								_links[_current].removeClassName('active');
								_margin = _sumWidth + _step*i;

								//if (i >= _length) _margin = _maxMargin;
								new Effect.Morph(_holder, {
									style: {'marginLeft': -(_margin)+'px'},
									duration: _duration
								});
								_current = i;
								this.addClassName('active');
								if (_c.nocircle) {
									if (_current+1 > _length) {
										_n = false;
										//_next.addClassName('next-disable');
									}
									if (_current-1 < 0) {
										_p = false;
										//_prev.addClassName('prev-disable');
									}
								}
							}		
							return false;
						};
					});
				}


				var _n = true;
				_next.onclick = function(){
					//_prev.removeClassName('prev-disable');
					_p = true;
					if (_n && !_animationFlag) {
						if (_links.length && _links[_current]) {_links[_current].removeClassName('active');}
						_current += 1;
						if (_current > _length && _links.length && _links[0]) _links[0].addClassName('active');
						_margin = _sumWidth + _step*_current;
						if (_current == _length) {
							_margin = _maxMargin;
						} else if (_current > _length) {
							_margin = _sumWidth*2;
						}
						_animationFlag = true;
						new Effect.Morph(_holder, {
							style: {'marginLeft': -(_margin)+'px'},
							duration: _duration,
							afterFinish: function(){
								if (_current > _length) {
									_holder.setStyle({'marginLeft':-_sumWidth+'px'});
									_current = 0;
									if (_links.length && _links[_current]) {_links[_current].addClassName('active');}
									_n = true;
								}
								_animationFlag = false;
							}
						});
						if (_links.length && _links[_current]) {_links[_current].addClassName('active');}
						
						if (_c.nocircle) {
							if (_current > _length) {
								_n = false;
								//this.addClassName('next-disable');
							}
						}
					}
					return false;
				}
				var _p = true;
				_prev.onclick = function(){

					//_next.removeClassName('next-disable');
					_n = true;
					if (_p || true) {
						if (_links.length && _links[_current]) {_links[_current].removeClassName('active');}
						_current -= 1;
						if (_current < 0 && _links.length && _links[_length]) _links[_length].addClassName('active');
						_margin = _sumWidth + _step*_current;
						if (_current == 0) {
							_margin = _sumWidth;
						} else if (_current < 0) {
							_margin = _sumWidth - _step;
						}
						_animationFlag = true;

						new Effect.Morph(_holder, {
							style: {'marginLeft': -(_margin)+'px'},
							duration: _duration,
							afterFinish: function(){
								if (_current < 0) {
									_holder.setStyle({'marginLeft':-_maxMargin+'px'});
									_current = _length;
									_p = true;
									if (_links.length && _links[_current]) {_links[_current].addClassName('active');}
								}
								_animationFlag = false;
							}
						});
						if (_links.length && _links[_current]) {_links[_current].addClassName('active');}
						
						if (_c.nocircle) {
							if (_current-1 < 0) {
								_p = false;
								//this.addClassName('prev-disable');
							}
						}
					}
					return false;
				}
				
				if (_c.nocircle) {
					if (_current-1 < 0) {
						_p = false;
						//_prev.addClassName('prev-disable');
					}
				}
			}
		});
	};
	
	// corusel popups **********************************************************
	var _popupLink = $$('a.popupLink');
	var _popupCarousel = $$('div.popup-carousel');
	var _activePopup = null;
	var _popupHideTimer = null;
	
	_popupLink.each(function(link, i){
		var _id = link.href.substr(link.href.indexOf('#')+1);
		
		link.onmouseover = function(){
			if (_popupHideTimer) clearTimeout(_popupHideTimer);
			if (_activePopup) _activePopup.setStyle({'display':'none'});
			
			$(_id).setStyle({'display':'block'});
			_activePopup = $(_id);
		};
		link.onmouseout = function(){
			_popupHideTimer = setTimeout(function(){
				$(_id).setStyle({'display':'none'});
			}, 200)
		};
	});
	_popupCarousel.each(function(popup, i){
		popup.onmouseover = function(){
			if (_popupHideTimer) clearTimeout(_popupHideTimer);
		};
		popup.onmouseout = function(){
			_popupHideTimer = setTimeout(function(){
				_activePopup.setStyle({'display':'none'});
			}, 200)
		};
	});
	
	// accordion ***************************************************************
	if (typeof accordion == 'function') {
		var _accordionBlock2 = $$('div.accordion li ul');
		_accordionBlock2.each(function(ac){
			var _accordion = new accordion(ac, {
				classNames : {
					toggle : 'sub-opener',
					toggleActive : 'active',
					content : 'slide'
				}
			});
		});
		
		var _accordionBlock = $$('div.accordion');
		_accordionBlock.each(function(ac){
			var _accordion = new accordion(ac, {
				classNames : {
					toggle : 'opener',
					toggleActive : 'state-active',
					content : 'slide'
				}
			});
		});
	}
	
	// accordion popups ********************************************************
	var _accordionLi = $$('div.accordion li li li');
	_accordionLi.each(function(li){
		li.onmouseover = function(){
			li.addClassName('hover');
		};
		li.onmouseout = function(){
			li.removeClassName('hover');
		};
	});
	
	// Gestion des popups du niveau superieur
	var _accordionLi2 = $$('div.accordion li li.popup-hover');
	_accordionLi2.each(function(li){
		li.onmouseover = function(){
			li.addClassName('hover');
		};
		li.onmouseout = function(){
			li.removeClassName('hover');
		};
	});


				
	// ajax photos *************************************************************
	var _buttonZoom = $$('a.button-zoom');
	if (_buttonZoom.length) {
		_buttonZoom.each(function(bt){
			bt.onclick = function(){
				var _id = this.rel;
				var _idhrefmedia = this.id;

				if ($(_id) == null) {
					
					var _thisLink = this;
					var _href = this.href;
					var _ajax = new Ajax.Request( _href, { method: 'get', 
						onSuccess: function(transport) {
							var response = new String(transport.responseText);
							new Insertion.Bottom(document.body, response);

							// Modifie la position du PopUp							
							popupSlideshow($(_id));							
							
						}.bind(this),
						onException: function(instance, exception){
							throw('Loading Error: ' + exception);
						}
					});
					
				} else {
					
					// Modif JJ
					// Selectionne l'image en cours
					// Relance le slide
					var _slideZoom = $(_id).select('div.slides > img');
					_slideZoom.each(function(slEl, j){
						
						slEl.removeClassName('active');
						popupSlideshow($(_id));
						
						if (slEl.id==_idhrefmedia){
							slEl.addClassName('active');
							slEl.setStyle({'display':'block','opacity':'100'});
						}
						popupSlideshow($(_id));
					});	
				
					// FIN Modif JJ
					
					$(_id).setStyle({'display':'block'});
				}
				
				return false;	
			}
		});
	}
	
	// popup slideshow *********************************************************
	
	function popupSlideshow(_obj){
		var _slideEl = _obj.select('div.slides > img');
		var _current = 0;
		var _next = _obj.select('a.next')[0];
		var _prev = _obj.select('a.prev')[0];
		var _close = _obj.select('a.close-btn')[0];
		
		_slideEl.each(function(slEl, j){
			if (!slEl.hasClassName('active')) {
				slEl.setStyle({'display':'none','opacity':'0'});	
			} else {_current = j;}
		});
		
		_close.onclick = function(){
			_obj.setStyle({'display':'none'});
			return false;
		};		
		_next.onclick = function(){
			nextSlide();
			return false;
		};
		_prev.onclick = function(){
			prevSlide();
			return false;
		};
		
		function nextSlide(){
			_current += 1;
			if (_current >= _slideEl.length) _current = 0;
			
			var _active = _obj.select('.active')[0];
			new Effect.Morph(_active, {
				style: {'opacity':'0'},
				duration: _fadeDuration,
				afterFinish:function(){
					_active.setStyle({'display':'none'});
				}
			});
			_active.removeClassName('active');
			_slideEl[_current].addClassName('active');
			_slideEl[_current].setStyle({'display':'block'});
			
			new Effect.Morph(_slideEl[_current], {
				style: {'opacity':'1'},
				duration: _fadeDuration
			});
		}
		function prevSlide(){
			_current -= 1;
			if (_current < 0 ) _current = _slideEl.length - 1;
			
			var _active = _obj.select('.active')[0];
			new Effect.Morph(_active, {
				style: {'opacity':'0'},
				duration: _fadeDuration,
				afterFinish:function(){
					_active.setStyle({'display':'none'});
				}
			});
			_active.removeClassName('active');
			_slideEl[_current].addClassName('active');
			_slideEl[_current].setStyle({'display':'block'});
			
			new Effect.Morph(_slideEl[_current], {
				style: {'opacity':'1'},
				duration: _fadeDuration
			});
		}
	}

	// tabs corusel' ***********************************************************
	var _curTab = 0;
	var _marginTab = 0;
	var _nextTab = $('nextTab');
	var _prevTab = $('prevTab');
	
	if (_nextTab && _prevTab) {
		var _tabHolder = $('tabHolder')
		var _tabWidth = [];
		var _width = 0;
		var _maxIndex = 0;
		var _tabs = $$('div.tabset li');
		_tabs.each(function(tab, i){
			if (i == 0)_tabWidth[i] = 0;
			else {
				_width += _tabs[i-1].offsetWidth;
				_tabWidth[i] = _width;
			}
		});
		if (_width < 450) {
			_prevTab.hide();
			_nextTab.hide();
		}
		var _max = _tabWidth[_tabWidth.length-1] - 504;
		_tabs.each(function(tab, i){
			if (_tabWidth[i+1] > _max) _maxIndex = i;
		});
		_prevTab.hide();
		_nextTab.onclick = function(){
			_prevTab.show();
			_curTab += 1;
			if (_curTab > _tabWidth.length-1) _curTab = 0;
			_marginTab = _tabWidth[_curTab];
			if (_marginTab > _max) {
				_nextTab.hide();
			}
			new Effect.Morph(_tabHolder, {
				style: {'marginLeft': -(_marginTab)+'px'},
				duration: 0.5
			});
			return false;
		};
		_prevTab.onclick = function(){
			_nextTab.show();
			_curTab -= 1;
			if (_curTab < 0) _curTab = _tabWidth.length-1;
			_marginTab = _tabWidth[_curTab];
			
			if (_marginTab <= 0) {_prevTab.hide();}
			
			new Effect.Morph(_tabHolder, {
				style: {'marginLeft': -(_marginTab)+'px'},
				duration: 0.5
			});
			return false;
		};
	}
	
	// tabs ********************************************************************
	initTabs();
	if (document.getElementById("tabs-block")){
	$('tabs-block').style.visibility='visible';
	}

});

function initTabs()
{
	var sets = document.getElementsByTagName("ul");
	for (var i = 0; i < sets.length; i++)
	{
		if (sets[i].className.indexOf("tabset") != -1)
		{
			var tabs = [];
			var links = sets[i].getElementsByTagName("a");
			for (var j = 0; j < links.length; j++)
			{
				if (links[j].className.indexOf("tab") != -1)
				{
					tabs.push(links[j]);
					links[j].tabs = tabs;
					var c = document.getElementById(links[j].href.substr(links[j].href.indexOf("#") + 1));
					//reset all tabs on start
					if (c) if (links[j].className.indexOf("active") != -1) c.style.display = "block";
					else c.style.display = "none";
					links[j].onclick = function ()
					{
						var c = document.getElementById(this.href.substr(this.href.indexOf("#") + 1));
						if (c)
						{
							//reset all tabs
							for (var i = 0; i < this.tabs.length; i++)
							{
								document.getElementById(this.tabs[i].href.substr(this.tabs[i].href.indexOf("#") + 1)).style.display = "none";
								this.tabs[i].className = this.tabs[i].className.replace("active", "");
							}
							this.className += " active";
							c.style.display = "block";
							return false;
						}
					}
				}
			}
		}
	}
}
	
//	// tabs corusel' ***********************************************************
//	var _curTab = 0;
//	var _marginTab = 0;
//	var _nextTab = $('nextTab');
//	var _prevTab = $('prevTab');
//	
//	if (_nextTab && _prevTab) {
//		var _tabHolder = $('tabHolder')
//		var _tabWidth = [];
//		var _width = 0;
//		var _maxIndex = 0;
//		var _tabs = $$('div.tabset li');
//		_tabs.each(function(tab, i){
//			if (i == 0)_tabWidth[i] = 0;
//			else {
//				_width += _tabs[i-1].offsetWidth;
//				_tabWidth[i] = _width;
//			}
//		});
//		var _max = _tabWidth[_tabWidth.length-1] - 504;
//		_tabs.each(function(tab, i){
//			if (_tabWidth[i+1] > _max) _maxIndex = i;
//		});
//		_prevTab.hide();
//		_nextTab.onclick = function(){
//			_prevTab.show();
//			_curTab += 1;
//			if (_curTab > _tabWidth.length-1) _curTab = 0;
//			_marginTab = _tabWidth[_curTab];
//			if (_marginTab > _max) {
//				_nextTab.hide();
//			}
//			new Effect.Morph(_tabHolder, {
//				style: {'marginLeft': -(_marginTab)+'px'},
//				duration: 0.5
//			});
//			return false;
//		};
//		_prevTab.onclick = function(){
//			_nextTab.show();
//			_curTab -= 1;
//			if (_curTab < 0) _curTab = _tabWidth.length-1;
//			_marginTab = _tabWidth[_curTab];
//			
//			if (_marginTab <= 0) {_prevTab.hide();}
//			
//			new Effect.Morph(_tabHolder, {
//				style: {'marginLeft': -(_marginTab)+'px'},
//				duration: 0.5
//			});
//			return false;
//		};
//	}
//	
//	// tabs ********************************************************************
//	initTabs();
//
//});
//
//function initTabs()
//{
//	var sets = document.getElementsByTagName("ul");
//	for (var i = 0; i < sets.length; i++)
//	{
//		if (sets[i].className.indexOf("tabset") != -1)
//		{
//			var tabs = [];
//			var links = sets[i].getElementsByTagName("a");
//			for (var j = 0; j < links.length; j++)
//			{
//				if (links[j].className.indexOf("tab") != -1)
//				{
//					tabs.push(links[j]);
//					links[j].tabs = tabs;
//					var c = document.getElementById(links[j].href.substr(links[j].href.indexOf("#") + 1));
//					//reset all tabs on start
//					if (c) if (links[j].className.indexOf("active") != -1) c.style.display = "block";
//					else c.style.display = "none";
//					links[j].onclick = function ()
//					{
//						var c = document.getElementById(this.href.substr(this.href.indexOf("#") + 1));
//						if (c)
//						{
//							//reset all tabs
//							for (var i = 0; i < this.tabs.length; i++)
//							{
//								document.getElementById(this.tabs[i].href.substr(this.tabs[i].href.indexOf("#") + 1)).style.display = "none";
//								this.tabs[i].className = this.tabs[i].className.replace("active", "");
//							}
//							this.className += " active";
//							c.style.display = "block";
//							return false;
//						}
//					}
//				}
//			}
//		}
//	}
//}
