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

/* PngFix v1.0 */
var PngFix = new Class({
	initialize : function (element){
		var images = $(element||document.body).getElements('img');
		images.each(function(img){
			if (img.src.toUpperCase().contains('.PNG')){
				var span = new Element('span', {
					'styles': {
						'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+img.src+'\', sizingMethod=\'scale\')',
						'display': 'block',
						'width': img.width,
						'height': img.height
					}
				});
				span.replaces(img);
			}
		});
	}
});


/* Initialisation */
window.addEvent('domready',function(){
	/* Initialise la galerie */
	$$('.gallery.slide').each(function(e){
		new Gallery(e,{
			auto: false,
			delay : 6000,
			navLinks : null,
			tween : {
				duration : 1000
			},
			autoHide : false
		});
	});
	/* Initialise le menu */
	new Menu('menu');
	/* Initialisation PngFix */
	if (Browser.Engine.trident4) new PngFix();
});

