// -----------------------------------------------------------------------------------
//
// Galeria de Imagenes
// Realizacion: digival.es
// joseignacio.marcos@digival.es
//
// -----------------------------------------------------------------------------------

var galeria = new Class({

	initialize: function(enlaces) {
		this.enlaces		= enlaces;
		this.actual			= 0;
		
		this.contenedor	= 'divGaleria';
		this.cajaCom		= 'divComentarios';
		this.cajaFoto 		= 'divFoto';
		this.cerrar			= 'cerrarGaleria';
		
		this.fotoId 		= 'fotoGaleria';
		this.prevLink 		= 'prevLink';
		this.nextLink 		= 'nextLink';
		
		this.mov 			= 1;
		
		this.anchoMax	= window.getWidth();
		this.altoMax	= window.getHeight();
	},

	start: function(){
		this.enlaces.each(function(enlace, i){
			enlace.addEvent('click', function(e) {
				e = new Event(e).stop();
				this.actual = i;			
				if(!$(this.contenedor)){
					this.creaContenedor(300, 100);
					this.insertaImg();
				}else{
					this.cambiaFoto();
				};
			}.bind(this));
		},this)
		
		window.addEvent('resize', function() {
			this.anchoMax	= window.getWidth();
			this.altoMax	= window.getHeight();
			if($(this.contenedor)) this.centraContenedor();
		}.bind(this));
		
	},
	
	
	creaContenedor: function(anchoIni, altoIni){
		$(document.body).adopt(
			new Element('div', {
				'styles': {
						'left': ((this.anchoMax-anchoIni)/2).toInt(),
						'top': ((this.altoMax-altoIni)/2).toInt()+window.getScrollTop(),
						'width': anchoIni+'px',
						'height': altoIni+'px'
			    },
			  'id': this.contenedor
				})		
		);		
		$(this.contenedor).adopt(new Element('a', {'events': {'click': function(e){e = new Event(e).stop(); this.cierra(); }.bind(this)}, 'title': 'Cerrar Imagen', 'id': this.cerrar, 'href': '#'}) );
		$(this.contenedor).adopt(new Element('div', {'id': this.cajaFoto}) );
		
		$(this.contenedor).effect('opacity', {duration: 500}).start(0,1);
	},
	
	insertaImg: function(){
		var srcImagen = this.enlaces[this.actual];
		var imgNew = new Asset.image(srcImagen, {id: this.fotoId, alt:'foto', onload: this.creaTextoyFoto.bind(this) });
		$(this.cajaFoto).adopt(imgNew);
	},

	creaTextoyFoto: function(){
		if(this.mov == 1){
			this.mov = 2;
			this.resizeContenedor($(this.fotoId).getStyle('width').toInt(), $(this.fotoId).getStyle('height').toInt());
		}
	},
	
	resizeContenedor: function(ancho, alto){
		var efectCont = $(this.contenedor).effects({duration: 500, transition:Fx.Transitions.Sine.easeInOut});
		efectCont.start({
	   	'width': ancho ,
	   	'height': alto ,
			'left': ((this.anchoMax-ancho)/2).toInt(),
			'top': ((this.altoMax-alto)/2).toInt()+window.getScrollTop()
		}).chain(function(){
			this.cargaFoto();
		}.bind(this));
	},
	
	centraContenedor: function(){
		var efectCont2 = $(this.contenedor).effects({duration: 500, transition:Fx.Transitions.Sine.easeInOut});
		efectCont2.start({
			'left': ((this.anchoMax-$(this.fotoId).getStyle('width').toInt())/2).toInt(),
			'top': ((this.altoMax- $(this.fotoId).getStyle('height').toInt())/2).toInt()+window.getScrollTop()
		});
	},
	
	paginador: function(){
		if(this.actual!=0){
			$(this.cajaFoto).adopt(
				new Element('a', {
					'styles':{
						'width': ($(this.cajaFoto).getStyle('width').toInt()/2).toInt(),
						'height': $(this.cajaFoto).getStyle('height').toInt()
					},
					'events': {
						'click': function(e){e = new Event(e).stop(); this.prevFoto(); }.bind(this)
					},
					'href': '#',
					'title': 'Imagen Anterior',
				  'id': this.prevLink
				})		
			);		
		}
		if(this.actual!=this.enlaces.length-1){
			$(this.cajaFoto).adopt(
				new Element('a', {
					'styles':{
						'width': ($(this.cajaFoto).getStyle('width').toInt()/2).toInt(),
						'height': $(this.cajaFoto).getStyle('height').toInt()
					},
					'events': {
						'click': function(e){e = new Event(e).stop(); this.nextFoto(); }.bind(this)
					},
					'href': '#',
					'title': 'Proxima Imagen',
				  'id': this.nextLink
				})		
			);		
		}
	},
	
	cargaFoto: function(){
		$(this.fotoId).effect('opacity', {duration: 500, onComplete: function(){this.mov=1;}.bind(this) }).start(0,1);
		this.paginador();
	},
	
	cambiaFoto: function(){
		$(this.fotoId).effect('opacity', {duration: 500, onComplete: function(){ 
				$(this.fotoId).remove(); 
				if($(this.nextLink)) $(this.nextLink).remove(); 
				if($(this.prevLink)) $(this.prevLink).remove(); 
				this.insertaImg(); 
		}.bind(this) }).start(1,0);
	},
	
	prevFoto: function(){
		this.actual-=1; 
		this.cambiaFoto();
	},
	
	nextFoto: function(){
		this.actual+=1; 
		this.cambiaFoto();
	},
	
	cierra: function(){
		$(this.contenedor).effect('opacity', {duration: 500, onComplete: function(){ $(this.contenedor).remove(); }.bind(this) }).start(1,0);
	}
		
});

function InitGaleria(){
	if($$('.minis li a')!=""){
		var galeriaFotos = new galeria($$('.minis li a')).start();
	}
}

/* -----------------------------------------------------------------------------
LOAD ---------------------------------------------------------------------------
----------------------------------------------------------------------------- */ 
window.addEvent('domready', InitGaleria);