var HNCarousel = 
{
	carousel: null,
	pe: null,
	length: null,
	direction: 1,
	index: -1,
	duration: 9,

	initialize: function()
	{
		$("carousel_status").hide();
		this.carousel = new UI.Carousel("horizontal_carousel");
		this.makeCells();
		this.makeStatus();
		this.makeSliding();
	},
	
	start: function()
	{
		if(this.pe)
		{
			return;
		}

		this.pe = new PeriodicalExecuter(this.slide.bind(HNCarousel), this.duration);
	},
	
	slide: function()
	{
		if(this.index == this.carousel.currentIndex())
		{
			this.direction = - this.direction;
		}
	
		this.index = this.carousel.currentIndex();
	
		this.carousel.scrollTo(this.carousel.currentIndex() + this.direction);		
	},
	
	stop: function()
	{
		if(this.pe)
		{
			this.pe.stop();
			this.pe = null;
		}
	},
	
	makeSliding: function()
	{
		$("horizontal_carousel").observe("mouseenter", this.stop.bindAsEventListener(HNCarousel));
		$("horizontal_carousel").observe("mouseleave", this.start.bindAsEventListener(HNCarousel));
	
		this.start();
	},

	makeCells: function()
	{
		this.length = $$(".cell").length;
	
		$$(".cell").each(function(cell)
		{
			if(cell.down(".cell_desc"))
			{
				cell.down(".cell_desc").hide();
				
				cell.observe("mouseenter", function()
				{
					$("carousel_status").update("<div>" + cell.down(".cell_desc").innerHTML + "</div>");
					
				});
			}
		});
	},
	
	makeStatus: function()
	{
		if(c = $("horizontal_carousel").down(".container"))
		{
			var options = 
			{
				duration: 0.5,
				queue:
				{
					position: "end",
					scope: "HNCarousel",
					limit: 2
				}
			};
			
			c.observe("mouseenter", function()
			{
				Effect.SlideDown("carousel_status", options);
			});
			
			c.observe("mouseleave", function()
			{
				Effect.SlideUp("carousel_status", options);
			});
		}
	}
};

document.observe("dom:loaded", HNCarousel.initialize.bindAsEventListener(HNCarousel));
