/* Scripts du site sois.fr */

/* 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);
		}
	}
});

/* Marquee V6 */
var Marquee = new Class({
	Implements : Options,
	options : {
		isVertical : false,
		speed : 1,
		pause : {mouse:false,interval:false,duration:0},
		delay : 30
	},
	initialize : function(element,options){
		this.element = document.id(element);
		this.setOptions(options);
		this.content = this.element.getFirst();
		Number.prototype.mod = function(n) {return ((this%n)+n)%n;}
		if (this.options.pause && this.options.pause.mouse){
			this.element.addEvents({mouseenter : this.stop.bind(this),mouseleave : this.start.bind(this)});
		}
		this.reset();
	},
	reset : function(){
		this.stop();
		this.element.size = this.element.getSize();
		this.content.size = this.content.getSize();
		this.size = {x: this.element.size.x+this.content.size.x, y: this.element.size.y+this.content.size.y};
		this.position = {x: this.element.size.x, y: this.element.size.y};
		this.start();
	},
	start : function (){
		this.thread = this.scroll.periodical(this.options.delay,this);
	},
	stop : function (){
		clearInterval(this.thread);
	},
	scroll : function (){
		if (this.options.isVertical){
			this.position.y = (this.position.y-this.options.speed+this.content.size.y).mod(this.size.y)-this.content.size.y;
			this.content.setStyle('top',this.position.y);
			if (this.options.pause && this.options.pause.interval && this.position.y%this.options.pause.interval==0) this.pause();
		}else{
			this.position.x = (this.position.x-this.options.speed+this.content.size.x).mod(this.size.x)-this.content.size.x;
			this.content.setStyle('left',this.position.x);
			if (this.options.pause && this.options.pause.interval && this.position.x%this.options.pause.interval==0) this.pause();
		}
	},
	pause : function(){
		this.stop();
		this.start.delay(this.options.pause.duration,this);
	}
});

/* Initialisation */
window.addEvent('domready',function(){
	// Menu suivant l'ascenseur (le long du contenu)
	new Follow('menu',{container:'contenu'});
	// Marquee
	$$('.gallery').each(function(g){
		new Marquee(g,{
			isVertical : true,
			pause : {mouse:true},
			delay : 50
		});
	});
});

