

addEvent(window,'load', initSlideshow);


function slideshow() {
	this.stopped = true;
	this.slider = $('slideshow');
	this.image = $('slideshow_center');
	this.src = null;
	this.button = null;
	this.oldbutton = null;
	this.direction = 4;
	this.imgIds = new Array('','slideshow_top','slideshow_right','slideshow_bottom','slideshow_left');
	this.X = new Array('',0,-620,0,620);
	this.Y = new Array('',527,0,-527,0);
}

slideshow.prototype.setbutton = function () {
	try {this.oldbutton.style.textDecoration = 'none'; this.oldbutton.style.color = '#cfcfcf';} catch (e) {};
	try {this.button.blur();
	this.button.style.textDecoration = 'underline';
	this.oldbutton = this.button;} catch (e) {};
}

slideshow.prototype.setdirection = function () {
	var rnd = new Array(1,2,3,4);
	rnd.shuffle();
	this.direction = rnd[0];
}

slideshow.prototype.preloadAndStart = function (source, button) {
	// try hide 360
	try{$('appletViewerRestaurant').style.display = 'none';} catch (e) {}
	try{$('appletViewerSerre').style.display = 'none';} catch (e) {}

	
	this.src = '../'+source;
	this.button = button;
	
	var preload = new Image;
	
	// set loading message
	try{$('loading').innerHTML = "loading...";} catch (e) {}
	
	var self = this;
	preload.onload = function () {
		try{$('loading').innerHTML = "";} catch (e) {}
		if (self.stopped) {
			self.setbutton();
			self.setdirection();
			self.insert();
			self.slide();
		}
	}
	preload.src = this.src; 
}

slideshow.prototype.insert = function () {
	var imgId = this.imgIds[this.direction];
	
	// calculate current slider offset
	var offsetTop = Element.getStyle(this.slider, 'top');
	var offsetLeft = Element.getStyle(this.slider, 'left');
	offsetTop = (offsetTop==null)? '0px':offsetTop; offsetTop = parseInt(offsetTop.slice(0,-2));
	offsetLeft = (offsetLeft==null)? '0px':offsetLeft; offsetLeft = parseInt(offsetLeft.slice(0,-2));
	
	var x = this.X[this.direction];
	var y = this.Y[this.direction];
	offsetTop = -y-offsetTop;
	offsetLeft = -x-offsetLeft;
	
	var style = 'top:'+offsetTop+'px;left:'+offsetLeft+'px;';

	new Insertion.Top($('slideshow'), '<img id="'+imgId+'" src="'+this.src+'" style="'+style+'" />');
}


slideshow.prototype.slide = function () {
	this.stopped = false;
	var x = this.X[this.direction];
	var y = this.Y[this.direction];
	var self = this;
	Effect.MoveBy(this.slider, y, x, {duration:1.5,afterFinish:function () {self.reset()}});
}

slideshow.prototype.reset = function () {	
	this.stopped = true;
	var imgId = this.imgIds[this.direction];
	Element.remove(this.image);
	this.image = $(imgId);
}

slideshow.prototype.show360 = function (which) {
	if (which == 'restaurant') {
		$('appletViewerSerre').style.display = 'none';
		$('appletViewerRestaurant').style.display = 'block';
	}
	if (which == 'serre') {
		$('appletViewerRestaurant').style.display = 'none';
		$('appletViewerSerre').style.display = 'block';
	}
	
}

function initSlideshow() {
	slide = new slideshow();
}





// ---------------------------- automatic slideshow in other pages than photo.html ------------------------------ //
// define sliderVisuals array per html page
if (typeof sliderVisuals != 'undefined')
	addEvent(window,'load', automaticSlideshow);
	
function automaticSlideshow() {
	var i = 0;
	window.setInterval(function () {slide.preloadAndStart('visuals/'+sliderVisuals[i], ''); i++; if (i == sliderVisuals.length) i = 0;}, 10000);
}











