//therendstudio
//V 0.2.6

inter_ImgLoadingQueue = new Class({
	Implements : [Events,Options],
	options : {
		iq_max_loading_requests : 3	//Maximum number of images to load simultaneously at any given time
	},
	initialize : function(image_paths,options){
		this.setOptions(options);
		this._numCurrentlyLoading = 0;
		this._listImagesPaths = $A(image_paths);
		this.length = image_paths.length;
		this._countLoaded = 0;
		this._listImages = [];
		this._cacheImages = [];	//All created images to be destroyed
	},
	_queueImages : function(){
		while(this._listImagesPaths && this._listImagesPaths.length && this._numCurrentlyLoading < this.options.iq_max_loading_requests){
			this._numCurrentlyLoading++;
			var path = this._listImagesPaths.shift();
			var img = new inter_Image(path,function(img){
				this._onLoad(img,img.__iq_ind);
			}.bind(this));
			img.__iq_ind = this._listImages.length;
			this._listImages.push(img);
			img.load();
			this._cacheImages.push(img);
		}
	},
	_onLoad : function(img,ind){
		this.fireEvent('loaded-img',[img,ind,++this._countLoaded]);
		this._numCurrentlyLoading--;
		if(!this._numCurrentlyLoading && !this._listImagesPaths.length)
			this.fireEvent('loaded-all',[this._listImages]);
		this._queueImages();
	},
	start : function(){
		if(this._listImagesPaths.length)
			this._queueImages();
		return this;
	},
	destroy : function(){
		delete this._listImagesPaths;
		this.length = 0;
		delete this._listImages;
		while(this._cacheImages.length)
			this._cacheImages.shift().destroy();
	 }
});

inter_SlideshowQueue = new Class({
	Extends : inter_ImgLoadingQueue,
	options : {
		sq_frame_duration : 3000,
		sq_max_frame_duration : 6000,	//If the next frame is not loaded after this long skip to the next frame
		sq_play_in_order : false,	//Play in the order they are passed in. If false will play in the order they get loaded.
		sq_loop : true					//Start over from the last img
	},
	initialize : function(ss_frames,options){
		this.parent(ss_frames,options);
		this._flagWaitingImage = 0;
		this._indNextFrame = 0;
		this._paused = false;
		this._playing = false;
		this._timerSlide = null;
		this._hashPlayedImages = new Hash();
	},
	startSlideshow : function(){
		this._paused = false;
		this._playing = true;
		return this.start();
	},
	stopSlideshow : function(){
		$clear(this._timerSlide);
		this._playing = false;
		this._paused = false;
		this._hashPlayedImages.empty();
	 },
	pause : function(){
		this._paused = true;
		$clear(this._timerSlide);
		return this;
  },
	resume : function(){
		if(this._paused)
			this._timerSlide = this._slide.delay(String(this.options.sq_frame_duration),this);	//delay of int(0) causes immeadiate call
		this._paused = false;
		return this;
	},
	_slide : function(){
		if(this._paused)
			return;
		var len = this._listImages.length;
		if(this._hashPlayedImages.getLength() == len)	//all played
			if(this.options.sq_loop)
				this._hashPlayedImages.empty();
			else{
				this.stopSlideshow();
				return;
			}
		for(var i = 0;i < len;i++)
			if(this._listImages[i].isLoaded){
				if(!this._hashPlayedImages.has(i))
					break;
			}else if(this.options.sq_play_in_order){	//If all loaded images have been played - wait more
				this._flagWaitingImage = i;
				return;
			};
		if(i == len){	//Nothing's loaded yet
			this._flagWaitingImage = 0;
			return;
		};
		var img = this._listImages[i];
		this._nextFrame(img,i);
	},
	_nextFrame : function(img,ind){
		if(!this._playing)
			return;
		this._hashPlayedImages.set(ind,true);
		this.fireEvent('frame',[img,ind]);
		this._timerSlide = this._slide.delay(String(this.options.sq_frame_duration),this);
	 },
	_onLoad : function(img,ind){
		this.parent(img,ind);
		if(this.options.sq_play_in_order && this._flagWaitingImage === ind
				|| !this.options.sq_play_in_order && this._flagWaitingImage !== false){
			this._flagWaitingImage = false;
			this._nextFrame(img,ind);
		}
	}
});

