/*
Desarollado por GEEKBOX - Soluciones Interactivas
http://www.geekbox.cl
*/

//var moviendose=0;

function carrusel(id,ancho,alto,modulo,restar,modIniciales,btnRe,btnAv,loop) {
	this.id=id;
	this.obj=$(id);
	this.parent = this.obj.parentNode;
	this.modulos = $$('#'+id+' .modulo');
	this.cantidadDeModulos = this.modulos.length;
	this.ancho=ancho;
	this.anchoTotal=(this.cantidadDeModulos*modulo)-restar;
    this.alto=alto;
	this.modulo=modulo;
	this.restar=restar;
	this.modIniciales=modIniciales;
	this.btnRe=$(btnRe);
	this.btnAv=$(btnAv);
	this.loop=loop;
	var moviendose=0;
	this.n=0;
	this.duracion=750;

	this.ir=function(accion){
		if(!moviendose){
			if(accion=="adelante"){ this.n++; }
			else if(accion=="atras"){ this.n--; }
			else{
				if(accion>=0 && accion<=this.modulos.length-this.modIniciales){
					this.n=accion;
				}
			}
			
			if(this.loop){
				if(this.n>this.modulos.length-this.modIniciales){ this.obj.style.left=0; this.n=1; }
				if(this.n<0){ this.obj.style.left=-(this.anchoTotal-this.ancho)+"px"; this.n=this.modulos.length-this.modIniciales-1; } 
			}
			else{
				if(this.n>this.modulos.length-this.modIniciales){ this.n=this.modulos.length-this.modIniciales; }
				if(this.n<0){ this.n=0; }
				
				if(this.n==0){
					this.btnRe.addClass('inactivo');
					this.btnRe.fade(0.5);
				}
				else{
					this.btnRe.removeClass('inactivo');
					this.btnRe.fade(1);
				}
				if(this.n==this.modulos.length-this.modIniciales){
					this.btnAv.addClass('inactivo');
					this.btnAv.fade(0.5);
				}
				else{
					this.btnAv.removeClass('inactivo');
					this.btnAv.fade(1);
				}
			}
			
			moviendose=1;
			var left=this.obj.offsetLeft;
			var newLeft=-this.modulo*this.n;
			this.animacion.start('left', newLeft+'px');
			setTimeout(function(){ moviendose=0; },this.duracion+100);
		}
	}
	
	this.init=function(){
		if(!this.loop){ $(this.btnRe).fade(0.5,0.5); }
		var container = document.createElement("div");
		container.id="carruselScroll_"+id;
		container.className="carruselScroll";
		this.parent.insertBefore(container,this.obj);
		container.appendChild(this.obj);
		
		container.style.position = "relative";
		container.style.overflow = "hidden";
		container.style.width = this.ancho+"px";
		container.style.height = this.alto+"px";
		this.obj.style.width = this.anchoTotal+"px";
		this.obj.style.position = "absolute";
		this.obj.style.top = "0";
		this.obj.style.left = "0";
		this.obj.style.display="block";
		
		for(i=0;i<this.modulos.length;i++){
			this.modulos[i].i=i+1;
			this.modulos[i].style.cssFloat="left";
			this.modulos[i].style.styleFloat="left";
			if(i==this.modulos.length-1){ this.modulos[i].addClass('ultimo'); }
		}
		
		this.animacion = new Fx.Tween(this.obj, { transition: Fx.Transitions.Quint.easeOut, duration:this.duracion });

		this.btnRe.addClass('inactivo');
	}
	
	this.init();
}