// JavaScript Document

var myLightbox;
var slideShows = new Array();

var Slideshow = Class.create();

Slideshow.prototype = {
	initialize: function(div,photo_array) {		
		this.element = $(div);
		this.slideShowIndex = 0;
		this.slideShowPE = null;
		this.fade_effet =  null;
		this.img_array = new Array(photo_array.lenght);
		this.hd_img_array = new Array(photo_array.lenght);
		var img;
		var myPhoto;
		var ml;
		var mt;
		var nbrImgs = photo_array.length;
		for(i=0; i < nbrImgs; i++) {
			myPhoto = new Hash(photo_array[i]);
			img = new Image();
			img.src = myPhoto.get('thumb');
			this.img_array[i] = Builder.node('div', {'class':"bloc_masque_image", 'style':"position:absolute;top:12px;left:31px;width:199px;height:147px;overflow:hidden;background-color:#FFFFFF;"}, [img]);
			this.hd_img_array[i] = myPhoto.get('image');
		}
		this.slideShowPE = new PeriodicalExecuter(this.doSlideShow.bind(this), 6);
		Event.observe(this.element.select('.bt-suivant-diaporama')[0],"click",this.onClickArrow.bindAsEventListener(this, -1));
		Event.observe(this.element.select('.bt-precendant-diaporama')[0],"click",this.onClickArrow.bindAsEventListener(this, 1));
		this.zoomLink = this.element.select('.loupe_image')[0];
	},
	onClickArrow: function(evt, step){ 
		evt.stop();
		this.doSlideShow(this.slideShowPE, step);
	},
	onAfterFinish: function(){ 
		var imgs = this.element.select('div.bloc_masque_image'); 
		var imgsLength = imgs.length;
		imgs.each(function(img, index) { if(index < imgsLength-2) img.remove(); } );
	},
	doSlideShow: function(pe, step, index) {
		if(!dragStarted) {
			var forceEffectNow = false;
			var currentSlideShowIndex = this.slideShowIndex;
			if(typeof index != "undefined") {
				if(this.slideShowPE) this.slideShowPE.stop();
				this.slideShowIndex = index;
			}
			else {
				if(typeof step == "undefined") {
					step = 1;
				}
				else {
					if(this.slideShowPE) this.slideShowPE.stop();
					forceEffectNow = true;
				}
				this.slideShowIndex += step;
			}
			if(this.slideShowIndex >= this.img_array.length) this.slideShowIndex = 0;
			if(this.slideShowIndex < 0) this.slideShowIndex = this.img_array.length-1;
			//alert(this.img_array[this.slideShowIndex]);
			Element.setOpacity(this.img_array[this.slideShowIndex], 0);
			var insertedImg = this.element.appendChild(this.img_array[this.slideShowIndex]);
			var img = this.img_array[this.slideShowIndex].firstDescendant();
			ml = Math.round((199 - img.width) / 2);
			mt = Math.round((147 - img.height) / 2);
			Element.setStyle(img, {'marginLeft':ml+'px', 'marginTop':mt+'px'});
			if(forceEffectNow) {
				this.fade_effet = new Effect.Fade(insertedImg, {from:0, to:1, duration:1, afterFinish:this.onAfterFinish.bind(this)} );
			}
			else {
				this.fade_effet = new Effect.Fade(insertedImg, {from:0, to:1, duration:1, afterFinish:this.onAfterFinish.bind(this), queue: {position:'end', scope: 'diaporama'} });
			}
			this.zoomLink.href = this.hd_img_array[this.slideShowIndex];
			myLightbox.updateImageList();
		}
	}
}
var startSlideShow = function(evt) {
	photos.each(function(photo){ new Slideshow(photo.key, photo.value); });
}
Event.observe(window,'load',startSlideShow);

var alignArkianeSearchDiv = function(obj) {
	//alert(obj.cumulativeOffset().top);
	if($('live-box').visible()) closeLivebox();
	var divReserver = $$('.reserver')[0];
	var offset = Element.cumulativeOffset(obj);
	var posy = offset.top - divReserver.getHeight() - (divReserver.getHeight() / 2) + Element.getHeight(obj);
	new Effect.Move (divReserver,{ y:posy , mode: 'absolute'});
}
