//carruselScroll.
//Copyright (c) 2008 GEEKBOX - Desarrollo Multimedia, <http://www.geekbox.cl>.
//requiere mootools 1.2
 
 function carruselScroll(id,separacion,alto,margen,retroceder,avanzar){
	var obj = $(id);
	var modulos = $$('#'+id+' .modulo');
	var cantidadDeModulos = modulos.length;
	var ancho=(cantidadDeModulos*margen)+(cantidadDeModulos*separacion)-margen;
	var btnRetroceder = $(retroceder);
	var btnAvanzar = $(avanzar);

	var container = document.createElement("div");
	var parent = obj.parentNode;
	container.id="carruselScroll";
	parent.insertBefore(container,obj);
	container.appendChild(obj);
	
	container.style.position = "relative";
	container.style.overflow = "hidden";
	container.style.width=separacion+"px";
	container.style.height=alto+"px";
	obj.style.width = ancho+"px";
	obj.style.position = "absolute";
	obj.style.top = "0";
	obj.style.left = "0";
	obj.style.display="block";
	
	for(i=0;i<modulos.length;i++){
		modulos[i].style.cssFloat="left";
		modulos[i].style.styleFloat="left";
		modulos[i].style.width=(separacion+margen)+"px";
		modulos[i].style.height=alto+"px";
		modulos[i].style.overflow="hidden";
	}
	modulos[modulos.length-1].style.width=separacion+"px";
	
	var anchoContenedor=container.offsetWidth;
	var ancho=obj.offsetWidth;
	//animaciones
	var duracion = 1000;
	var rebote = new Fx.Tween(obj, { transition: Fx.Transitions.Bounce.easeOut, duration:duracion });
	var nice = new Fx.Tween(obj, { transition: Fx.Transitions.Back.easeOut, duration:duracion });
	var elastico = new Fx.Tween(obj, { transition: Fx.Transitions.Elastic.easeOut, duration:duracion });
	var quint = new Fx.Tween(obj, { transition: Fx.Transitions.Quint.easeOut, duration:duracion });
	var avance = new Fx.Tween(obj, { transition: Fx.Transitions.Quint.easeOut, duration:duracion });

	btnAvanzar.addEvent('click', function(){
		var left=obj.offsetLeft;
		var newLeft=left-separacion-margen;
		if(newLeft<anchoContenedor-ancho){
			newLeft=anchoContenedor-ancho;
		}
		else{
			detalleCarrusel('','','');
		}
		quint.start('left', newLeft+'px');
	});
	btnAvanzar.addEvent('mouseenter', function(){
		this.addClass('over');
	});
	btnAvanzar.addEvent('mouseleave', function(){
		this.removeClass('over');
	});
	
	btnRetroceder.addEvent('click', function(){
		var left=obj.offsetLeft;
		var newLeft=left+separacion+margen;
		if(newLeft>0){
			newLeft=0;
		}
		else{
			detalleCarrusel('','','');
		}
		quint.start('left', newLeft+'px');
	});
	btnRetroceder.addEvent('mouseenter', function(){
		this.addClass('over');
	});
	btnRetroceder.addEvent('mouseleave', function(){
		this.removeClass('over');
	});
}