/*
 * jQuery ContentSlider Plugin v1.0
 * 
 * Copyright 2011, MTD
 * http://themeforest.net/user/MTD
 *
 */
(function($){

	var contentSlider = function( slider, options ){
		
		// Get the options
		var param = $.extend({}, $.fn.contentSlider.defaults, options);
		
		var el	= {
				slider	: $(slider)
			}
		el.sliderList	= el.slider.find(".contentSliderList");
		el.slides		= el.sliderList.children(".contentSlide");
		el.sliderWrap	= $("<div />",{ "class" : 'contentSliderWrapper' });
		
		el.slider.wrap( el.sliderWrap );
		
		var utils	= {
				slidesNum		: el.slides.length,
				isPlaying		: true,
				hovered			: false,
				timer			: false,
				currentIndex	: 0,
				width			: el.slider.width(),
				beforeSlide		: function(){},
				aftesSlide		: function(){}
			}
			utils.nextIndex		= utils.currentIndex +1;
		
		// Add the arrow navigation
		/*if( param.directionNav ){
		
			el.directionNav	= {};
		
			el.directionNav.next	= $("<a />", {
				"class"	: 'csNext csDirectionNav',
				href	: '#'
			}).click(function(){
				slide(el, param, utils);
				return false;
			}).insertBefore( el.slider );
			
			el.directionNav.prev	= $("<a />", {
				"class"	: 'csPrev csDirectionNav',
				href	: '#'
			}).click(function(){
				utils.nextIndex	= utils.currentIndex -1;
				slide(el, param, utils);
				return false;
			}).insertBefore( el.slider );
		
		}*/
		
		// Build the bullet navigation
		if( param.bulletNav ){
		
			el.bulletNav	= {
				wrap	: $("<div />",{ "class"	: 'csNavWrapper' }),
				list	: $("<ul />",{ "class"	: 'csBulletNav' }),
				bullets	: {}
			};
			
			var html	= '';
			for( i=0; i<utils.slidesNum; i++ ){
				html	+= '<li></li>';
			}
			el.bulletNav.list.html( html ).insertAfter( el.slider );
			
			el.bulletNav.list.wrap( el.bulletNav.wrap );
			
			el.bulletNav.bullets = el.bulletNav.list.find("li");
			
			// Set the starting bullet as active
			el.bulletNav.bullets.eq( 0 ).addClass("active");
			
			// Adjust width
			el.bulletNav.list.css({
				width	: el.bulletNav.bullets.eq(0).outerWidth(true)*utils.slidesNum
			});
			
			// Add click event
			el.bulletNav.bullets.click(function(){
				utils.nextIndex	= $(this).index();
				slide(el, param, utils);
				return false;
			});
			
			utils.aftesSlide	= function(el, utils){
				el.bulletNav.bullets.eq( utils.currentIndex ).removeClass("active");
				el.bulletNav.bullets.eq( utils.nextIndex ).addClass("active");
			}
		
		// Build the thumbnail navigation
		}else if( param.thumbnailNav ){
		
			el.thumbnailNav	= {
				wrap	: $("<div />",{ "class"	: 'csNavWrapper' }),
				list	: $("<ul />",{ "class"	: 'csThumbnailNav' }),
				thumbs	: {}
			};
			el.thumbnailNav.wrap.insertAfter( el.slider );
			
			// Build a list of thumbnails
			var html	= '';
			for( i=0; i<utils.slidesNum; i++ ){
				var thumbUrl = el.slides.eq(i).data("thumb") || param.thumbnailNavDefault;
				html	+= '<li>';
					html	+= '<a href="#" style="background-image: url('+thumbUrl+');" ></a>';
				html	+= '</li>';
			}
			el.thumbnailNav.list.html( html ).appendTo( el.thumbnailNav.wrap );
			
			// Get the thumbs
			el.thumbnailNav.thumbs = el.thumbnailNav.list.find("li");
			
			// Set the starting bullet as active
			el.thumbnailNav.thumbs.eq( 0 ).addClass("active");
			
			// Get the width
			el.thumbnailNav.thumbWidth	= el.thumbnailNav.thumbs.eq(0).outerWidth(true);
			
			// Calculate how many thumbnails can fit
			el.thumbnailNav.fittingThumbs	= Math.round(el.thumbnailNav.wrap.width()/el.thumbnailNav.thumbWidth);
			
			// If the thumbnails cannot fit
			if( el.thumbnailNav.fittingThumbs < utils.slidesNum ){
			
				el.thumbnailNav.thumbSet	= 0;
				el.thumbnailNav.thumbSets	= Math.round( utils.slidesNum / el.thumbnailNav.fittingThumbs );
			
				el.thumbnailNav.list.wrap( $("<div />",{ "class"	: 'csThumbnailNavSlider' }) );
				
				el.thumbnailNav.arrowLeft	= $("<a />", {
					"class"	: 'csThumbnailNavArrowLeft',
					href	: '#'
				}).click(function(){
					var nextThumbSet = el.thumbnailNav.thumbSet -1;
					thumbSetSlide(el, param, utils, nextThumbSet);
					return false;
				}).appendTo( el.thumbnailNav.wrap );
				
				el.thumbnailNav.arrowRight	= $("<a />", {
					"class"	: 'csThumbnailNavArrowRight',
					href	: '#'
				}).click(function(){
					var nextThumbSet = el.thumbnailNav.thumbSet +1;
					thumbSetSlide(el, param, utils, nextThumbSet);
					return false;
				}).appendTo( el.thumbnailNav.wrap );
				
				// Show the current set of thumbs
				utils.beforeSlide	= function(el, utils){
					if( utils.nextIndex+1 <= el.thumbnailNav.fittingThumbs ){
						var nextThumbSet	= 0;
					}else{
						var nextThumbSet	= Math.round( (utils.nextIndex+1) / el.thumbnailNav.fittingThumbs );
					}
					thumbSetSlide(el, param, utils, nextThumbSet);
				}
				
			}else{
				// Adjust width
				el.thumbnailNav.list.css({
					width	: el.thumbnailNav.thumbWidth*utils.slidesNum
				});
			}
			
			// Add click event
			el.thumbnailNav.thumbs.click(function(){
				utils.nextIndex	= $(this).index();
				slide(el, param, utils);
				return false;
			});
			
			// Set the callback
			utils.aftesSlide	= function(el, utils){
				el.thumbnailNav.thumbs.eq( utils.currentIndex ).removeClass("active");
				el.thumbnailNav.thumbs.eq( utils.nextIndex ).addClass("active");
			}
		
		}
		
		// Pause on hover
		if( param.pauseOnHover ){
			el.slider.hover(function(){
				utils.hovered	= true;
			}, function(){
				utils.hovered	= false;
			});
		}
		
		// Keyboard Navigation
		if( param.keyboardNav ){
			$(window).keypress(function(e){
				// Left
				if(e.keyCode == '37'){
					utils.nextIndex	= utils.currentIndex -1;
					slide(el, param, utils);
				}
				// Right
				if(e.keyCode == '39'){
					slide(el, param, utils);
				}
			});
		}
		
		// Search for videos
		if( el.slides.find("iframe").length ){
			el.slides.find("iframe").each(function(){
				if( $(this).attr('src').match(/vimeo|youtube/gi) ){
					clearInterval( utils.timer );
					param.autoplay	= false;
				}
			})
		}
		
		// Slide animation
		var slide	= function(el, param, utils){
			
			if( !utils.isPlaying ){
				if(utils.nextIndex	!== utils.currentIndex){
					
					clearInterval( utils.timer );
					utils.isPlaying	= true;
				
					// Check if this is the first or last slide
					if( utils.nextIndex < 0 ){
						utils.nextIndex	= utils.slidesNum -1;
					}else if( utils.nextIndex === utils.slidesNum ){
						utils.nextIndex	= 0;
					}
					
					var left = - (utils.width * utils.nextIndex);
					
					utils.beforeSlide(el, utils);
					
					el.sliderList.animate({
						left	: left
					}, param.speed, param.easing, function(){
						// Callback to adjust the navigation
						utils.aftesSlide(el, utils);
						
						utils.currentIndex	= utils.nextIndex;
						utils.nextIndex		= utils.nextIndex +1;
						utils.isPlaying		= false;
						clearTimer(el, param, utils);
					});
				
				}
			}
			
		};
		
		// Slide to the right set of thumbnails
		var thumbSetSlide	= function(el, param, utils, nextThumbSet){
			if( nextThumbSet < 0 ){
				nextThumbSet	= el.thumbnailNav.thumbSets;
			}else if( nextThumbSet > el.thumbnailNav.thumbSets ){
				nextThumbSet	= 0;
			}
			if( nextThumbSet != el.thumbnailNav.thumbSet ){
				el.thumbnailNav.list.animate({
					left	: -nextThumbSet*el.thumbnailNav.thumbWidth*el.thumbnailNav.fittingThumbs
				}, param.speed, function(){
					el.thumbnailNav.thumbSet	= nextThumbSet;
				});
			}
		}
		
		// Auto-playing timer
		var clearTimer	= function(el, param, utils){
			clearInterval( utils.timer );
			if( param.autoplay ){
				utils.timer = setInterval(function(){
					if( param.pauseOnHover && !utils.hovered ){
						if( !utils.isPlaying ){
							slide(el, param, utils);
						}
					}
				}, param.pause);
			}
		}
		
		// Ready to go
		utils.isPlaying	= false;
		clearTimer(el, param, utils);
		
	}
	
	$.fn.contentSlider = function(options) {
		return this.each(function(key, value){
			new contentSlider( this, options );
		});
	};
	
	//Default settings
	$.fn.contentSlider.defaults = {
		speed				: 1000,
		easing				: 'linear',
		continuous          : false,
		autoplay			: true,
		pause				: 3500,
		pauseOnHover		: true,
		bulletNav			: false,
		thumbnailNav		: true,
		thumbnailNavDefault	: 'img/slider-thumbnail-default.png',
		directionNav		: true,
		keyboardNav			: true
	};

})(jQuery);
