/*
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
   Reproductor de Audio
   version:  d2-m11-a11
//  -- -- -- -- -- -- -- -- --
//  Mauricio F. Tolezano (www.aquataller.es)
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
// 
//
*/


JL_reproductorAudio = {		
	
	id_cookie			: false		,
	is_ready			: false		,
	reproductor			: false		,
	reproductor_iu		: false		,
	contenedor_titulo	: false		,
	tmo_animar_titulo	: false		,
	
	temas				: false		,		
	ids_temas			: false		,	
	tema_inicial		: false		,
	indice_activo		: false		,
	tema_activo			: false		,
	is_play				: false		,
	is_play_lista		: false		,
	volume				: false		,
	// is_cambio_estado	: false		,
	
	cntrl_anterior		: false		,
	cntrl_sigiente		: false		,
	
	
	iniciar: function( conf ) 
	{	
		var self = this;
		
		
		// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --		
		// Config
		//
				
		this.id_cookie 			= conf.id_cookie				;	
		this.contenedor_titulo 	= $( conf.contenedor_titulo )	;		
			
		this.cntrl_anterior 	= $( conf.cntrl_anterior 	)	;
		this.cntrl_sigiente 	= $( conf.cntrl_sigiente 	)	;
		
		
		// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --		
		// Controles nav secuencial
		//	
		this.cntrl_sigiente.click(function( event )
		{
			self.sigiente();			
		});
		
		this.cntrl_anterior.click(function( event )
		{
			self.anterior();
		});				
		
		// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --		
		// 
		//
		$(window).unload(function() 
		{
		 	self.set_estado();
		});
		
		
		// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --		
		// Inicializando Reproductor
		//
		
		this.reproductor 	= $( conf.id_reproductor 		);
		this.reproductor_iu = $( conf.id_reproductor_iu 	);
		
		this.reproductor.jPlayer({
				  
			ready: function () 
			{
				// self.reproductor_iu.fadeIn('slow').addClass('iniciado');
				
				if( self.tema_inicial )
				{					
					self.is_ready = true;
					self.iniciar_reproduccion();
				}
			},
			
			play: function () {				
				self.is_play = true;
				// self.is_cambio_estado = true;
			},
			
			pause: function () {				
				self.is_play 		= false;		
				// self.is_play_lista 	= false;
				// self.is_cambio_estado = true;
			},
			
			ended: function () {
				if(self.is_play_lista)
				{
					self.sigiente();
				}
				else{
					self.is_play = false;	
				}							
			},
			
			
			volumechange: function (event) {				
				self.volume = event.jPlayer.status.volume;
			},
			
			// verticalVolume	: true						,
			// errorAlerts		: true						,
 			// warningAlerts	: true						, 
 			solution			: conf.solution				,
			volume				: conf.volume				,
			swfPath				: conf.swfPath				,
			supplied			: conf.supplied				,
			cssSelectorAncestor	: conf.id_reproductor_iu	,
			wmode				: conf.wmode				});
		
	},
	
	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --		
	// Sigiente / Anterior
	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --
	//		
	sigiente : function()
	{
		this.seleccionar( this.indice_activo +1 );
	},
	anterior : function()
	{
		this.seleccionar( this.indice_activo -1 );
	},
	
	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --		
	// Set temas 
	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --
	//		
	set_temas : function( conf )
	{		
		if( ! conf.temas.length )
		{
			return false;
		}
		
		this.temas 			= conf.temas			;
		this.ids_temas 		= conf.ids_temas		;
		this.tema_inicial 	= ( conf.tema_inicial 	)? conf.tema_inicial 	: 1		;		
		this.is_play_lista 	= ( conf.play_lista 	)? conf.play_lista 		: false	;	
	
		this.indice_activo	= false		;
		this.tema_activo	= false		;
		
	
		
		if( this.is_ready )
		{
			this.iniciar_reproduccion( this.tema_inicial, true );
		}
	},	
	
	
	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --		
	// Iniciar reproduccion
	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --
	//		
	iniciar_reproduccion : function( tema, reproducir ) 
	{
		if( ! this.is_ready || ! this.temas )
		{
			return false;
		}		
		
		// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -
		// Reproduccion 'arrastrada' desde pagina anterior
		// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -		
		
		var estado = this.get_estado();
		
		if( tema )
		{			
			this.seleccionar( tema, reproducir );	
		}
		else if( estado )
		{	
			var self = this;
			
			// this.reproductor.jPlayer('volume', estado.volume);		
			
			// FadeIn si el tema no empieza desde el inicio
			if( estado.is_play && estado.tiempo > 0 )
			{
				this.reproductor.bind($.jPlayer.event.play , function(event) 
				{
					self.reproductor.unbind( $.jPlayer.event.play );
					
					self.vol_fade_in();					
				});	
			}			
			
			this.seleccionar( estado.tema, estado.is_play, estado.tiempo );		
			
		}
		else if( this.tema_inicial )
		{			
			this.seleccionar( this.tema_inicial, false );		
		}
		/*
		// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -
		// Reproduccion por defecto.
		// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -
		//
		else if( por_defecto ) {			
			this.seleccionar( por_defecto, false );
		}*/
						
	},
	
	
	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --		
	// Seleccionar tema 
	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --
	//		
	seleccionar : function( numero, reproducir, tiempo )
	{		
		
		// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --		
		// Seleccionar tema por Id 
		// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --
		//
		if( typeof(numero) == 'string' )
		{
			numero = $.inArray(numero, this.ids_temas) +1;	
		}
		
		// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -
		// Seleccionar nuevo Tema
		// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -
		//
		if( numero != this.indice_activo && this.temas[ numero-1 ]  )
		{
			
			reproducir 	= (typeof(reproducir) == 'undefined')? true : reproducir ; 		
			tiempo 		= (typeof(tiempo	) == 'undefined')? 0 	: tiempo 	 ; 	
			
			
			this.tema_activo = this.temas[ numero-1 ];
			
					
			// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -	
			// Seleccionar Tema
			//
			this.reproductor.jPlayer('setMedia', { mp3: this.tema_activo.url, preload: 'none' });			
			
			this.animar_titulo();
						
			
			$(this).trigger('tema-seleccionado', this.tema_activo );			
			
			
			// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
			// Reporducir a partir de un tiempo
			//
			if( reproducir && tiempo && tiempo > 0 )
			{				
				var self = this;
				// this.is_play = true;
				window.setTimeout( function(){self.reproductor.jPlayer('play', tiempo )}, 800 );
			}			
			// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
			// Reproducir desde el inicio
			//
			else if( reproducir )
			{				
				this.reproductor.jPlayer('play');
			}
									
			this.indice_activo = numero;
			// this.is_cambio_estado = true;
			
					
			// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
			// estado IU
			//
			this.actualizar_iu();
			
		}		
		// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -
		// Reproducir tema ya seleccionado
		// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -
		//	
		else if( this.indice_activo && ! this.is_play )
		{
			this.reproductor.jPlayer('play');		
		}
	},
	
	
	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --		
	// Actualizar IU
	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --
	//		
	actualizar_iu : function()
	{
		if( this.indice_activo == this.temas.length )
		{
			this.cntrl_sigiente.removeClass('activo');
		}else{
			this.cntrl_sigiente.addClass('activo');
		}
		
		if( this.indice_activo == 1 )
		{
			this.cntrl_anterior.removeClass('activo');
		}else{
			this.cntrl_anterior.addClass('activo');
		}
	},
	
	
	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --		
	// Get Estado
	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --
	//		
	get_estado : function()
	{			
		var val = $.cookie( this.id_cookie );
		
		if( val )
		{
			val = val.split('|');
			return {tema:val[0], tiempo:parseInt(val[1]), is_play:parseInt(val[2]), volume:val[3]}; //parseInt()
		}
		else
		{
			return false;
		}
	},	
	
	
	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --		
	// Set Estado
	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --
	//
	set_estado : function()
	{	
		if( ! this.is_ready || ! this.tema_activo ) // ! this.is_cambio_estado || 
		{
			return false;
		}
		
		var tiempo = this.reproductor.data("jPlayer").status.currentTime;	
		// var seek 		= this.reproductor.data("jPlayer").status.seekPercent;	
		var is_play 	= (this.is_play)? 1 : 0;	
		
		$.cookie( this.id_cookie, this.tema_activo.id+'|'+tiempo+'|'+is_play+'|'+this.volume, { path: '/' } ); // , { expires: 7, path: '/' }
	},	
		
	
	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --		
	//  FadeIn Volumen
	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --
	//
	vol_fade_in : function()
	{	
		var fade = function( vol, reproductor )
		{
			if(vol < 1) {				
				vol += 0.05;
				reproductor.jPlayer('volume', vol);
				this.t = setTimeout( function(){ fade(vol, reproductor)}, 50);
			} 
			else {
				clearTimeout(this.t);
				vol = 1;
			}
		}
		fade(0, this.reproductor);		
	},	
		
	
	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --		
	//  Animar titulo
	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --
	//
			
	animar_titulo : function( is_nuevo_titulo )
	{	
		this.contenedor_titulo
			.css('text-indent', 0)
			.html( this.tema_activo.titulo + '&nbsp;&nbsp;' );
		
		var ancho_titulo = this.contenedor_titulo.width();		
		
		this.contenedor_titulo.stop().append( this.tema_activo.titulo );		
		
		
		var self = this;
		
		clearTimeout(this.tmo_animar_titulo);
		
		this.tmo_animar_titulo = setTimeout( function(){
			
			self.contenedor_titulo.animate ({
				'text-indent': 0 - ancho_titulo
			  }, ancho_titulo*50, 'linear', function() {
				self.animar_titulo();
			});
			
			
		}, (self.is_play)? 4000: 12000 ); 
		
	
	}
}



// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --		
// Incializar 
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- -- -- -- -- -- -- --

$(function(){		
	
	JL_reproductorAudio.iniciar
	({ 
		id_reproductor		: '#jp_reproductor_audio'									, 
		id_reproductor_iu	: '#jp_reproductor_audio_contenedor'						, 
		cntrl_sigiente		: '#jp_reproductor_audio_contenedor .jp-next'				, 
		cntrl_anterior		: '#jp_reproductor_audio_contenedor .jp-previous'			,
		contenedor_titulo	: '#jp_reproductor_audio_contenedor .titulo .contenido'		,
		
		swfPath				: '/_js/jplayer'											, 
		supplied			: 'mp3'														,	
		volume				: 1															,		
		wmode				: 'window' 													,			
		solution			: 'flash' 													,	
		
		id_cookie			: 'jl_reproductor_audio'							
	});
		
});

