/*
	Scott Slider
	------------
	design@scottluxford.com
	v0.1 Initial Release
	v0.2 Checks for thumbs, added transType setting ( 'slide' | 'fade' )
*/
(function($){
	$.fn.scottSlider = function(options) {

		var defaults = {
			slideWrap:  '.slides',
			slideClass: '.slide',
			thumbClass: '.thumb',
			slideTrans: 'swing',
			slideTimer: 5500,
			slideDur:   500,
			transType:  'slide',
			moreSlide:  '.slide-more',
			moreWrap:   '.slide-more-info',
			moreUrl:    'work/'
		};
		var settings = $.extend(defaults, options);
		
		var slider = $(this);
		
		// Selectors	
		var slideW = $(settings.slideWrap, slider);	
		var slides = $(settings.slideClass, slideW);
		var	thumbs = $(settings.thumbClass, slider);
		var	active = $(settings.slideClass + '.active', slideW);
		
		// Vars
		var	slideWidth 		= active.width();
		var sliderTimer 	= startTimer();
		var slideBusy		= false;
		var oldSlideBusy	= false;
		
		if (slider.length) initialise();
		
		function initialise() {					
			// Thumb Events
			if (thumbs.length) {
				thumbs.each(function(i, thumb) {
					// Reset timer , hide more info and advance to clicked slide
					$(thumb).click(function() {
						clearInterval(sliderTimer);
						hideMore();
						advanceSlide(i);
					});
					// Hover states
					$(thumb).hover(
						function() { $(thumb).not('.active').fadeTo(100, 0.75); },
						function() { $(thumb).not('.active').fadeTo(250, 0.5); }
					);
				});
			}
			
			// Stop timer on enter / restart on leave
			slider.hover(
				function() { clearInterval(sliderTimer); },
				function() { sliderTimer = startTimer(); }
			);
			
			// Show and hide more info
			slides.each(function(i, slide) {
				$(slide).click(function() {
					//if($(settings.moreSlide, slide).is(':hidden')) showMore();
					//else hideMore();
				});
			});
			
			// Activate first thumb
			lightThumb(0);
		}
		
		// Stop advance, load more info from server and store
		function showMore() {
			slideBusy = true;
			var more = $(settings.moreSlide, active);
			more.fadeIn();
			// Load html from server once using rel attr as file pointer
			if(!more.hasClass('loaded')){
				$.get(settings.moreUrl + $(active).attr('rel') + '.php', function(html) {
					more.addClass('loaded').html(html);
					$(settings.moreWrap, more).fadeIn();
					// Apply fancyBox to loaded content
					$('a.preview', more).fancybox({
						'transitionIn':   'elastic',
						'transitionOut':  'elastic',
						'cylic': 		  true,
						'overlayColor':   '#000',
						'padding': 		  0
					});
					// Check if slide inactive (user clicked away before load complete)
					if(!more.parent().hasClass('active')) more.hide();
				});		
			}
		}
		
		// Allow advance and hide info
		function hideMore() {
			slideBusy = false;
			$(settings.moreSlide, active).fadeOut();
		}

		// Start advancing slides at intervals
		function startTimer() {
			return setInterval(function() {
				if(!slideBusy) advanceSlide(null);
			}, settings.slideTimer);
		}
		
		// Advance Slides automatically, or manually through passed index
		function advanceSlide(newSlideIndex) {									
			// Set Old Slide
			var oldSlide = $(active);
			var oldSlideIndex = $(slides).index(oldSlide);

			// Default Determined direction
			var dir = (newSlideIndex > oldSlideIndex) ? 1 : -1;
			
			// Automatic Advance to next slide override direction
			if(newSlideIndex == null) {
				var newSlideIndex = ($(oldSlide).next().length) ? slides.index(oldSlide.next()) : 0;
				dir = 1;
			}

			// Set New Slide
			var newSlide = $(slides[newSlideIndex]);

			// Check if advance required and both animations are complete
			if(newSlideIndex != oldSlideIndex && !slideBusy && !oldSlideBusy) {
				// Prevent additional advances while animating
				slideBusy = oldSlideBusy = true;

				if( settings.transType == 'slide' ) {
					slideSlides(newSlide, newSlideIndex, oldSlide, oldSlideIndex, dir);
				} else if ( settings.transType == 'fade' ) {
					fadeSlides(newSlide, newSlideIndex, oldSlide, oldSlideIndex);
				}				
				
			}
		}
		
		function slideSlides(newSlide, newSlideIndex, oldSlide, oldSlideIndex, dir) {
			// Activate new slide & prepare for entry
			active = $(newSlide);
			lightThumb(newSlideIndex);
			newSlide.css({ 'left': dir * slideWidth, 'top': 0 }).show();

			// Out with the old
			oldSlide.animate({ 'left': -dir * slideWidth }, settings.slideDur, settings.slideTrans, function() {
				oldSlide.removeClass('active');
				oldSlideBusy = false;
			});

			// In with the new, reenable advance when done
			newSlide.animate({ 'left': 0 }, settings.slideDur, settings.slideTrans, function() {
				newSlide.addClass('active');
				slideBusy = false;
			});
		}
		
		function fadeSlides(newSlide, newSlideIndex, oldSlide, oldSlideIndex) {
			// Activate new slide & prepare for entry
			active = $(newSlide);
			lightThumb(newSlideIndex);
			oldSlide.css({ 'z-index': 2 });
			newSlide.css({ 'z-index': 1, 'opacity': 1 }).show();

			// Out with the old, revealing the new
			oldSlide.animate({ 'opacity': 0 }, settings.slideDur, settings.slideTrans, function() {
				oldSlide.removeClass('active').css({ 'z-index': 1 });
				newSlide.addClass('active').css({ 'z-index': 2 });
				oldSlideBusy = false;
				slideBusy = false;
			});
		}

		// Change active thumb 
		function lightThumb(newSlideIndex) {
			if (thumbs.length) {
				var newThumb = $(thumbs[newSlideIndex]);
				$(settings.thumbClass, slider).not(newThumb).fadeTo(250, 0.5).removeClass('active');
				newThumb.addClass('active').css('opacity', 1);
			}
		}
   
	 	return this;
	
	};
})(jQuery);
