/* Script client commun à toutes les pages du site */

/* Déclaration */

/* Gallery V7 */
/* TODO Recode to handle variable image width */
var Gallery = new Class({
	Implements : Options,
	options : {
		container : 'div',
		list : 'ul',
		items : 'li',
		prev : '.button.prev',
		next : '.button.next',
		auto : true,
		step : 1,
		delay : 3000,
		loop : true,
		links : 'ul.nav li a',
		play : '.button.play',
		autoHide : true,
		tween : {}
	},
	initialize : function(element,options){
		this.element = $(element);
		this.setOptions(options);
		this.pause = true;
		this.prev = this.element.getElement(this.options.prev);
		this.next = this.element.getElement(this.options.next);
		this.play = this.element.getElement(this.options.play);
		this.links = this.element.getElements(this.options.links);
		this.container = this.element.getElement(this.options.container);
		this.list = this.container.getElement(this.options.list);
		this.items = this.list.getElements(this.options.items);
		this.itemWidth = this.items[0].getStyle('width').toInt()+this.items[0].getStyle('padding-right').toInt()+this.items[0].getStyle('padding-left').toInt();
		this.i = 0;
		this.maxStep = Math.ceil(this.items.length/this.options.step);
		if (this.maxStep>0){
			if (this.options.auto){
				this.start();
				this.element.addEvents({'mouseenter':this.stop.bind(this),'mouseleave':this.start.bind(this)});
			}
			if (this.prev) this.prev.addEvent('click',this.scrollBy.bind(this,[0]));
			if (this.next) this.next.addEvent('click',this.scrollBy.bind(this,[1]));
			if (this.play) this.play.addEvent('click',function(){if (this.timer) this.stop(); else	this.start();}.bind(this));
			if (this.options.autoHide){
				this.element.getElements('.button').setStyle('opacity',0);
				this.element.addEvents({
					'mouseenter' : function(){
						$$(this.prev,this.next,this.play).fade('in');
					}.bind(this),
					'mouseleave' : function(){
						$$(this.prev,this.next,this.play).fade('out');
					}.bind(this)
				});
			}
		};
		// Loop V2 : Copy items twice and move to 2nd instance at startup
		if (this.options.loop){
			this.items.clone().inject(this.list);
			this.items.clone().inject(this.list);
			this.list.setStyle('margin-left',-this.items.length * this.itemWidth * this.options.step);
		}
		this.list.tween = new Fx.Tween(this.list, this.options.tween);
		this.list.setStyle('width',this.itemWidth*3*this.items.length);
		// If slimbox is used, rescan page just in case...
		if (typeof(Slimbox)!='undefined') Slimbox.scanPage();
		// Init nav menu
		if (this.links.length>0){
			this.links.each(function(link,j){
				link.addEvent('click',function(event){
					event.stop();
					this.scrollTo(j);
				}.bind(this));
				if (j==0) link.addClass('selected');
			}.bind(this));
		}
	},
	scrollBy : function (direction){
		this.scrollTo(this.i+direction*2-1);
		return false;
	},
	scrollTo : function (index){
		// Remove selected class from nav link BEFORE tween
		if (this.links.length>0){
			this.links[this.i].removeClass('selected');
		}
		this.i = index;
		this.list.tween.start('margin-left',-this.itemWidth * (this.i+this.items.length) * this.options.step).chain(function(){
			// Loop V2 : setStyle AFTER tween when loop	
			if (this.options.loop && Math.abs(this.i)>=this.maxStep){
				this.list.setStyle('margin-left',-this.items.length * this.itemWidth * this.options.step);
				this.i = (this.i+this.maxStep) % this.maxStep;
			}
		}.bind(this));
		// Add selected class to nav link DURING tween
		if (this.links.length>0){
			this.links[(this.i+this.maxStep) % this.maxStep].addClass('selected');
		}
	},
	start : function(){
		this.pause = false;
		if (this.play) this.play.addClass('stop');
		if (this.next) this.next.fireEvent('mouseenter');
		this.timer = this.scrollBy.bind(this,[1]).periodical(this.options.delay);
	},
	stop : function(){
		if (this.play) this.play.removeClass('stop');
		if (this.next) this.next.fireEvent('mouseleave');
		this.timer = $clear(this.timer);
	}
});

/* Initialisation */
window.addEvents({
	domready : function(){
		/* Cacher les champs Société et SIRET dans le formulaire de contact lorsque la case "un particulier" est cochée */
		var particulier = $('cf_field_1-1');
		if (particulier) particulier.addEvent('click',function(){$$('#li--2,#li--3').setStyle('display','none');});
		/* Afficher les champs Société et SIRET dans le formulaire de contact lorsque la case "un professionnel" est cochée */
		var professionnel = $('cf_field_1-2');
		if (professionnel) professionnel.addEvent('click',function(){$$('#li--2,#li--3').setStyle('display','block');});
		/* Effets au survol des menus */
		/*$$('#header ul.menu li a').each(function(link,i){
			if (!link.getParent().hasClass('current_page_item')){
				link.i = i;
				link.addEvents({
					'mouseover':function(){link.morph('.hover-menu-effect');},
					'mouseout':function(){link.morph('.hout-menu-effect');}
				});
			}
		});*/
		/* Initialisation gallery slide */
		$$('.gallery.slide').each(function(g){
			new Gallery(g,{delay:4000,tween:{duration:400}});
		});
		/* Initialisation coins arrondis pour IE */
		if (typeof BorderRadius != 'undefined' && Browser.Engine.trident){		
			$$('#header ul.menu li a,#content a.button,.sendbutton,#accueil .gallery ul li .gallery-caption,.carousel').borderRadius({radius:5,parentBgColor:'#fff'});
		}
	},
	load : function(){
		var carousel = $$('#accueil .gallery');
		if (carousel.length>0){
			/* Initialisation du carousel */
			new Carousel(carousel[0],{
				radiusx     :  300,
				radiusy     :  40,
				centerx     :  310, 
				centery     :  40,
				zoomIn      :  1,
			 	speed       :  0.10, 
				clickit : false
			});
			/* Affiche le texte correspondant au survol d'un item du carousel */
			var captions = $$('.gallery-caption');
			captions.fade('hide');
			$$('.carousel').each(function(carousel,i){
				carousel.caption = captions[i];
				carousel.addEvents({
					mouseenter : function(){
						carousel.caption.fade('in');
					},
					mouseleave : function(){
						carousel.caption.fade('out');
					}
				});
			});
		}
	}
});

