/*
/	This plugin creates a carousels of the container elements.  Many carousels can be created with the same call to JFVCarousel if they require the same settings.
*/

(function($){

	$.fn.JFVCarousel = function(callerSettings) {

		return this.each(function() {

			this.carousel = $.extend({
				slides: '.slide',
				transition: 'slide',
				autoRotate: true,
				transitionSpeed: 1500,
				transitionDelay: 7500,
				loop: true,
				current: 0,
				animationActive: false
			}, callerSettings||{});

			$('.header').append('<ul class="carouselcontrol"><li class="prev">&laquo;</li><li class="next">&raquo;</li></ul>');

			$(this).find('li.prev').bind('click', function() {
				carousel = $(this).parents().filter('.carousel').get(0);
				$(carousel).stopTime();
				carousel.advance(-1);
			});

			$(this).find('li.next').bind('click', function() {
				carousel = $(this).parents().filter('.carousel').get(0);
				$(carousel).stopTime();
				carousel.advance(1);
			});

			$(this).find(this.carousel.slides).each(function(n) {
				this.index = n;
			});

			$(this).bind('click', function() {
				$(this).stopTime(); 
			});
			
			$(this).everyTime(this.carousel.transitionDelay, function() { 
				this.advance(1); 
			});
			
			this.advance = function(frames) {
				if (this.carousel.animationActive ||
					!this.carousel.loop && 
					(this.carousel.current + frames > $(this).find(this.carousel.slides).length ||
					 this.carousel.current + frames < 0)) {
					return;
				}

				this.carousel.current = (this.carousel.current + frames) % $(this).find(this.carousel.slides).length;				

				if (this.carousel.current < 0) {
					this.carousel.current += $(this).find(this.carousel.slides).length;
				}

				if (this.carousel.transition == 'slide') {
					this.carousel.animationActive = true;
				
					if (frames > 0) {
						this.updateView('slidenext');
					}
					else {
						this.updateView('slideprevious');
					}
				}
				else {
					this.updateView(this.carousel.transition);
					this.carousel.animationActive = false;
				}
			}

			this.updateView = function(transitionStyle) {
			
				$(this).find(this.carousel.slides).each(function() {
					
					if ($(this).parent()[0].carousel.current == this.index) {
				
						switch (transitionStyle) {
							case 'slidenext':
								$(this).css({
									left: $(this).width(),
									right: $(this).width()
								}).show().animate({
									left: 0,
									right: 0
								}, {
									duration: $(this).parent()[0].carousel.transitionSpeed,
									easing: 'swing'
								});
								break;
						
							case 'slideprevious':
								$(this).css({
									left: -1 * $(this).width(),
									right: -1 * $(this).width()
								}).show().animate({
									left: 0,
									right: 0
								}, {
									duration: $(this).parent()[0].carousel.transitionSpeed,
									easing: 'swing'
								});
								break;
						
							case 'fade':
								$(this).fadeIn();
								break;
								
							default:
								$(this).show();
								$(this).parent()[0].carousel.animationActive = false;
								break;
						}
					}
					else if ($(this).is(':visible')) {
					
						switch (transitionStyle) {
							case 'slidenext':
								$(this).css({
									left: 0,
									right: 0
								}).animate({
									left: -1 * $(this).width(),
									right: -1 * $(this).width()
								}, {
									duration: $(this).parent()[0].carousel.transitionSpeed,
									easing: 'swing',
									complete: function() {
										$(this).hide().css({
											left: 0,
											right: 0
										});
										$(this).parent()[0].carousel.animationActive = false;
									}
								});
								break;
							
							case 'slideprevious':
								$(this).css({
									left: 0,
									right: 0
								}).animate({
									left: $(this).width(),
									right: $(this).width()
								}, {
									duration: $(this).parent()[0].carousel.transitionSpeed,
									easing: 'swing',
									complete: function() {
										$(this).hide().css({
											left: 0,
											right: 0
										});
										$(this).parent()[0].carousel.animationActive = false;
									}
								});
								break;
							
							case 'fade':
								$(this).fadeOut();
								break;
							
							default:
								$(this).hide();
								$(this).parent()[0].carousel.animationActive = false;
								break;
						}
					}
					else {
						$(this).hide();
					}
				});				
			
			};
			this.updateView();
		}).css('position', 'relative');
	};
	
})(jQuery);
