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

/* Déclaration */

/* Gallery photo */
var Gallery = function (cssGallery){
	var gallery = $$(cssGallery);
	gallery.each(function(g){
		var photos = g.getElements("ul > li");
		var effets = new Array();
		photos.i = 0;
		photos.each(function(photo,i){
			effets[i] = new Fx.Morph(photo, {duration:1000, wait:false});
			effets[i].set({
				'margin-left' : [(i>0)?550:0]
			});
		});
		var afficher = function(i,sens){
			effets[i].set({
				'margin-left' : [1100*sens-550]
			});
			effets[i].start({
				'margin-left' : [0]
			});
		};
		var cacher = function(i,sens){
			effets[i].start({
				'margin-left' : [1100*sens-550]
			});
		};
		g.getElement('a.prev').addEvent('click', function(event){
			cacher(photos.i,1);
			photos.i = (photos.i+photos.length-1) % photos.length;
			afficher(photos.i,0);
		});
		g.getElements('img,a.next').addEvent('click', function(event){
			cacher(photos.i,0);
			photos.i = (photos.i+1) % photos.length;
			afficher(photos.i,1);
		});
	});
}

/* Menu V4.0 */
var Menu = new Class({
	Implements : Options,
	options : {
		showMinLevel : 0,
		hideDelay : 300,
		morph : {
			showStyles : {opacity:1},
			hideStyles : {opacity:0},
			options : {link:'cancel',duration:200}
		}
	},
	initialize: function(menu,options){
		this.setOptions(options);
		this.items = $(menu).getChildren('li');
		this.items.each(function(item,i){
			item.i = i;
			item.link = item.getElement('a');
			item.link.addEvent('focus',this.showItem.bind(this,[item]));
			item.submenu = item.getElement('ul');
			if (this.options.showMinLevel>0){
				if (item.submenu) item.submenu.setStyle('display','block');
			}else{
				item.addEvents({mouseenter:this.showItem.bind(this,[item]),mouseleave:this.hideItem.bind(this,[item])});
			}
			if (item.submenu){
				item.submenu.morph = new Fx.Morph(item.submenu,this.options.morph.options);
				var options = $merge(this.options);
				options.showMinLevel = options.showMinLevel-1;
				new Menu(item.submenu,options);
			}
		}.bind(this));
	},
	showItem : function(item){
		this.timer = $clear(this.timer);
		if (item.submenu){
			item.link.addClass('hover');
			item.submenu.morph.start(this.options.morph.showStyles).chain(function(){item.submenu.setStyle('display','block');}.bind(this));
		}
		this.items.each(function(otherItem,j){
			if (item.i!=j) this.hideItemNow(otherItem);
		}.bind(this));
	},
	hideItem : function(item){
		this.timer = $clear(this.timer);
		this.timer = this.hideItemNow.bind(this,[item]).delay(this.options.hideDelay);
	},
	hideItemNow : function(item){
		if (item.link){
			if (item.submenu){
				item.link.removeClass('hover');
				item.submenu.morph.start(this.options.morph.hideStyles).chain(function(){item.submenu.setStyle('display','none');}.bind(this));
			}
		}
	}
});

/* Marquee V3 */
var Marquee = new Class({
	Implements : Options,
	options : {
		isVertical : false,
		speed : 1,
		pause : true,
		delay : 30
	},
	initialize : function(element,options){
		this.element = $(element);
		this.setOptions(options);
		this.content = this.element.getFirst();
		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};
		Number.prototype.mod = function(n) {return ((this%n)+n)%n;}
		if (this.options.pause){
			this.element.addEvents({
				mouseenter : this.stop.bind(this),
				mouseleave : this.start.bind(this)
			});
		}
		this.start();
	},
	start : function (){
		this.thread = this.scroll.periodical(this.options.delay,this);
	},
	stop : function (){
		$clear(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);
		}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);
		}
	}
});

/* Initialisation */
window.addEvent('domready', function() {
	/* Menu */
	new Menu('menu');
	/* Gallery */
	new Gallery('.gallery.slide');
	/* Marquee */
	$$('.marquee').each(function(m){
		new Marquee(m,{speed:2,delay:30});
	});
});

