/* Scripts du site spabike.com */

/* Déclaration */

/* Gallery V13 */
/* 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 : '.button.prev',
		next : '.button.next',
		auto : true,
		step : 1,
		delay : 3000,
		loop : true,
		links : 'ul.nav li a',
		linksEvent : 'click',
		linksRel : false,
		play : '.button.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){
		for (var i=0;i<this.links.length;i++){
			if (this.links[i].rel==this.items[j].id) return this.links[i];
		}
		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);
	}
});

/* Initialisation */

window.addEvent('domready', function() {
	$$(".csc-textpic").each(function(e,i){
		e.getElements("a").each(function(a,j){
			a.setProperty("rel","lightbox-"+i);
		});
	});
	Slimbox.scanPage();
	$$('.gallery.slide').each(function(gallery){
		new Gallery(gallery,{
			container : '.csc-textpic',
			list : '.csc-textpic-imagewrap',
			items : 'li',
			effect : FADE,
			pause : false
		});
	});
});

