// loopfadr: class for cycling images with nice jquery fades.
//
//	todo:
//			Go -> image in list before pushing onto imageSrcs.
//
//


function loopfadr(atarget) {
//	Setup
	this.loop = true;
	//this.loopTime = aloopTime;
	//this.fadeTime = afadeTime;
	
	this.loopTime = 10000;	// 10 seconds
	this.fadeTime = 2000; 	// 2s
	
	//	jsQuery target for image to be flipped.
	this.target = atarget;
	
	//	images to flip through
	this.imageSrcs = ['/images/layout/h_art_f2.jpg','/images/layout/h_art_f3.jpg','/images/layout/h_art_f4.jpg','/images/layout/h_art_f5.jpg'];
	
	//overide?
	//if( aimageSrcs != undefined){
	//	this.imageSrcs = aimageSrcs;
	//}

	// array holding image objects (for preloading)
	this.images = new Array;
	this.imgArrayPos = 0;
	this.triggerNext = '';
}

loopfadr.prototype.startLoop = function() {
	//	console.log("startLoop: loop:%i:src:%s", this.loop,this.imageSrcs[this.imgArrayPos]);

	if (this.loop){
		//alert("startLoop");
		this.loadNextImage();
	}
}
loopfadr.prototype.loadNextImage = function() {
	//console.log("loadNextImage: loop:%i:src:%s", this.loop,this.imageSrcs[this.imgArrayPos]);
	if (this.images[this.imgArrayPos]==undefined){
		this.images[this.imgArrayPos] = new Image;
		
		var triggerNext = new CCallWrapper(this, 0, 'startFadeOut');
		this.images[this.imgArrayPos].onload = function(){CCallWrapper.asyncExecute(triggerNext);};
		//	local filesystem testing version:
		//	window.rimages[window.rimgArrayPos].src = uri.dir + window.rimageSrcs[window.rimgArrayPos];
		this.images[this.imgArrayPos].src = this.imageSrcs[this.imgArrayPos];
		//alert(this.images[this.imgArrayPos].src);
	}
	else {
		this.startFadeOut();
	}
}
loopfadr.prototype.startFadeOut = function() {
	if (this.loop){
		//console.log("startFadeOut: loop:%i", this.loop);
		var triggerFade = new CCallWrapper(this, 0, 'startFadeIn');
		$(this.target).fadeTo(this.fadeTime,.001, function(){CCallWrapper.asyncExecute(triggerFade);});
	}
}
loopfadr.prototype.startFadeIn = function() {
	//alert("startFadeIn" + this.fadeTime);
	//console.log("startFadeIn: loop:%i:src:%s", this.loop,this.imageSrcs[this.imgArrayPos]);
	if (this.loop){
		$(this.target).attr("src",this.images[this.imgArrayPos].src);
		var triggerFade = new CCallWrapper(this, 0, 'repeat');
		$(this.target).fadeTo(this.fadeTime,1,function(){CCallWrapper.asyncExecute(triggerFade);});
	}
	else{
		$(this.target).fadeTo(100,1);
	}
}

loopfadr.prototype.repeat = function() {
	//alert("repeat");
	//console.log("repeat: loop:%i:src:%s", this.loop,this.imageSrcs[this.imgArrayPos]);
	//	start the next roll.
	if (this.loop){
		// move pointer to next image in list.
		this.imgArrayPos++;
		if (this.imgArrayPos > (this.imageSrcs.length-1)) {
			this.imgArrayPos = 0;
		}

		this.triggerNext = new CCallWrapper(this, this.loopTime, 'startLoop');
		CCallWrapper.asyncExecute(this.triggerNext);
	}
}

loopfadr.prototype.Stop = function () {
	//alert("Stop:"+this.target);
	this.loop = false;
}

loopfadr.prototype.Go = function() {
	//alert(this.target + ":" +$(this.target).attr("src"));
	if ($(this.target).attr("src") != undefined ){
		this.imageSrcs.push($(this.target).attr('src'));
	}
	this.loop = true;
	this.triggerNext = new CCallWrapper(this, this.loopTime, 'startLoop');
	CCallWrapper.asyncExecute(this.triggerNext);
}
loopfadr.prototype.triggerLoop = function(){
	if (this.loop){
		this.Stop();
		//$(this.target).fadeIn('fast');
	}
	else {
		this.loop = true;
		this.startLoop();		
	}
	//console.log("trigger: loop:%i", this.loop);
}
