/* -------------------------------------------------------------------------------------------
--	JAVASCRIPT CBVALLADOLID.ES		-------------------------------------------------------------
--	Realizacion: digival.es 		-------------------------------------------------------------
--	joseignacio.marcos@digival.es -------------------------------------------------------------
------------------------------------------------------------------------------------------- */
function InitTooltips(){	

	var myTips = new Tips('.toolTip', {offsets: {'x': 15, 'y': -30}, fixed: true});
	
	if($('imagenDisplay')) var display = new DisplayPortada().start();
	
	if($('imgFicha')) var cambiarImagenes = new chageImg($$('.miniImagenes a'), $('imgFicha')).start();
	
	if($$('.tipAbono')!=''){
		$$('.tipAbono').each(function(tipAb, i){tipAb.addEvent('click', function(e){new Event(e).stop();});});//quito el enlace a los tooltips
		var tipsAbonos = new Tips('.tipAbono', {offsets: {'x': -10, 'y': -35}}); //asigno los tooltips
		tipsAbonos.addEvent('show', function(tip){ tip.fade('in'); }); //le asigno el fade in
		tipsAbonos.addEvent('hide', function(tip){ tip.fade('out'); });//le asigno el fade out
	}
	
	if($$('.tabsCronica')!=''){
		var tabs = new Array($$('.tabsCronica li a')[0], $$('.tabsCronica li a')[1]);
		$$('.tabsCronica li a')[0].addEvent('click', function(e){new Event(e).stop();});
		$$('.tabsCronica li a')[1].addEvent('click', function(e){new Event(e).stop();});
		var myAccordion = new Accordion(tabs, $$('.contCronica'), {
			onActive: function(toggler, element){
				toggler.addClass('activo');
			},
			onBackground: function(toggler, element){
				toggler.removeClass('activo');
			}
		});
	}
	
}

var chageImg = new Class({

	initialize: function(images, imgCambiar) {		
		this.images 			= 	images; 
		this.imagenCambiar	= 	imgCambiar;
		this.contImg			= 	imgCambiar.getParent();
		this.imagenId 			= 	this.imagenCambiar.getProperty('id');
		this.imagenAlt 		= 	this.imagenCambiar.getProperty('alt');
		this.loading			= 	'#fff url(../images/iconos/cargando.gif) no-repeat 50% 50%';
		this.tiempo				=	400;
	},
	
	start: function(){
		this.images.each(function(elem, i){
			elem.addEvent('click', function(e){new Event(e).stop(); this.cargaImg(elem); }.bind(this));
		}.bind(this));
		this.efectoImagen = new Fx.Tween(this.contImg, {wait:false, duration: this.tiempo});
	},
	
	cargaImg: function(ruta){
		this.efectoImagen.start('opacity', 0).chain(
			function() { 
				this.contImg.getParent().setStyle('background', this.loading);
				this.imgNew = new Asset.image(ruta, {id: this.imagenId, alt:this.imagenAlt, onload: this.cambiaImg.bind(this) });	
			}.bind(this)
		);
	},
	
	cambiaImg: function(){
	
		this.efectoImagen.start('height', this.imgNew.getProperty('height').toInt()).chain(
			function() { 
				this.efectoImagen.start('opacity', 1)
				this.imgNew.replaces($(this.imagenId));
				this.contImg.getParent().setStyle('background', 'none'); 
			}.bind(this)
		);
	}
	
});


var DisplayPortada = new Class({

	initialize: function() {
		this.imagen			= $('imagenDisplay');
		this.contImg		= $('contImgDisplay');
		
		this.enlaces 		= $$('.displayNoticias ul li a')	;
		this.nuevasImg		= $$('.displayNoticias ul li a').getProperty('href');
		this.minisImg		= $$('.displayNoticias ul li a img');
		this.flecha			= $('flecha');
		
		//variables generales
		this.actual 		= 0;
		this.anterior		= 0; //almaceno el que esta activo que lo cambiare al ocultar los textos
		this.efectos		= []; 	//array con los efectos de las miniaturas
		this.tiempo			= 5000; //tiempo para que cambie de noticia
		this.pausa			= 0; 		//control para saber cuando esta pausado
		
		this.contDescripcion = $$('.descripcion');
		this.titulos			= $$('.tituloDisplay');
		this.textos				= $$('.textoDisplay');
		
		document.addEvent('unload',function(){$clear(this.timerDisplay);});
	},
	
	start: function(){
		
		//oculto los titulos y descripciones
		for(tit=1;tit<this.titulos.length; tit++){	
			this.titulos[tit].setStyle('display', 'none');
			this.textos	[tit].setStyle('display', 'none');
		}
		
		//efectos
		this.efectoFlecha = new Fx.Tween(this.flecha, {wait:false, duration: 500});
		this.efectoImagen = new Fx.Tween(this.contImg, {wait:false, duration: 400});

		this.botonesDisplay();
		this.play(); //arrancamos el display
		
		this.enlaces.each(function(enlace, i){
		
			//efectos de color de las miniaturas
			this.efectos[i] = new Fx.Morph(enlace, {duration:500, wait:false});
			
			//eventos de las miniaturas
			enlace.addEvent('click', function(e) {e = new Event(e).stop(); 
				this.pause();
				this.actual = i;
				this.avanza(); 
			}.bind(this));		
			
			enlace.addEvent('mouseover', function() { this.bordeInEfecto(i); }.bind(this));
			enlace.addEvent('mouseout', function() { this.bordeOutEfecto(i); }.bind(this));
			
		}.bind(this));
		
	},
	
	avanzaPer: function(){
	
		if(this.actual == 3){
			this.actual = 0;
			this.avanza(this.actual);
		}else{
			this.actual++;
		 	this.avanza(this.actual);
		}
		
	},
	
	avanza: function(){
		this.cambiaTextos();
		this.cambiaImg();
		this.mueveFlecha();
	},
	
	bordeInEfecto: function(i){
		this.efectos[i].start({
			'background-color': '#701d25',
			'border-color': '#701d25'
		});
	},
	
	bordeOutEfecto: function(i){
		this.efectos[i].start({
			'background-color':'#ffffff',
			'border-color': '#cccccc'
		});
	},
	
	mueveFlecha: function(){
		this.efectoFlecha.start('top', parseInt( (this.actual*11) + (this.actual*59) + 30));
	},
	
	cambiaTextos: function(){
		this.titulos[this.anterior].setStyle('display', 'none');
		this.textos[this.anterior].setStyle('display', 'none');
		this.anterior=this.actual;
		this.titulos[this.actual].setStyle('display', '');
		this.textos[this.actual].setStyle('display', '');
	}, 
	
	cambiaImg: function(){
		
		this.efectoImagen.start('opacity', 0).chain(
			function() { 
				
				$('fotoDisplay').setStyle('background', '#fff url(../images/iconos/cargando.gif) no-repeat 50% 50%');
				this.imgNew = new Asset.image(this.nuevasImg[this.actual], {id: 'imagenDisplay', alt:'Noticia Iraurgi SB', onload: this.replaceImg.bind(this) });
				
			}.bind(this)
		);
		
	},
	
	replaceImg: function(){
	
		this.imgNew.replaces($('imagenDisplay')); //se intercambian las images
		$('fotoDisplay').setStyle('background', '#fff'); //quito el icono de cargando
		this.efectoImagen.start('opacity', 1); //muestro la imagen
		
	},
	
	//FUNCIONES PARA CONTROL DE REPRODUCCION DEL DISPLAY
	pause: function(){
		$clear(this.timerDisplay);
		$('playPause').setStyle('background-image', 'url(../images/iconos/play.png)');
		this.pausa = 1;
	}, 
	
	play: function(sig){
		if(sig==1) this.avanzaPer();
		this.timerDisplay = this.avanzaPer.periodical(this.tiempo, this);
		$('playPause').setStyle('background-image', 'url(../images/iconos/pause.png)');
		this.pausa = 0;
	},
	
	botonesDisplay: function(){
		this.botonPlayPause = new Element('a', {href: '#', title: 'Parar/reproducir el display', id: 'playPause'});
		$('fotoDisplay').adopt(this.botonPlayPause);
		this.botonPlayPause.addEvent('click', function(e){ e = new Event(e).stop(); 
			if(this.pausa == 1){
				this.play(1);
			}else{
				this.pause();
			}
		}.bind(this));
		
	}
	
});

/* -----------------------------------------------------------------------------
LOAD ---------------------------------------------------------------
----------------------------------------------------------------------------- */ 
window.addEvent('domready', InitTooltips);
