/*
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
   Secuencia de imagenes
   Namespace com.acuataller
   version:  d20-m04-a07
//  -- -- -- -- -- -- -- -- --
//  Mauricio F. Tolezano (www.acuataller.com)
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
// 
*/

com.acuataller.ui.SecuenciaImagenes = function(idImagen, intervalo, urlImagenes, preferencias) 
{										
	this.imagen 	 	= document.getElementById( idImagen );
	this.intervaloId 	= null;
	this.intervalo 	 	= intervalo;
	this.urlImagenes 	= urlImagenes;
	this.indiceSecuencia= 0;
	this.siguienteImagen= 0;
	
	this.ronda			= 0;
	
	// Timeout
	var _this = this;
	this.delegarTimeout = function(){_this.cambioDeImagen()};
	
	// Precarga de imagenes.	
	this.precargaImagen = new com.acuataller.utiles.CargarImagen( this.imagen, this.finPrecargaImagen, this);
		
	
	this.opciones 		= { 
							rondas	: false,
							vista 	: { vistaCambioDeImagen : false, scope : false }	 
						  };
	this.opciones 		= this.aplicarPreferencias( this.opciones, preferencias );				  

	
	this.iniciarSecuencia()
}


com.acuataller.ui.SecuenciaImagenes.prototype = {
		
		
	iniciarSecuencia: function() 
	{
		this.prepararSiguiente();
	},	
	
	
	prepararSiguiente: function() 
	{
		if( this.siguienteImagen > this.urlImagenes.length-1 ) 
		{
			this.siguienteImagen = 0;
			this.ronda ++;
		}		
		if( this.opciones.rondas && this.ronda == this.opciones.rondas) return false;
		
		
		this.precargarImagen( this.urlImagenes[this.siguienteImagen] );
			
		this.siguienteImagen++;
	},
	
	
	precargarImagen: function(url) 
	{
		this.precargaImagen.cargar(url);
	},
	
	
	finPrecargaImagen: function() 
	{
		this.siquienteEnEspera();
	},	
	
	
	siquienteEnEspera: function() 
	{
		this.eliminarIntervalo();	
		
		if (!this.intervaloId) {
			this.intervaloId = window.setTimeout(this.delegarTimeout, this.intervalo);
		}
	},
	
	
	cambioDeImagen: function() 
	{		
		this.vistaCambioDeImagen();
		this.prepararSiguiente();
		
		this.indiceSecuencia++;
	},
	
	
	vistaCambioDeImagen: function() 
	{
		if(this.opciones.vista.vistaCambioDeImagen) 
			this.opciones.vista.vistaCambioDeImagen.call(this.opciones.vista.scope);
	},
	
	
	eliminarIntervalo: function() 
	{
		window.clearTimeout(this.intervaloId);
		this.intervaloId = null;

	},
	
	
	aplicarPreferencias : function(defaults, prefs) 
	{
		prefs = prefs || {};
		var prop, result = {};
		for (prop in defaults) result[prop] = prefs[prop] || defaults[prop];
		return result;
	},
	
	
	getIndice: function() 
	{
		return this.indiceSecuencia;
	},
	
	getIndiceSiguiente: function() 
	{
		return this.siguienteImagen;
	}
	
}
