var Carousel = (function() {
	
	var animating = false;
	var duration = 400;
	var distance = 50;
	var autoRotateTime = 5000;
	var autoRotateOn = true;
	
	// ------------------------------------------------------------------------
	var init = function() {
		console.log('Carousel.init()');
		
		$('#carousel .left.button').bind('click', function(){
			moveLeft();
			autoRotateOn=false;
		});
		$('#carousel .right.button').bind('click', function(){
			moveRight();
			autoRotateOn=false;
		});
		$('#carousel .circles .circle').bind('click', function(evt) {
			Carousel.moveTo($(this).index());
			autoRotateOn=false;
		});
		
		$("#carousel .left.button").mouseover(function(){
			$("#carousel .left.button").css("opacity",".5");
		}).mouseout(function(){
			$("#carousel .left.button").css("opacity","1");
		});
		
		$("#carousel .right.button").mouseover(function(){
			$("#carousel .right.button").css("opacity",".5");
		}).mouseout(function(){
			$("#carousel .right.button").css("opacity","1");
		});
		
		setTimeout("Carousel.autoRotate()",autoRotateTime);
	};
	
	// ------------------------------------------------------------------------
	var moveLeft = function()
	{
		moveTo(true);
	};
	
	// ------------------------------------------------------------------------
	var moveRight = function()
	{
		moveTo(false);
	};
	
	var autoRotate = function()
	{
		if (!autoRotateOn) return false;
		
		moveRight();
		setTimeout("Carousel.autoRotate()",autoRotateTime);
	}
	
	// ------------------------------------------------------------------------
	var moveTo = function(arg) {
		console.log('moveTo(' + arg + ')');
		if(animating) return;
		
		var $activeSlide = $('#carousel .slides .active.slide');
		var $nextSlide;
		var $activeCircle = $('#carousel .circles .active.circle');
		var $nextCircle;
		var toLeft;
		
		//If a number is passed, jump to that slide
		if(typeof arg == 'number')
		{
			$nextSlide = $activeSlide.parent().children('div:eq(' + arg + ')');
			if(!$nextSlide.length || $activeSlide.index() == arg) return;
			toLeft = arg > $activeSlide.index();
		}
		
		//If "true" was passed, rotate left
		else if(arg)
		{
			toLeft = true;
			if($activeSlide.index() == 0) $nextSlide = $activeSlide.parent().children().last();
			else $nextSlide = $activeSlide.prev();
		}
		
		//If "false" or any other value is passed, rotate right
		else
		{
			toLeft = false;
			if($activeSlide.index() + 1 == $activeSlide.parent().children().length) $nextSlide = $activeSlide.parent().children().first();
			else $nextSlide = $activeSlide.next();
		}
		
		//Hide the active slide
		$activeSlide
			.removeClass('active')
			.css({
				'visibility': 'visible',
				'opacity': '1.0'
			})
			.animate({
				left: (toLeft ? '-=' : '+=') + distance,
				opacity: '0'
			}, duration, null, function() {
				$(this).css('visibility', 'hidden');
		});
		
		//Show the next slide and mark it active
		$nextSlide
			.addClass('active')
			.css({
				'visibility': 'visible',
				'opacity': '0',
				'left': (toLeft ? '' : '-') + distance + 'px'
			})
			.animate({
				'left': '0',
				'opacity': '1.0'
			}, duration);
		
		animating = setTimeout(function() {animating = false;}, duration);
		
		//remove the active state of the current circle, set the next circle to be active
		$('.circles .circle').removeClass('active');
		$(".circles .circle:nth-child("+parseInt($nextSlide.index()+1)+")").addClass('active');
		
	};
	
	// PUBLIC INTERFACE -------------------------------------------------------
	return {
		init: init,
		moveLeft: moveLeft,
		moveRight: moveRight,
		moveTo: moveTo,
		autoRotate: autoRotate
	};
	
})();
$(document).ready(Carousel.init);
