// Copyright 2011 The Aspen Institute. All rights reserved. May not use without permission.
(function($) {
	var methods = {
		
		init: function(options) {
			
			var settings = {
				'delay'			: 7500,
				'overlayDelay'	: 350
			};
			
			var data = this.data('featureCarousel', {});
			
			return this.each(function(){
				
				var $this = jQuery(this);
				
				if (options) { 
					$.extend(settings, options);
				}
				
				// set some data properties that other functions can use
				data.carousel = this;
				data.carouselCount = $('.slide', data.carousel).length;
				data.changing = false;
				data.settings = settings;
				data.slideNum = 1;
				
				
				if (data.carouselCount) {
					
					// add controls at the bottom of the carousel
					for (j=1; j<=data.carouselCount; j++) {
						$('<li>').addClass('num').html('<a href="#">'+j+'</a>').insertBefore($('.controls li.next', data.carousel));
						$('li.num a', data.carousel).click(function(){
							clearTimeout(data.changeTimeout);
							var n = parseInt($(this).html());
							$this.featureCarousel('changeSlide', n);
							return false;
						});
					}
					
					// hide all slides and prep overlays
					$('.slide', data.carousel).hide().find('.overlay').css('opacity', .6).hide().wrapInner('<div class="overlayShim"></div>').parents('.slides').show();
					$('.overlayShim', data.carousel).each(function(){
						var overlay = $(this).parent();
						$(this).css('visibility','hidden');
					});
					
					// enable next/prev actions
					$('li.prev a', data.carousel).click(function(){
						$this.featureCarousel('prevSlide');
						return false;
					});
					$('li.next a', data.carousel).click(function(){
						$this.featureCarousel('nextSlide');
						return false;
					});
					
					// show the first slide
					$('.slide:first', data.carousel).show();
					$('li.num:first', data.carousel).addClass('on');
					data.overlayTimeout = setTimeout(function(){$this.featureCarousel('showOverlay')}, data.settings.overlayDelay);
					
					// show the controls
					$('.controls', data.carousel).show();
					
					// set up the timer to change the slide
					data.changeTimeout = setTimeout(function(){$this.featureCarousel('changeSlide')}, data.settings.delay);
				}
				
				$this.data('featureCarousel', data);
			});
		},
		
		changeSlide: function(num) {
			
			return this.each(function(){
				var $this = jQuery(this);
				
				var data = $this.data('featureCarousel');
				
				if (!data.changing) {
					data.changing = true;
					
					if (typeof num == 'undefined') {
						if (data.slideNum == data.carouselCount) {
							num = 1;
						} else {
							num = data.slideNum+1;
						}
					}
					
					var currentSlide = $('.slide', data.carousel).get(data.slideNum-1);
					var nextSlide = $('.slide', data.carousel).get(num-1);
					
					// hide overlay data
					$('.currentOverlay', data.carousel).hide();
					$('.overlay', currentSlide).hide();
					$('.overlay', nextSlide).hide();
					
					$(currentSlide).fadeOut(250, function(){
						$(nextSlide).fadeIn(250, function(){
							$('li.on', data.carousel).removeClass('on');
							
							data.changeTimeout = setTimeout(function(){$this.featureCarousel('changeSlide')}, data.settings.delay);
							data.slideNum = num;
							if (data.slideNum > data.carouselCount) {
								data.slideNum = 1;
							}
							var nextLi = $('li.num', data.carousel).get(data.slideNum-1);
							$(nextLi).addClass('on');
							
							data.overlayTimeout = setTimeout(function(){$this.featureCarousel('showOverlay')}, data.settings.overlayDelay);
							
							data.changing = false;
						});
					});
				}
				$this.data('featureCarousel', data);
			});
		},
		
		nextSlide: function() {
			
			return this.each(function(){
				var $this = jQuery(this);
				
				var data = $this.data('featureCarousel');
				
				if (!data.changing) {
					clearTimeout(data.changeTimeout);
					$this.featureCarousel('changeSlide');
				}
				return false;
			});
		},
		
		prevSlide: function() {
			
			return this.each(function(){
				var $this = jQuery(this);
				
				var data = $this.data('featureCarousel');
				
				if (!data.changing) {
					clearTimeout(data.changeTimeout);
					var newSlideNum = data.slideNum - 1;
					if (newSlideNum < 1) {
						newSlideNum = data.carouselCount;
					}
					$this.featureCarousel('changeSlide',newSlideNum);
				}
				return false;
			});
		},
		
		showOverlay: function() {
			
			return this.each(function(){
				var $this = jQuery(this);
				
				var data = $this.data('featureCarousel');
				
				var currentSlide = $('.slide', data.carousel).get(data.slideNum-1);
				var currentOverlay = $('.currentOverlay', data.carousel);
				
				data.overlayTimeout = setTimeout(function(){
					var o = $('.overlay', currentSlide);
					var h = o.height();
					var end = 355 - parseInt(h, 10);
					currentOverlay.css({opacity: 0, top: end+'px'}).html('').show();
					o.find('.overlayShim').clone().css('visibility','visible').appendTo(currentOverlay);
					o.css('top', '355px').show().animate({top: end+'px'}, 350, 'swing', function(){
						currentOverlay.animate({opacity: 1}, 350, 'swing', function(){
							
						});
					});
				}, data.settings.overlayDelay);
				
				$this.data('featureCarousel', data);
			});
		}
	};
	
	$.fn.featureCarousel = function(method) {
		
		// Method calling logic
		if ( methods[method] ) {
			return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
			
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
			
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
		}
	};
	
})(jQuery);
