carouselObject = false;

;(function($){

	function init() {

		$('#announcements > ul').jcarousel({
			scroll : 1,
			wrap : 'last',
			auto : 5,
			initCallback: carouselInitCallback,
			itemVisibleInCallback: {
				onBeforeAnimation: itemVisibleInCallbackBeforeAnimation,
				onAfterAnimation:  itemVisibleInCallbackAfterAnimation
			}
		});
/*		
		$('#announcements').mouseover( function() { if(carouselObject) carouselObject.stopAuto(); } );
		$('#announcements').mouseout( function() { if(carouselObject) carouselObject.startAuto(); } );
*/	
	}
	
	carouselIdxHTML = '';
	
	function carouselInitCallback(carousel) {
		// set carousel for external use
		window.carouselObject = carousel;
		// pause autoscrolling if the user moves with the cursor over the clip
		carousel.clip.hover(
			function() { 
				carousel.stopAuto(); 
				carouselIdxHTML = $('#announcements .pagination').html();
				$('#announcements .pagination').html('Paused...') ;
			}, 
			function() { 
				carousel.startAuto(); 
				$('#announcements .pagination').html(carouselIdxHTML); 
			}
		);
	}
	
	function itemVisibleInCallbackBeforeAnimation(carousel, item, idx, state) {
		// the overflow-y:auto setting screws up the animation so we disable it before and re-enable it after
		if(window.slides && slides.length > 1 && slides_started) nextSlide();
		disableOverflow();
	}
	
	function itemVisibleInCallbackAfterAnimation(carousel, item, idx, state) {
		// the overflow-y:auto setting screws up the animation so we disable it before and re-enable it after
		enableOverflow();
		$('#announcements .pagination').html(idx+' of '+carousel.size());
	}
	
	function disableOverflow() {
		$('#announcements .list .item .content').css('overflow','hidden');
	}
	
	function enableOverflow() {
		$('#announcements .list .item .content').css('overflow-y','auto');
	}
	
	$(init);

})(jQuery);