Event.observe(window, 'load', function() {

	// 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 _corusel1matiere = $$('div.carousel1MatiereUnique');
		if (_corusel1matiere) {
			_corusel1matiere.wrapper = 'div.carousel-hold1';
			_corusel1matiere.ul = 'ul';
			_corusel1matiere.lis = 'ul li';
			_corusel1matiere.links = $$('#num1 a');
			initCoruselUneMatiere(_corusel1matiere);
		}

		var _corusel = $$('div.carousel1');
		if (_corusel) {
			_corusel.wrapper = 'div.carousel-hold1';
			_corusel.ul = 'ul';
			_corusel.lis = 'ul li';
			_corusel.links = $$('#num1 a');
			_corusel.prev = 'a.prev';
			_corusel.next = 'a.next';
			_corusel.duration = .5;
			initCorusel(_corusel);
		}
		var _corusel2 = $$('div.carousel2');
		if (_corusel2) {
			_corusel2.wrapper = 'div.carousel-hold2';
			_corusel2.ul = 'ul';
			_corusel2.lis = 'ul li';
			_corusel2.links = $$('#num2 a');
			_corusel2.prev = 'a.prev';
			_corusel2.next = 'a.next';
			_corusel2.duration = .5;
			initCorusel(_corusel2);
		}
		var _corusel3 = $$('div.carousel3');
		if (_corusel3) {
			_corusel3.wrapper = 'div.carousel-hold3';
			_corusel3.ul = 'ul';
			_corusel3.lis = 'ul li';
			_corusel3.links = $$('#num3 a');
			_corusel3.prev = 'a.prev';
			_corusel3.next = 'a.next';
			_corusel3.duration = .5;
			initCorusel(_corusel3);
		}
		var _corusel4 = $$('div.carousel4');
		if (_corusel4) {
			_corusel4.wrapper = 'div.carousel-hold4';
			_corusel4.ul = 'ul';
			_corusel4.lis = 'ul li';
			_corusel4.links = $$('#num4 a');
			_corusel4.prev = 'a.prev';
			_corusel4.next = 'a.next';
			_corusel4.duration = .5;
			initCorusel(_corusel4);
		}
		var _corusel5 = $$('div.carousel5');
		if (_corusel5) {
			_corusel5.wrapper = 'div.carousel-hold5';
			_corusel5.ul = 'ul';
			_corusel5.lis = 'ul li';
			_corusel5.links = $$('#num5 a');
			_corusel5.prev = 'a.prev';
			_corusel5.next = 'a.next';
			_corusel5.duration = .5;
			initCorusel(_corusel5);
		}
		var _corusel6 = $$('div.carousel6');
		if (_corusel6) {
			_corusel6.wrapper = 'div.carousel-hold6';
			_corusel6.ul = 'ul';
			_corusel6.lis = 'ul li';
			_corusel6.links = $$('#num6 a');
			_corusel6.prev = 'a.prev';
			_corusel6.next = 'a.next';
			_corusel6.duration = .5;
			initCorusel(_corusel6);
		}
		var _corusel7 = $$('div.carousel7');
		if (_corusel7) {
			_corusel7.wrapper = 'div.carousel-hold7';
			_corusel7.ul = 'ul';
			_corusel7.lis = 'ul li';
			_corusel7.links = $$('#num7 a');
			_corusel7.prev = 'a.prev';
			_corusel7.next = 'a.next';
			_corusel7.duration = .5;
			initCorusel(_corusel7);
		}

		var _corusel8 = $$('div.carousel8');
		if (_corusel8) {
			_corusel8.wrapper = 'div.carousel-hold8';
			_corusel8.ul = 'ul';
			_corusel8.lis = 'ul li';
			_corusel8.links = $$('#num8 a');
			_corusel8.prev = 'a.prev';
			_corusel8.next = 'a.next';
			_corusel8.duration = .5;
			initCorusel(_corusel8);
		}

		var _corusel9 = $$('div.carousel9');
		if (_corusel9) {
			_corusel9.wrapper = 'div.carousel-hold9';
			_corusel9.ul = 'ul';
			_corusel9.lis = 'ul li';
			_corusel9.links = $$('#num9 a');
			_corusel9.prev = 'a.prev';
			_corusel9.next = 'a.next';
			_corusel9.duration = .5;
			initCorusel(_corusel9);
		}

		var _corusel0 = $$('div.gallery-holder');
		if (_corusel0) {
			_corusel0.wrapper = 'div.gallery';
			_corusel0.ul = 'ul';
			_corusel0.lis = 'ul li';
			_corusel0.links = false;
			_corusel0.prev = 'a.prev';
			_corusel0.next = 'a.next';
			_corusel0.duration = .5;
			initCorusel(_corusel0);
		}
		
		if(window.autocheckmatiere) {
			autocheckmatiere();
		}
		
		// accordion ***************************************************************
		if (typeof accordion == 'function') {
			var _accordionBlock = $$('div.accordion');
			_accordionBlock.each(function(ac){
				var _accordion = new accordion(ac, {
					classNames : {
						toggle : 'opener',
						toggleActive : 'state-active',
						content : 'slide'
					}
				});
			});
		}


		// tabs ********************************************************************
		initTabs();
		SelectTabs('tab1',false);

	});

	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 = _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;

					if (_lis[k].innerHTML==''){
						//newItem.addClassName('li-vide');
						newItem.className='li-vide';
					}

					/* Ajout Jerome Longet */
					if (_c.wrapper != 'div.gallery'){

						// Récupere l'id du premier lien pour retrouver l'id de la matière à sélectionner
						var lesliens = newItem.getElementsByTagName("a");
						link=lesliens[0];
						if (link){
							// Ajoute le div qui affiche le check
							newItem.innerHTML = '<div id="check-matiere-'+ $(link).rel +'" class="matiere-activation"></div>'+_lis[k].innerHTML;
						}

						// Pour chaque lien du div
						for (i=0; i<lesliens.length; i++) {
							link=lesliens[i];

							$(link).href='javascript:void(0)';

							if ($(link).title){
								new Tip($(link), $(link).title,{
									style: 'protogrey',
									hook: { target: 'bottomRight', tip: 'topLeft' },
									stem: { position: 'leftMiddle', width: 14, height: 14 },
									offset: { x: 1, y: -160 },
									width: 250,
									border: 2,
									radius: 2,
									background: '#908D87'
								}
								);
							}
							$(link).title='';

							// Ajoute fonction Onclick
							link.onclick = function(){
								idlink=this.rel;
								// Supprime la class de selection sur le div qui affiche le check
								$('voir-les-couleurs-disponibles').select('div.matiere-activation').each(
									function(divmatiere, i){
										$(divmatiere.id).removeClassName('matiere-checked');
									}
								)

								$('chkcouleur-' + idlink).checked=true;
								$('check-matiere-' + idlink).addClassName('matiere-checked');

								new Ajax.Updater('zonephoto',
								'includes/photo_article.asp',
								{method: 'get',
									parameters: 'ajax=1&acod='+$('aCod').value+'&idmat='+idlink
									,evalScripts:true
								});

								window.location.hash="#";
								
								pageTracker._trackEvent('Fiche produit', ' sélection de couleur');
							}

						}
					}
					/* Fin Ajout Jerome Longet */

					_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');
					}
				}

			}
		});
	};


	function initCoruselUneMatiere(_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 = _c.links;

				for (var k=0; k<_lis.length; k++) {
					newItem=_lis[k];

					/* Ajout Jerome Longet */
					if (_c.wrapper != 'div.gallery'){

						// Récupere l'id du premier lien pour retrouver l'id de la matière à sélectionner
						var lesliens = newItem.getElementsByTagName("a");
						link=lesliens[0];

						if (link){
							// Ajoute le div qui affiche le check
							newItem.innerHTML = '<div id="check-matiere-'+ $(link).rel +'" class="matiere-activation"></div>' + newItem.innerHTML;
						}

						// Pour chaque lien du div
						for (i=0; i<lesliens.length; i++) {
							link=lesliens[i];

							$(link).href='javascript:void(0)';

							if ($(link).title){
								new Tip($(link), $(link).title,{
									style: 'protogrey',
									hook: { target: 'bottomRight', tip: 'topLeft' },
									stem: { position: 'leftMiddle', width: 14, height: 14 },
									offset: { x: 1, y: -160 },
									width: 250,
									border: 2,
									radius: 2,
									background: '#908D87'
								}
								);
							}
							$(link).title='';

							// Ajoute fonction Onclick
							link.onclick = function(){
								idlink=this.rel;
								// Supprime la class de selection sur le div qui affiche le check
								$('voir-les-couleurs-disponibles').select('div.matiere-activation').each(
								function(divmatiere, i){
									$(divmatiere.id).removeClassName('matiere-checked');
								}
								)

								$('chkcouleur-' + idlink).checked=true;
								$('check-matiere-' + idlink).addClassName('matiere-checked');

								new Ajax.Updater('zonephoto',
								'includes/photo_article.asp',
								{method: 'get',
									parameters: 'ajax=1&acod='+$('aCod').value+'&idmat='+idlink
									,evalScripts:true
								});

								window.location.hash="#";
							}

						}
					}
					/* Fin Ajout Jerome Longet */
				}
			}
		});		
	};

	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";
									pageTracker._trackEvent('Fiche produit', this.href.substr(this.href.indexOf("#") + 1) );
									return false;
								}
							}
						}
					}
				}
			}
		}