/*

BRSlideshow - jQuery Plugin
http://www.brothersroloff.com/labs/

Copyright © 2011 Brothers Roloff
http://www.brothersroloff.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

(function($) {
	var methods = {
		init: function(options) {
				
			return this.each(function() {
				var $this = $(this);
				options = $.extend({}, $.fn.brslideshow.defaults, options);
				
				$this.data('brslideshow', {
					target: $this,
					options: options,
					internal: {
						interval:	null,
						current: 0,
						previous: null,
						index: null,
						width: null,
						height: null,
						animating: false
					}
				});
				
				var data = $this.data('brslideshow');
				var opt = data.options;
				var internal = data.internal;
					
				// auto create slides from template
				
				if(opt.slideTemplate){
					var slideContainer = $this.find(opt.slideTemplate.container);
					$.tmpl($.template(null, opt.slideTemplate.template), opt.templateData).appendTo(slideContainer);
				}
				
				var slides = $this.find(opt.slides);
				
				// auto create pagination navigation - with or without template
				
				if(opt.paginationTemplate){
					var navContainer = $this.find(opt.paginationTemplate.container);
					if(opt.templateData){
						$.tmpl($.template(null, opt.paginationTemplate.template), opt.templateData).appendTo(navContainer);
					} else {
						for(var i=0; i<slides.length; ++i) {
							navContainer.append(opt.paginationTemplate.template);
						}
					}
				}
				
				// prep slide styling
				
				if(opt.animation == 'horizontal' || opt.animation == 'vertical'){
					if(slides.eq(0).width() == 0){
						slides.css({'position': 'relative'}); // fallback for trouble getting measurement
					} 
					internal.width = slides.eq(0).width();
					internal.height = slides.eq(0).height();
				
					slides.css({
						'position': 'absolute',
						'left': '0px',
						'top': '0px',
						'height': internal.height,
						'width': internal.width
					});
				} 
				slides.hide();
				slides.eq(0).show();
					
				// initialize next button
				
				if(opt.nextButton){
					var nextButton = $this.find(opt.nextButton);
					nextButton.attr('href', '#');
					nextButton.click(function(){
						$this.brslideshow('nextSlide');
						return false;
					});
					if(slides.length < 2) nextButton.hide();
				}
				
				// initialize previous button
				
				if(opt.prevButton){
					var prevButton = $this.find(opt.prevButton);
					prevButton.attr('href', '#');
					prevButton.click(function(){
						$this.brslideshow('prevSlide');
						return false;
					});
					if(slides.length < 2) prevButton.hide();
				}

				// initialize pagination navigation
				
				if(opt.pagination){
					var navElements = $this.find(opt.pagination);
					navElements.attr('href', '#');
					navElements.click(function(){
						internal.index = navElements.index(this);
						$this.brslideshow('showSlide', internal.index);	
						return false;
					});	
					navElements.eq(0).addClass(opt.activeClass);
				}
				
				// start automatic playback
				
				if(opt.autoPlay){
					internal.interval  = setInterval(function(){
						$this.brslideshow('nextSlide', true);	
					}, opt.autoPlay);
				}

			});
		},
		
		nextSlide: function(autoPlaying) {
			return this.each(function() {
				var $this = $(this);
				var data = $this.data('brslideshow');
				var opt = data.options;
				var internal = data.internal;

				var slides = $this.find(opt.slides);
				var next = internal.current == slides.length - 1 ? 0 : internal.current + 1;
				autoPlaying ? $this.brslideshow('showSlide', next, true) : $this.brslideshow('showSlide', next);
				
			});
		},
		
		prevSlide: function() {
			return this.each(function() {
				var $this = $(this);
				var data = $this.data('brslideshow');
				var opt = data.options;
				var internal = data.internal;
				
				var slides = $this.find(opt.slides);
				var prev = internal.current ==  0 ? slides.length - 1 : internal.current - 1;
				$this.brslideshow('showSlide', prev);
					
			});
		},
		
		showSlide: function(index, autoPlaying) {
			return this.each(function() {
				var $this = $(this);
				var data = $this.data('brslideshow');
				var opt = data.options;
				var internal = data.internal;
				
				if(internal.animating) return;
			
				var slides = $this.find(opt.slides);					
				if(opt.preventCurrentNavigation) if(index==internal.current) return;
				internal.previous = internal.current;
				internal.current = index;
				if(!autoPlaying && opt.stopAutoPlay) $this.brslideshow('stopAutoPlay');
				
				internal.animating = true;
				switch(opt.animation){
				
				case 'fade':
				// fade animation
					if(opt.slideExit) opt.slideExit();
					slides.eq(internal.previous).fadeOut('fast', function() {
						if(opt.slideExitComplete) opt.slideExitComplete();
						if(opt.pagination){
							var navElements = $this.find(opt.pagination);
							navElements.removeClass(opt.activeClass);
							navElements.eq(index).addClass(opt.activeClass);
						}
						slides.eq(internal.current).fadeIn(function(){
							if(opt.slideEnterComplete) opt.slideEnterComplete();
							internal.animating = false;
						});
						if(opt.slideEnter) opt.slideEnter();
						
					});					
				break;
				case 'crossfade':
				
				// cross-fade animation
					slides.css({
						position: 'absolute'
					});
					if(opt.slideExit) opt.slideExit();
					if(opt.slideEnter) opt.slideEnter();
					slides.eq(internal.previous).fadeOut(function() {
						if(opt.slideExitComplete) opt.slideExitComplete();
						if(opt.pagination){
							var navElements = $this.find(opt.pagination);
							navElements.removeClass(opt.activeClass);
							navElements.eq(index).addClass(opt.activeClass);
						}							
					});	
					slides.eq(internal.current).fadeIn(function(){
						if(opt.slideEnterComplete) opt.slideEnterComplete();
						internal.animating = false;
					});	
						
				break;
				case 'horizontal':
				// horizontal slide animation
				
						if(opt.slideExit) opt.slideExit();
						if(opt.slideEnter) opt.slideEnter();
						
						if(internal.current > internal.previous){
							slides.eq(internal.current).css({'left': internal.width + 'px' });
							slides.eq(internal.current).show();
							slides.eq(internal.previous).animate({'left': '-' + internal.width + 'px' });
							slides.eq(internal.current).animate({'left': '0px' }, function(){
								slides.eq(internal.previous).hide();
								if(opt.slideExitComplete) opt.slideExitComplete();
								if(opt.slideEnterComplete) opt.slideEnterComplete();
								internal.animating = false;
							});
						} else {
							slides.eq(internal.current).css({'left': '-' + internal.width + 'px' });
							slides.eq(internal.current).show();
							slides.eq(internal.previous).animate({'left': internal.width + 'px' });
							slides.eq(internal.current).animate({'left': '0px' }, function(){
								slides.eq(internal.previous).hide();
								if(opt.slideExitComplete) opt.slideExitComplete();
								if(opt.slideEnterComplete) opt.slideEnterComplete();
								internal.animating = false;
							});
						}
												
						if(opt.pagination){
							var navElements = $this.find(opt.pagination);
							navElements.removeClass(opt.activeClass);
							navElements.eq(index).addClass(opt.activeClass);
						}
						
				break;
				case 'vertical':
				// horizontal slide animation
				
						if(opt.slideExit) opt.slideExit();
						if(opt.slideEnter) opt.slideEnter();
						
						if(internal.current > internal.previous){
							slides.eq(internal.current).css({'top': internal.height + 'px' });
							slides.eq(internal.current).show();
							slides.eq(internal.previous).animate({'top': '-' + internal.height + 'px' });
							slides.eq(internal.current).animate({'top': '0px' }, function(){
								slides.eq(internal.previous).hide();
								if(opt.slideExitComplete) opt.slideExitComplete();
								if(opt.slideEnterComplete) opt.slideEnterComplete();
								internal.animating = false;
							});
						} else {
							slides.eq(internal.current).css({'top': '-' + internal.height + 'px' });
							slides.eq(internal.current).show();
							slides.eq(internal.previous).animate({'top': internal.height + 'px' });
							slides.eq(internal.current).animate({'top': '0px' }, function(){
								slides.eq(internal.previous).hide();
								if(opt.slideExitComplete) opt.slideExitComplete();
								if(opt.slideEnterComplete) opt.slideEnterComplete();
								internal.animating = false;
							});
						}
												
						if(opt.pagination){
							var navElements = $this.find(opt.pagination);
							navElements.removeClass(opt.activeClass);
							navElements.eq(index).addClass(opt.activeClass);
						}
						
				break;
				default:
				// no animation
					if(opt.slideExit) opt.slideExit();
					slides.eq(internal.previous).hide();
					if(opt.slideExitComplete) opt.slideExitComplete();
					if(opt.pagination){
						var navElements = $this.find(opt.pagination);
						navElements.removeClass(opt.activeClass);
						navElements.eq(index).addClass(opt.activeClass);
					}
					if(opt.slideEnter) opt.slideEnter();
					slides.eq(internal.current).show();
					if(opt.slideEnterComplete) opt.slideEnterComplete();
					internal.animating = false;
				}
			});
		},
		
		stopAutoPlay: function() {
			return this.each(function() {
				var $this = $(this);
				var data = $this.data('brslideshow');
				var opt = data.options;
				var internal = data.internal;
				
				clearInterval(internal.interval);
					
			});
		}
		
		
	};
	
	$.fn.brslideshow = function(method) {
		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.brslideshow');
	}

	$.fn.brslideshow.defaults = {
		slides: 'div.slide',
		pagination: false, // selector string enables navigation (e.g. 'div.mynav a')
		activeClass: 'active',
		preventCurrentNavigation: true,
		autoPlay: 5000, // false prevents autoplay
		stopAutoPlay: true, // manual navigation cancels autoplay
		animation: 'fade', // false disables animation
		nextButton: null,
		prevButton: null,
		slideExit: null,
		slideExitComplete: null,
		slideEnter: null, 
		slideEnterComplete: null,
		slideTemplate: null, // template string
		paginationTemplate: null, // template string or HTML
		templateData: null // JSON data - required if slideTemplate is specified
	};
})(jQuery);

