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

/* Déclaration */

/* Gallery V15 */
/* TODO Recode to handle variable image width with SLIDE effect */
var SLIDE = 0, FADE = 1;
var Gallery = new Class({
	Implements : Options,
	options : {
		container : 'div',
		list : 'ul',
		items : 'li',
		prev : '.prev',
		next : '.next',
		auto : true,
		step : 1,
		delay : 3000,
		loop : true,
		links : 'ul.nav li a',
		linksEvent : 'click',
		linksRel : false,
		play : '.play',
		autoHide : true,
		tween : {'link':'cancel'},
		effect : SLIDE, // SLIDE | FADE
		pause : true
	},
	initialize : function(element,options){
		this.element = document.id(element);
		this.setOptions(options);
		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();
				if (this.options.pause) 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)
				});
			}
		};
		if (this.options.effect==SLIDE){
			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);
		}else{
			this.zIndex = this.items.length+1;
			for(var i=0;i<this.items.length;i++){
				this.items[i].set('tween',this.options.tween);
				this.items[i].setStyles({'position':'absolute','z-index':this.items.length-i});
			}
		}
		if (typeof(Slimbox)!='undefined') Slimbox.scanPage();
		if (this.links.length>0){
			this.links.each(function(link,j){
				link.addEvent(this.options.linksEvent,function(event){
					event.stop();
					this.stop();
					if (this.options.linksRel) j = this.getIndexByLink(link);
					this.scrollTo(j);
				}.bind(this));
			}.bind(this));
			this.link = this.links[0];
			this.link.addClass('selected');
		}
	},
	getIndexByLink : function(link){
		for(var i=0;i<this.items.length;i++){
			if (this.items[i].id==link.rel) return i;
		}
		return 0;
	},
	getLinkByIndex : function(j){
		if (this.options.linksRel){
			for (var i=0;i<this.links.length;i++){
				if (this.links[i].rel==this.items[j].id) return this.links[i];
			}
		}else{
			this.link = this.links[j];
		}
		return this.link;
	},
	scrollBy : function (direction){
		this.scrollTo(this.i+direction*2-1);
		return false;
	},
	scrollTo : function (index,callback){
		if (this.link) this.link.removeClass('selected');
		this.i = index;
		if (this.options.effect==SLIDE){
			this.list.tween.start('margin-left',-this.itemWidth * (this.i+this.items.length) * this.options.step).chain(function(){
				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;
					if (callback) callback();
				}
			}.bind(this));
		}else if (this.options.effect==FADE){
			this.i = (this.i+this.maxStep) % this.maxStep;
			this.items[this.i].setStyles({'z-index':this.zIndex,'opacity':0});
			this.items[this.i].tween('opacity',1);
			this.zIndex++;
		}
		this.link = this.getLinkByIndex((this.i+this.maxStep) % this.maxStep);
		if (this.link) this.link.addClass('selected');
	},
	start : function(){
		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);
	}
});

/* PngFix V3 */
// TODO copy standard border style (border-radius style raise an error on IE)
var PngFix = new Class({
	initialize : function (element){
		var images = document.id(element||document.body).getElements('img');
		images.each(function(img){
			if (img.src.toUpperCase().contains('.PNG')){
				var span = new Element('span', {
					'class': img.className+' img',
					'styles': {
						'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+img.src+'\', sizingMethod=\'scale\')',
						'display': 'block',
						'width': img.width,
						'height': img.height
					}
				});
				span.setStyles(img.getStyles('position','top','left','z-index','width','height','padding','margin','text-align','vertical-align'));
				span.replaces(img);
			}
		});
	}
});

/* Follow V3 */
var Follow = new Class({
	Implements : Options,
	options : {
		container : 'content',
		debug : false,
		delta : 0,
		toTop : false
	},
	initialize : function(element,options){
		this.element = $(element);
		this.setOptions(options);
		this.marginOffset = this.element.getStyle('margin-top').toInt();
		this.topOffset = this.element.getPosition().y;
		this.bottomOffset = $(this.options.container).getStyle('height').toInt();
		this.screenHeight = window.getSize().y;
		window.addEvent('scroll',this.follow.bind(this));
		if (this.options.debug)	this.debug = new Element('div',{styles:{position:'fixed',top:0,left:0,background:'#fff'}}).inject($(document.body));
	},
	follow : function(){
		var elementHeight = this.element.getStyle('height').toInt();
		var screenTop = window.getScrollTop()-this.topOffset+this.options.delta;
		var screenBottom = screenTop+this.screenHeight;
		var elementTop = this.element.getStyle('margin-top').toInt()+this.element.getStyle('border-top-width').toInt();
		if (this.options.debug) this.debug.innerHTML = 'marginOffset='+this.marginOffset+', topOffset='+this.topOffset+', bottomOffset='+this.bottomOffset+', screenHeight='+this.screenHeight+', elementTop='+elementTop+', elementHeight='+elementHeight+',screenTop='+screenTop+',screenBottom='+screenBottom;
		if (this.options.toTop || elementHeight<this.screenHeight || elementTop>screenTop){
			this.element.tween('margin-top',Math.max(Math.min(screenTop,this.bottomOffset-elementHeight),0)+this.marginOffset);
		}else if (elementTop<screenBottom-elementHeight && elementTop<this.bottomOffset-elementHeight){
			this.element.tween('margin-top',Math.max(Math.min(screenBottom-elementHeight,this.bottomOffset-elementHeight),0)+this.marginOffset);
		}
	}
});

/* Initialisation */
window.addEvent('domready',function(){
	/* Gallery slide */
	$$('.gallery.slide').each(function(gallery){
		new Gallery(gallery,{effect:SLIDE,delay:10000,autoHide:false});
	});
	/* PngFix */
	if (Browser.Engine.trident4) new PngFix();
	/* Follow */
	new Follow('sidebar');
});

