var escaparate = new Class({

	initialize: function() {		
		this.promos 		= $$('.escaparate'); //contenedor general donde aparecera la imagen y la descripcion
		this.tiempo			= 9000; 	//tiempo de transicion entre foto y foto
		this.ancho			= 490; 	//ancho de las imagenes
		this.myTimer;					// interval
		this.activo 		= 0;		//contador de la actual imagen
		this.efectos		= new Array();
		
		this.moviendose 	= 0;
		this.anterior 		= $$('.flechaIzq');
		this.siguiente 	= $$('.flechaDer');
	},
	
	start: function(){
		this.promos.each(function(elem, i){
			if(i==0) elem.setStyle('left', '0px');
			else elem.setStyle('left', '500px');
			this.efectos[i] = elem.effect('left',{duration: 1000,onStart: function(){ this.moviendose=1; }.bind(this), onComplete: function(){ this.moviendose=0; }.bind(this) });
		}.bind(this));
		
		this.anterior.addEvent('click', function(e){new Event(e).stop(); if(this.moviendose==0){this.irant();} }.bind(this));
		this.anterior.addEvent('mouseover', function(){ this.setStyle('opacity', 0.5); });
		this.anterior.addEvent('mouseout', function(){ this.setStyle('opacity', 1); });
		this.siguiente.addEvent('click', function(e){new Event(e).stop(); if(this.moviendose==0){this.irsig();} }.bind(this));
		this.siguiente.addEvent('mouseover', function(){ this.setStyle('opacity', 0.5); });
		this.siguiente.addEvent('mouseout', function(){ this.setStyle('opacity', 1); });
		
		this.myTimer = this.mueveCarrusel.periodical(this.tiempo, this);
	}, 
	
	mueveCarrusel: function(){
		this.mover(this.activo, 1); //muevo el que hay para que salga
		if(this.activo==this.promos.length-1){//compruebo si es o no el ultimo
			this.activo = 0;	
		}else{
			this.activo +=1;
		}
		this.mover(this.activo,2); //muevo el nuevo para que entre
	},
	
	mover: function(num, accion){
		if(accion==1){ //desde dentro de la foto a la izquierda
			this.efectos[num].start(0, -this.ancho);
		}else if(accion==2){ //desde la derecha a dentro de la foto
			this.efectos[num].start(this.ancho, 0);
		}else if(accion==3){ //desde dentro de la foto a la derecha
			this.efectos[num].start(0, this.ancho);
		}else if(accion==4){ //desde la izquierda a dentro de la foto
			this.efectos[num].start(-this.ancho, 0);
		}	
	},
	
	irant: function(){
		$clear(this.myTimer);
		this.mover(this.activo, 3);
		if(this.activo==0){
			this.activo=this.promos.length-1;
		}else{
			this.activo-=1;
		}
		this.mover(this.activo, 4);
		this.myTimer = this.mueveCarrusel.periodical(this.tiempo, this);
	},
	
	irsig: function(){
		$clear(this.myTimer);
		this.mueveCarrusel();
		this.myTimer = this.mueveCarrusel.periodical(this.tiempo, this);
	},
	
	pausar: function(){
		$clear(this.myTimer);
	}
	
});

var chageImg = new Class({

	initialize: function() {		
		this.imagenes 		= $$('.minisSubasta a'); 
		this.imagenCambiar= $$('.fotoSubasta img');
	},
	
	start: function(){
		this.imagenes.each(function(elem, i){
			elem.addEvent('click', function(e){new Event(e).stop(); this.cambiaImg(elem); }.bind(this));
		}.bind(this));
	},
	
	cambiaImg: function(ruta){
		this.imagenCambiar.setProperty('src', ruta);
	}
	
});

function Inicia(){
	
	if($chk('contEscaparate')){ //inicializo el escaparate de la portada
		var escaparateSubastas = new escaparate().start();
	}
	
	if($chk('subastaFicha')){
		var imagenesFicha = new chageImg().start();//inicializo los cambios de imagenes
		
		$$('.toggler').each(function(elm, i){//le quito el href para que no haga nada
			elm.addEvent('click', function(e){e = new Event(e).stop();});
		});
		//accordion de fotos y mapa de localizacion
		var accordionSubasta = new Accordion('.toggler', '.elements', {opacity:false,show:1});
	}

}

window.addEvent('load', Inicia);