//////////////////////////////////////////
// slides.js
//////////////////////////////////////////

//////////////////////////////////////////
// constantes
FADE_INTERVAL=15000;

//////////////////////////////////////////
// funciones  y objetos

// ----------------------------------------------------
// Slides
// ----------------------------------------------------
function Slides(id,imgList){

	var i,oldId=0,self=this,timer;

	this.id=id;
	this.imgs=new Array();

	for(i=0;i<imgList.length;i++){

		this.imgs[i]=new Image(); 
		this.imgs[i].src=imgList[i];

	}

	// showRand
	this.showRand=function(chain){

		var next; 

		do{next=Math.floor(Math.random()*this.imgs.length)}
			while(next==oldId);

		this.showById(next,chain)

	}

	// showById
	this.showById=function(id,chain){

		if(this.imgs[id]){

			if(!chain) chain=function(){};

			// cambiamos a loading con fadeout...
			setTimeout(function(){

				loadImg(self.id,'img/trans.png');
				changeOpac(self.id,100);

				// al cargar nueva imagen...
				self.showImg=function(){

					changeOpac(self.id,0);
					loadImg(self.id,self.imgs[id].src);

					// mostramos con fadein y enlazamos con siguiente funcion...
					setTimeout(chain,fadeIn(self.id)) 

				}

				self.imgs[id].onload=self.showImg;
				if(self.imgs[id].complete) self.showImg();
			
			},fadeOut(self.id));

			oldId=id

		}

	}

	// start
	this.start=function(){

		var self=this;

		timer=setInterval(function(){self.showRand()},FADE_INTERVAL)

	}

	// stop
	this.stop=function(){

		if(!timer) return;
		clearTimeout(timer); 
		delete timer

	}

}

