//
// create closure
//
(function($) {
	//
	// plugin definition
	//
	$.fn.cover_slider = function(options) {
		// build main options before element iteration
		var opts = $.extend({}, $.fn.cover_slider.defaults, options);
		
		// iterate and reformat each matched element
		return this.each(function() {
			var $slider = $(this);

			// build element specific options
			var o = $.meta ? $.extend({}, opts, $slider.data()) : opts;

			$slider.addClass('positioned');

			$slider
				.bind('next.cover_slider', slideNext)
				.bind('prev.cover_slider', slidePrev);

			var $nextBtn = $('.next', $slider),
				$prevBtn = $('.prev', $slider),
				$images  = $('.images', $slider),
				imgCount = $images.find('img').length;

			$('.control', $slider).hover(
				function() { $(this).addClass('hover'); },
				function() { $(this).removeClass('hover'); }
			);

			//
			// Attach slide left functionality to the "next" button
			// 
			$nextBtn.click(function() {
				slideNext();
			});

			//
			// Attach slide right functionality to the "prev" button
			// 
			$prevBtn.click(function() {
				slidePrev();
			});

			var positions = {
				farLeft:    { height: '160px', width: '120px', left: '15px',   top: '68px' },
				midLeft:    { height: '200px', width: '150px', left: '150px',  top: '48px' },
				center:     { height: '267px', width: '200px', left: '315px',  top: '15px' },
				midRight:   { height: '200px', width: '150px', left: '530px',  top: '48px' },
				farRight:   { height: '160px', width: '120px', left: '695px',  top: '68px' },
				prior:      { height: '160px', width: '120px', left: '-140px', top: '68px' },
				onDeck:     { height: '160px', width: '120px', left: '835px',  top: '68px' },
				reset:      { height: '',      width: '',      left: '',       top: '' }
			};

			function slideNext() {
				if(!isAnimating()) {
					markAnimating();
					o.beforeSlide($slider);
					var $onDeck = $slider.find('.on-deck');

					$images.find('.on-deck, .far-left, .mid-left, .center, .mid-right, .far-right, prior').removeClass('hidden');

					$slider.find('.prior').addClass('hidden').removeClass('prior');
					$slider.find('.far-left img')
						.animate(positions.prior, o.speed, o.easing, function() {
							$(this).parents('a').eq(0).removeClass('far-left').addClass('prior');
							singleAnimationComplete();
						});
					$slider.find('.mid-left img').animate(positions.farLeft, o.speed, o.easing, function() {
							$(this).parents('a').eq(0).removeClass('mid-left').addClass('far-left');
							singleAnimationComplete();
						});
					$slider.find('.center img').animate(positions.midLeft, o.speed, o.easing, function() {
							$(this).parents('a').eq(0).removeClass('center').addClass('mid-left');
							singleAnimationComplete();
						});
					$slider.find('.mid-right img').animate(positions.center, o.speed, o.easing, function() {
							$(this).parents('a').eq(0).removeClass('mid-right').addClass('center');
							singleAnimationComplete();
						});
					$slider.find('.far-right img').animate(positions.midRight, o.speed, o.easing, function() {
							$(this).parents('a').eq(0).removeClass('far-right').addClass('mid-right');
							singleAnimationComplete();
						});
					$onDeck.find('img').animate(positions.farRight, o.speed, o.easing, function() {
							$(this).parents('a').eq(0).removeClass('on-deck').addClass('far-right');
							singleAnimationComplete();
						});
					$onDeck.next('a').addClass('on-deck');
				}
			}


			function slidePrev() {
				if(!isAnimating()) {
					o.beforeSlide($slider);
					markAnimating();
					var $prior = $slider.find('.prior');


					$images.find('.on-deck, .far-left, .mid-left, .center, .mid-right, .far-right, prior').removeClass('hidden');

					$slider.find('.on-deck').addClass('hidden').removeClass('on-deck');
					$slider.find('.far-left img').animate(positions.midLeft, o.speed, o.easing, function() {
							$(this).parents('a').eq(0).removeClass('far-left').addClass('mid-left');
							singleAnimationComplete();
						});
					$slider.find('.mid-left img').animate(positions.center, o.speed, o.easing, function() {
							$(this).parents('a').eq(0).removeClass('mid-left').addClass('center');
							singleAnimationComplete();
						});
					$slider.find('.center img').animate(positions.midRight, o.speed, o.easing, function() {
							$(this).parents('a').eq(0).removeClass('center').addClass('mid-right');
							singleAnimationComplete();
						});
					$slider.find('.mid-right img').animate(positions.farRight, o.speed, o.easing, function() {
							$(this).parents('a').eq(0).removeClass('mid-right').addClass('far-right');
							singleAnimationComplete();
						});
					$slider.find('.far-right img').animate(positions.onDeck, o.speed, o.easing, function() {
							$(this).parents('a').eq(0).removeClass('far-right').addClass('on-deck');
							singleAnimationComplete();
						});
					$prior.find('img').animate(positions.farLeft, o.speed, o.easing, function() {
							$(this).parents('a').eq(0).removeClass('prior').addClass('far-left');
							singleAnimationComplete();
						});
					$prior.prev('a').addClass('prior');
				}
			}


			function resetViewport() {
				$images.find('a.cover-link img').css(positions.reset);
				$images.find('a.cover-link').removeClass('prior far-left mid-left center mid-right far-right on-deck').addClass('hidden');
				$images.find('a.cover-link').not('.clone').removeClass('hidden')
					.eq(0).addClass('far-left')
					.next('a.cover-link').addClass('mid-left')
					.next('a.cover-link').addClass('center')
					.next('a.cover-link').addClass('mid-right')
					.next('a.cover-link').addClass('far-right')
					.next('a.cover-link').addClass('on-deck').find('img').hide().end()
					.nextAll('a.cover-link').addClass('hidden').find('img').hide();
				$images.find('a.clone img').hide();
				$images.find('a.far-left').prev('a').addClass('prior');
			}


			//
			// is the cover slider currently animating?
			// 
			function isAnimating() {
				return $slider.data('animating');
			}

			//
			// Diable the next/prev buttons
			//
			function markAnimating() {
				$slider.data('animating', true).addClass('animating').removeClass('positioned');
			};

			//
			// Enable the next/prev buttons
			//
			function unmarkAnimating() {
				$slider.data('animating', false).addClass('positioned').removeClass('animating');
			};


			function singleAnimationComplete() {
				var animations_remaining = 0;
				$images.find('img').each(function() {
 					animations_remaining += $(this).queue("fx").length;
				});
				if(animations_remaining === 0) {
					animationsComplete();
				}
			}

			//
			// pseudo event, when all animations have completed
			// 
			function animationsComplete() {
				if($images.find('.center').hasClass('reset-trigger')) {
					resetViewport();
				}
				unmarkAnimating();
				// remove all hidden clones
				o.afterSlide($slider);
			}

		});
	};

	//
	// plugin defaults
	//
	$.fn.cover_slider.defaults = {
		speed: 500,
		easing: 'easeOutQuad',
		afterSlide: function($slider) {},
		beforeSlide: function($slider) {}
	};
//
// end of closure
//
})(jQuery);
