PanoScroll = Class.create();
Object.extend(Object.extend(PanoScroll.prototype, Abstract.prototype), {
	initialize: function(wrapper, options){
		this.wrapper = $(wrapper);
		this.options = Object.extend({ pause: 1, speed: 1 }, options || {});

		this.options.offset = 1;
		this.options.scrollFreq = 0.05 / this.options.speed;
		
		this.needReset = false;
		this.sens = 1;
		this.executer = null;
	},

	scrollTo: function(offset) {
		this.stop();
		this.wrapper.scrollLeft = offset;
	},
	
	scroll: function() {
		var max = this.wrapper.scrollWidth - this.wrapper.offsetWidth;

		if(this.needReset) {
			this.stop();
			this.start();
		}
		
		if(this.sens==1) {
			if(this.wrapper.scrollLeft < max)
				this.wrapper.scrollLeft = this.wrapper.scrollLeft + this.options.offset;
			else {
				this.wrapper.scrollLeft = max;
				this.sens = -1;
				this.stop();
				this.executer = new PeriodicalExecuter(this.scroll.bind(this), this.options.pause);
				this.needReset = true;
			}
		}
		else {
			if(this.wrapper.scrollLeft > 0)
				this.wrapper.scrollLeft = this.wrapper.scrollLeft - this.options.offset;
			else {
				this.wrapper.scrollLeft = 0;
				this.sens = 1;
				this.stop();
				this.executer = new PeriodicalExecuter(this.scroll.bind(this), this.options.pause);
				this.needReset = true;
			}
		}
	},

	start: function() {
		this.needReset = false;
		this.executer = new PeriodicalExecuter(this.scroll.bind(this), this.options.scrollFreq);
	},

	startScrollLeft: function() {
		this.stop();
		this.sens = -1;
		this.start();
	},
	
	startScrollRight: function() {
		this.stop();
		this.sens = 1;
		this.start();
	},

	stop: function(){
		//clearTimeout(this.autoscrollTimerID);
		if(this.executer)
			this.executer.stop();
	}
});




PanoSelector = Class.create();
Object.extend(Object.extend(PanoSelector.prototype, Abstract.prototype), {
	initialize: function(container, panoimg, options){
		this.effectFinished  = false;
		this.preloader = null;
		this.executer = null;
		/* imageId pour changer de pano */
		this.container    = $(container);
		this.panoimg    = $(panoimg);
		this.options    = Object.extend({ duration: 0.5 }, options || {});
		this.imageLoaded = false;
	},
	
	update: function(filename) {
		if(filename != "") {
			this.effectFinished  = false;
			this.preloader = null;
			this.imageLoaded = false;
			
			Effect.Fade(this.container, {to: 0.1, duration: this.options.duration, afterFinish: this.onFinishEffect.bind(this)});
			var myLoader = new ImagePreloader(filename, this.onLoadImage.bind(this));
			this.executer = new PeriodicalExecuter(this.checkFinished.bind(this), 0.5);
		}
	},
	
	onLoadImage: function(preldr) {
		this.preloader = preldr;
	},
	
	onFinishEffect: function() {
		this.effectFinished = true;
	},
	
	checkFinished: function() {
		if(this.imageLoaded) {
			Effect.Appear(this.container, {duration: this.options.duration});
			this.executer.stop();
		}
		else if(this.preloader != null && this.effectFinished) {
			this.panoimg.style.width = "auto";
			//this.panoimg.style.height = this.preloader.image.height + "px";

			this.panoimg.src = this.preloader.imageSrc;
			//this.panoimg.style.width = this.preloader.image.width + "px";
			//this.panoimg.style.height = this.preloader.image.height + "px";

			this.imageLoaded = true;
			this.executer.stop();
			this.executer = new PeriodicalExecuter(this.checkFinished.bind(this), 0.5);
		}
	}
});
