var Carousel = {
	init: function(){		
		Carousel.module = $('also_available_module');
		Carousel.ul = Carousel.module.down('#also_available_carousel');
		Carousel.lis = Carousel.ul.select('li');
		Carousel.liWidth = Carousel.lis[0].getWidth();
		Carousel.ulWidth = Carousel.liWidth * Carousel.lis.length

		Carousel.ul.setStyle({
			width: Carousel.ulWidth + "px"
		});

		Carousel.module.insert("<a id='also_available_carousel_button_left'></a><a id='also_available_carousel_button_right'></a>")

		Carousel.btnL = $('also_available_carousel_button_left');
		Carousel.btnR = $('also_available_carousel_button_right');
		
		Carousel.btnL.addClassName("disabled");
		
		Carousel.setObservers();
	},
	setObservers: function(){
		Carousel.btnR.observe('click', function(e){
			if(!Carousel.btnR.hasClassName("disabled")){
				Carousel.shiftCarousel(-Carousel.liWidth);
			}
			return false;	
		});

		Carousel.btnL.observe('click', function(e){
			if(!Carousel.btnL.hasClassName("disabled")){
				Carousel.shiftCarousel(Carousel.liWidth);
			}
			return false;
		});
	},
	shiftCarousel: function(distance){
		new Effect.Move(Carousel.ul, {
			x: distance, 
			transition: Effect.Transitions.sinoidal,
			duration: 0.5,
			queue: { position: 'end', scope: 'carouselscope', limit: 1 },
			afterUpdate: Carousel.classBuilder(parseInt(Carousel.ul.getStyle("left")) + distance)
		});
	},
	classBuilder: function(left){
		Carousel.btnL.removeClassName("disabled");
		Carousel.btnR.removeClassName("disabled");
		if(left==0){
			Carousel.btnL.addClassName("disabled");
		}else if(left==(-(Carousel.ulWidth-(Carousel.liWidth*4)))){
			Carousel.btnR.addClassName("disabled");
		}
	}
}

Event.observe(window, 'load', function() {
	if ($('also_available_carousel_wrapper')) Carousel.init();
});