/* 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);
	}
});

/* Menu V4 */
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);
		item.link.addClass('hover');
		if (item.submenu){
			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){
			item.link.removeClass('hover');
			if (item.submenu){
				item.submenu.morph.start(this.options.morph.hideStyles).chain(function(){item.submenu.setStyle('display','none');}.bind(this));
			}
		}
	}
});

/* Zoombox V1 */
var Zoombox = new Class({
	Implements : Options,
	options: {
		selectorOut : '.zoombox-out',
		selectorIn : '.zoombox-in',
		morphOut : {
			opacity:0,
			position:'absolute',
			'z-index':1000,
			'margin-left':0,
			'margin-top':0
		},
		morphIn : {
			opacity:1
		},
		morph : {
			duration : 100
		},
		position : 4, // 0:top left, 1:top center, 2:top right, 3:center left, 4:center, 5:center right, 6:bottom left, 7:bottom center, 8:bottom right
		zoom : 2
	},
	initialize : function(element,options){
		this.element = $(element);
		this.setOptions(options);
		this.imageOut = this.element.getElement('img');
		this.imageOut.size = this.imageOut.getSize();
		this.options.morphOut = $merge({width:this.imageOut.size.x,height:this.imageOut.size.y},this.options.morphOut,new Fx.CSS().search(this.options.selectorOut));
		this.imageIn = new Element('IMG',{
			styles : {opacity:0.01,width:'auto',height:'auto'},
			src : this.element.href,
			morph : this.options.morph,
			events : {load : this.onImageLoad.bind(this)}
		}).inject(this.element,'top');
	},
	zoomIn: function(){
		this.imageIn.morph(this.options.morphIn);
	},
	zoomOut: function(){
		this.imageIn.morph(this.options.morphOut);
	},
	getScale: function(){
		return {width:this.imageOut.size.x*this.options.zoom,height:this.imageIn.size.y*this.imageOut.size.x*this.options.zoom/this.imageIn.size.x};
	},
	getPosition: function(){
		return {
			'margin-left':(2-this.options.position%3)*(this.imageOut.size.x-this.imageOut.size.x*this.options.zoom)/2,
			'margin-top':(2-this.options.position/3)*(this.imageOut.size.y-this.imageIn.size.y*this.imageOut.size.x*this.options.zoom/this.imageIn.size.x)/2
		};
	},
	onImageLoad : function(){
		this.imageIn.size = this.imageIn.getSize();
		//console.info(this.imageIn.size);
		this.options.morphIn = $merge(this.getScale(),this.getPosition(),this.options.morphIn,new Fx.CSS().search(this.options.selectorIn));
		//console.info(this.options.morphIn);
		this.element.addEvents({mouseenter:this.zoomIn.bind(this),mouseleave:this.zoomOut.bind(this)});
		this.imageIn.setStyles(this.options.morphOut);
	}
});

/* Initialisation */
window.addEvent('domready',function(){
	/* Galerie */
	$$('.gallery.slide').each(function(e){
		new Gallery(e,{
			auto: true,
			delay : 5000,
			navLinks : null,
			tween : {
				duration : 400
			},
			autoHide : false
		});
	});
	/* Menu */
	new Menu('menu');
	/* Zoombox */
	$$('.zoombox,.thumbnail .gallery-icon a').each(function(zoombox){
		new Zoombox(zoombox,eval('({'+zoombox.rev+'})'));// Parse REV attribute as zoombox options
	});
});	

