window.addEvent('domready', function(){
	document.getElement('body').removeClass('no-js').addClass('js');
	
	// TICKERS
	//if($('actualites')) new ScrollTicker($('actualites').getElement('.scroll'));
	if($('actualites')) new Ticker($('actualites').getElement('.actualites'), {nextText: 'Actualité suivante ›'});
	if($('nouveautes')) new Ticker($('nouveautes').getElement('.nouveautes'), {nextText: 'Nouveauté suivante ›'});
	
	// CARTE
	swfobject.embedSWF('/swf/carte.swf', 'carte', '214', '225', '9.0.0', '/swf/expressInstall.swf');
	
	// GD H3
	$$('#corps h3').each(function(item, i){
		if(!item.hasClass('no-gd')){
			item.addClass('gd');
			if(a = $E('a', item)) item = a;
			item.getChildren().each(function(child){
				child.remove();
			});
			var text = item.getText().clean();
			item.empty();
			new Element('img', {src: '/img/titres/h3/'+encodeURIComponent(text)+'.png', width: 520, height: 32, alt: text}).injectInside(item);
			new Element('span').setText(text).injectInside(item);
		}
	});
	
	// GD H4
	$$('#sidebar .bloc-a h4').each(function(item, i){
		item.addClass('gd');
		if(a = $E('a', item)) item = a;
		item.getChildren().each(function(child){
			child.remove();
		});
		var text = item.getText().clean();
		new Element('img', {src: '/img/titres/h4/'+encodeURIComponent(text)+'.png', width: 170, height: 18, alt: text}).injectInside(item.empty());
	});
	
	// PLUS DE CRITERES
	var lien = $('lien-plus-de-criteres');
	var slide = new Fx.Slide($('plus-de-criteres'), {
		onStart: function(){
			if(!this.open) lien.getElement('span').set('text', 'moins');
			else lien.getElement('span').set('text', 'plus');
		}
	});
	if(!$('plus-de-criteres').hasClass('show')) slide.hide();
	else lien.getElement('span').set('text', 'moins');
	lien.addEvent('click', function(event){
		event = new Event(event).stop();
		slide.toggle();
	});
	
	// ACCES RAPIDE GAMMES
	if($('acces-rapide-gammes')) $('acces-rapide-gammes').getElement('select').addEvent('change', function(){
		this.form.submit();
	});
	
	// INFOS PRODUITS
	var accordions = new Array();
	$$('.informations').each(function(item, i){
		accordions[i] = new Accordion(item.getElements('h5'), item.getElements('.contenu'), {
			alwaysHide: true,
			display: 'none',
			onActive: function(toggler, element){
				element.getParent().morph('.alt');
			},
			onBackground: function(toggler, element){
				element.getParent().morph('.information');
			}	
		});
	});
	
	if(window.ie){
		if(window.getSize().y.toInt() < window.getScrollSize().y.toInt()) document.getElement('body').addClass('relative-footer');
		else document.getElement('body').removeClass('relative-footer');
	}
});

var Ticker = new Class({
	options: {
		duration: 250,
		nextText: 'Suivant'
	},

	initialize: function(list, options){
		this.setOptions(options);
		this.list = $(list);
		this.items = this.list.getChildren();
		this.effect = new Fx.Morph(this.list, {duration: this.options.duration});
		if(this.items.length > 1){
			this.next();
			var that = this;
			new Element('a', {text: this.options.nextText, href: '#', events: {click: function(event){
				event = new Event(event).stop(); 
				that.effect.start({
					opacity: 0
				}).chain(that.next.bind(that));
			}}}).inject(new Element('p', {'class': 'navigation'}).inject(this.list, 'after'));
		}
	},
	
	next: function(){
		this.items.setStyle('display', 'none');
		var last = this.list.getLast();
		last.setStyle('display', 'block').injectTop(this.list);
		var height = last.getStyle('height').toInt()+last.getStyle('padding-top').toInt()+last.getStyle('padding-bottom').toInt();
		this.effect.start({
			opacity: 1,
			height: height+'px',
			overflow: 'hidden'
		});
	}
});

Ticker.implement(new Events, new Options);

var ScrollTicker = new Class({
	options: {
		timeout: 5000,
		duration: 1000,
		transition: Fx.Transitions.Quad.easeInOut
	},

	initialize: function(element, options){
		this.setOptions(options);
		this.element = $(element);
		this.items = this.element.getChildren()[0].getChildren();
		this.numItems = this.items.length;
		this.current = 0;
		this.scroll = new Fx.Scroll(this.element, {
			wheelStops: false,
			wait: false,
			duration: this.options.duration,
			transition: this.options.transition
		});
		this.scroll.toTop();
		this.periodical = this.next.periodical(this.options.timeout, this);
		
		that = this;
		this.element.addEvents({
			mouseenter: function(){
				$clear(that.periodical);
			},
			mouseleave: function(){
				that.periodical = that.next.periodical(that.options.timeout, that);
			},
			mousewheel: function(event){
				event = new Event(event);
				if(event.wheel > 0) that.previous(); // UP
				else if(event.wheel < 0) that.next(); // DOWN
				event.stop();
			}
		});
		
		this.controls = new Element('ul', {'class': 'controles'});
		this.controlsPrevious = new Element('a', {href: '#', title: 'Actualité précédente', events: {click: function(event){
			event = new Event(event).stop();
			that.previous();
		}}}).setText('Précédent').injectInside(new Element('li', {'class': 'precedent'}).injectInside(this.controls));
		this.controlsNext = new Element('a', {href: '#', title: 'Actualité suivante', events: {click: function(event){
			event = new Event(event).stop();
			that.next();
		}}}).setText('Suivant').injectInside(new Element('li', {'class': 'suivant'}).injectInside(this.controls));
		this.controls.injectInside(element);
	},
	
	next: function(){
		if(this.current < this.numItems-1) this.current++;
		else this.current = 0;
		this.scroll.toElement(this.items[this.current]);
	},
	
	previous: function(){
		if(this.current > 0) this.current--;
		else this.current = this.numItems-1;
		this.scroll.toElement(this.items[this.current]);
	}
});

ScrollTicker.implement(new Events, new Options);