var top_currentPage = 0;
var bottom_currentPage = 0;
var top_nextPagesLeft = 0;
var top_prevPagesLeft = 0;
var bottom_nextPagesLeft = 0;
var bottom_prevPagesLeft = 0;
var links_perScroll = 5;
var carouselULId = 'top-carousel-ul';

function prepareCarousel(carouselUL_id, topNextPages, bottomNextPages, linksPerScroll, carouselStartPage) {
	carouselULId = carouselUL_id == null ? carouselULId : carouselUL_id;
	top_nextPagesLeft = topNextPages == null ? top_nextPagesLeft : topNextPages;
	bottom_prevPagesLeft = bottomNextPages == null ? bottom_prevPagesLeft : bottomNextPages;
	links_perScroll = linksPerScroll == null ? links_perScroll : linksPerScroll;
		
	if ( carouselStartPage > 0 && $(carouselULId) != null ) {	
		new Effect.Move( carouselULId, { x: -150*carouselStartPage*links_perScroll,
								 		 y: 0,
										 duration: 0.0
							   		   }
			   			);

		if ( carouselULId.search("top") != -1 ) {
			top_nextPagesLeft -= (carouselStartPage - top_currentPage);
			top_prevPagesLeft += (carouselStartPage - top_currentPage);
			top_currentPage = carouselStartPage;
		}
		else if ( carouselULId.search("bottom") != -1 ) {
			bottom_nextPagesLeft-= (carouselStartPage - bottom_currentPage);
			bottom_prevPagesLeft+= (carouselStartPage - bottom_currentPage);
			bottom_currentPage = carouselStartPage;
		}
	}
}

function stopListeningOnButtons() {
	$$('#top-carousel-prev').each(function(elm){ elm.stopObserving('click'); });
	$$('#top-carousel-next').each(function(elm){ elm.stopObserving('click'); });	
	$$('#top-carousel-prev').each(function(elm){ elm.observe('click', function(event) { event.stop(); }); });
	$$('#top-carousel-next').each(function(elm){ elm.observe('click', function(event) { event.stop(); }); });
}

function startListeningOnButtons() {
	$$('#top-carousel-prev').each(function(elm){ elm.observe('click', function(event) { event.stop(); slideToPrevPage('top-carousel-ul'); }); });
	$$('#top-carousel-next').each(function(elm){ elm.observe('click', function(event) { event.stop(); slideToNextPage('top-carousel-ul'); }); });
}

function getCurrentPageShown() {
	var page = 0;

	if ( carouselULId.search("top") != -1 ) {
		page = top_currentPage;
	}
	else if ( carouselULId.search("bottom") != -1 ) {
		page = bottom_currentPage;
	}
	
	return page;
}

function getCurrentlySelectedLinkNumber() {
	var ctr = 1;

	$$("ul#"+carouselULId + " li").each(
		function(elm) {
			if ( elm.hasClassName("selected") ) {
				throw $break; // exit .each()
			}
			
			ctr++;
		}
	);
	
	
	return ctr;	
}

function slideToNextPage() {

	/* block input while we're carouseling */
	stopListeningOnButtons();
	/* set a timeout till animation is done, then re-enable input */
		setTimeout('startListeningOnButtons()', 1650);
	
	
	if ( carouselULId.search("top") != -1 ) {
		if ( top_nextPagesLeft > 0 ) {
			// advance to the next page
			new Effect.Move( carouselULId, { x: -150*links_perScroll,
											 y: 0,
											 fps: 30,
											 duration: 1.6,
											 transition: Effect.Transitions.sinoidal
										   }
							);

			top_nextPagesLeft-=1;
			top_prevPagesLeft+=1;
			top_currentPage++;
		}
	}
	else if ( carouselULId.search("bottom") != -1 ) {
		if ( bottom_nextPagesLeft > 0 ) {

			// advance to the next page
			new Effect.Move( carouselULId, { x: -150*links_perScroll,
											 y: 0,
											 fps: 30,
											 duration: 1.6,
											 transition: Effect.Transitions.sinoidal
										   }
							);
	
			bottom_nextPagesLeft-=1;
			bottom_prevPagesLeft+=1;
			bottom_currentPage++;
		}
	}
	
	
	return false;
}

function slideToPrevPage() {

	/* block input while we're carouseling */
	stopListeningOnButtons();
	/* set a timeout till animation is done, then re-enable input */
		setTimeout('startListeningOnButtons()', 1650);

	if ( carouselULId.search("top") != -1 ) {
		if ( top_prevPagesLeft > 0 ) {
			// advance to the next page
			new Effect.Move( carouselULId, { x: 150*links_perScroll,
										 y: 0,
										 fps: 30,
										 duration: 1.6,
										 transition: Effect.Transitions.sinoidal
									   }
					   );
		
		
	
			top_nextPagesLeft+=1;
			top_prevPagesLeft-=1;
			top_currentPage--;
		}
	}
		
	else if ( carouselULId.search("bottom") != -1 ) {
		if ( bottom_prevPagesLeft > 0 ) {
			// advance to the next page
			new Effect.Move( carouselULId, { x: 150*links_perScroll,
										 y: 0,
										 fps: 30,
										 duration: 1.6,
										 transition: Effect.Transitions.sinoidal
									   }
					   );
		
			bottom_nextPagesLeft+=1;
			bottom_prevPagesLeft-=1;	
			bottom_currentPage--;
		}
		
	}
				
	return false;
}

Event.observe( window, 'load', function() { startListeningOnButtons(); });
