/* CAROUSELS
--------------------------------------------- */
var carouselTimer
//hack to detect mouse position
var noScroll = false;
var clicked = false;

//this function starts the timer on the carousel (clearing it first)
function timeCarousel(maxElements) {
	//set the slide time to live in milliseconds
	var slideTime = 4000;
	clearTimeout(carouselTimer);
	carouselTimer = setTimeout("upCarousel(maxElements)",slideTime);
}

//this function advances the carousel only if the mouse is in the area
function upCarousel(maxElements) {
	//only move if the mouse is not in the area
	if(noScroll == false) {
		if(Math.round(hCarousel.currentIndex()) < maxElements) {
			hCarousel.scrollTo(hCarousel.currentIndex()+1);	
		}
		else {
			hCarousel.scrollTo(0);	
		}
	}
}

//this function mounts the listeners for the carousels
function listenToCarousel(carouselId) {
	Event.observe(carouselId, 'mouseover', function() {
		noScroll = true;
	});
	Event.observe($('carousel-direct-nav'), 'mouseover', function() {
		noScroll = true;
	});	
	Event.observe(carouselId, 'click', function() {
		noScroll = true;
		clicked = true;
	});
	Event.observe($('carousel-direct-nav'), 'click', function() {												  
		noScroll = true;
		clicked = true;
	});	
	Event.observe(carouselId, 'mouseout', function() {
		if (!clicked) {
			noScroll = false;
			timeCarousel(maxElements);
		}
	});
	Event.observe($('carousel-direct-nav'), 'mouseout', function() {
		if (!clicked) {
			noScroll = false;
			timeCarousel(maxElements);
		}
	});
}
