// n3std Multi-Slider by MikeGulline.com 
// Get more slide layouts at n3std.com
// enjoy!
(function( $ ){
	$.fn.n3std = function(options) {
		options = $.extend({
			eggDrop			: 0,				// 0 => side scroll, 1 => drop scroll, 2 => ghost scroll
			eggDropDomino	: 100,				// time between items
			eggDropFade		: 500,				// item animation time
			progress		: 1, 				// show progress bar
			minWidth 		: 960,				// min slider width
			fixedWidth		: 0,				// set a fixed slider width
			height			: 295,				// slide height
			slideView 		: 10000,  			// main slide viewing time
			slideTrans 		: 2000,  			// main slide transition time
			slideEasing		: 'easeInOutBack',	// main slide easing
			innerDomino 	: 200, 				// Time Between Inner slide domino
			innerTrans 		: 500,				// inner slide transition time
			innerEasing		: 'easeInOutQuad',	// main slide easing
			background		: '',				// main bg color => '#cccccc'
			// dont edit! ================================================================
			cX 				: 0,				// current slide pos (don't edit)
			c 				: 0,				// current slide number (don't edit)
			s 				: 1,				// for naming slide class (don't edit)
			navClickable 	: 1					// turns off nave while animating (don't edit)
		},options);	

		// adds classes and other other stuff
		// so you don't have to!
		$('#branch').children('li').addClass('nest');
		$('.nest').each(function(){
			i = 1;
			thisSlide = $(this);
			thisSlide.find('.egg').each(function(){
				$(this).addClass('img'+i);
				if(!$(this).find('img').attr('data-cnt')) $(this).find('img').attr('data-cnt', 2);
				i++;
			});	
			wrap = '<div class="pos"><div class="text">'+thisSlide.html()+'</div></div>';
			thisSlide.html(wrap).addClass('s'+options.s);
			options.s++;
		});
		
		if(options.background){
			$('#tree').css('background-color', options.background);
		};
		$('#tree').append('<div class="runnerT"></div><div class="runnerB"></div><div id="n3stdProgress"><div></div></div><div id="n3-nav"><div id="n3-back"> </div><div id="n3-pause"> </div><div id="n3-play"> </div><div id="n3-next"> </div></div>'); 
		if(!options.progress)  $('#n3stdProgress').hide();
		$('#tree, #branch, .pos').height(options.height);
		obj = $('#n3-nav');
		objIn = -($('#tree').height()-72)+'px';
		objOut = -$('#tree').height()+'px';
		$(obj).css('bottom', objOut);
		$('.moveit').each(function(){
			thisObj = $(this);
			thisW = thisObj.width();
			
			btm = thisObj.css('paddingTop');
			btm = parseInt(btm.substring(0,btm.length-2));
			thisH = thisObj.height()+btm;
			thisObj.css({'width': thisW, 'height': thisH, 'position': 'absolute', 'bottom': options.height-thisH-btm+'px'})
		});
		// sets the width and height of the inner slide windows based on the image used
		// slider starts when all images have loaded
		function slideConfig(){
			nogood = 0;
			$('.egg').each(function(){ 
				thisDiv = $(this);					
				thisImage = $(this).find('img');
				if(thisImage.attr('data-cnt')) eggs = thisImage.attr('data-cnt');
				else eggs = 2;
				w = thisImage.width()/eggs;
				h = thisImage.height();
				thisDiv.width(w).height(h);
				if(w==0) nogood++;
			});	
			if(nogood){ 
				setTimeout(function(){slideConfig();}, 100);
			}else {
				window.scrollTo(0,1);
				multi(1);
			};
		};
// triggers nested slides, progress bar and scatter 
		function multi(x){ 
			curSlide = '.s'+(options.c+1);
			loop = 1;
			// triggers inner slide switch
			$(curSlide+' .egg img').each(function(){
				img = $(this);
				if($(img).css('left')=='0px'){ 
					useAm = -$(img).width()/2;
				}else{ 
					useAm = 0;
				};
				innerTrigger = options.slideView/img.attr('data-cnt');
				$(img).delay(innerTrigger+options.innerDomino*loop).animate({left: useAm},  options.innerTrans, options.innerEasing);
				loop++;
			});
			// starts progress bar
			$('#n3stdProgress div').stop().width(0).height(10).animate({width: '100%'},  options.slideView, 'linear', function(){
				$(this).animate({height: 0}, 200);
				scatter();
			});
		};
// drops the content befor next slide
		function scatter(x){
			options.navClickable = 0;
			if(options.eggDrop){
				ds = ($(curSlide).find('.text').children().length-1)*options.eggDropDomino;
				bs = 0;
				newSlide = ds+options.eggDropFade;
				$(curSlide).find('.text').children().each(function(){	
					chick = $(this);	
					btm = chick.css('bottom');
					btm = btm.substring(0,btm.length-2);
					if(options.eggDrop==2){
						chick.delay(ds).fadeTo(options.eggDropFade, 0)
						.delay(options.slideTrans+bs+options.eggDropFade).fadeTo(0, 100);
					}else{
						chick.delay(ds).animate({bottom: -500},  options.eggDropFade, 'easeInQuad')
						.delay(options.slideTrans+bs+options.eggDropFade).animate({bottom: btm},  0, 'easeInQuad');
					};
					ds = ds-options.eggDropDomino;
					bs = bs+options.eggDropDomino;
				});
			}else{
				newSlide = 0;
			};		
			if(x=='b'){
				y = 1;
			}else {
				y = '';
			};
			setTimeout(function(){switchSlide(y)}, newSlide);
		};			
// main slide movement 
		function switchSlide(prev){ 
			// if nav back
			if(prev){
				options.cX += amnt;
				options.c--;
				// if at start, move to end!
				if(options.cX > 0){
					options.cX = -$("ul#branch").width()+amnt;
					options.c = $("ul#branch").width()/amnt-1;
				}
				slideStartPos = options.cX-amnt;
			// else keep moving foreward!
			}else{
				options.cX -= amnt;
				options.c++;
				// if at end, move to start!
				if(options.cX == -$("ul#branch").width()){
					options.cX = options.c = 0;
				};
				slideStartPos = options.cX+amnt;
			};
			if(options.eggDrop){
				$("ul#branch").css({left: slideStartPos}).animate({left: options.cX},  options.slideTrans, options.slideEasing, function(){
					options.navClickable = 1;																													   
					multi(1);
				});
			}else{
				$("ul#branch").animate({left: options.cX},  options.slideTrans, options.slideEasing, function(){
					options.navClickable = 1;																													   
					multi(1);
				});				
			}
		};			
// nav functions 	
		// show nave on hover
		$('#tree').mouseenter(function(){
			if($(obj).css('bottom')==objOut ){
				var n3av = obj.delay(500).animate({bottom: objIn  },  500, 'easeOutBack');
			}else if($(obj).css('bottom')==objIn){
				obj.clearQueue();
			}else {
				var n3av = obj.stop().animate({bottom: objIn  },  500, 'easeInOutBack');
			};
		// hide nave on mouseout
		}).mouseleave(function(){
			if($(obj).css('bottom')==objIn ){
				var n3av = obj.delay(500).animate({bottom: objOut},  500, 'easeInBack');
			}else if($(obj).css('bottom')==objOut ){
				obj.clearQueue();
			}else{
				var n3av = obj.stop().animate({bottom: objOut  },  500, 'easeInOutBack');
			};
		});
		// back, next button
		$('#n3-next, #n3-back').click(function(){
			if(options.navClickable){
				$('#n3-play').hide();
				$('#n3-pause').show();
				$('#n3stdProgress div').stop().animate({height: 0}, 200);
				if($(this).attr('id')=='n3-next'){
					scatter('n');
				}else{
					scatter('b');
				};
			};
		});
		// play, pause button
		$('#n3-pause').click(function(){
			if(options.navClickable){
				$(this).hide();
				$('#n3-play').show();
				$('#n3stdProgress div').stop().animate({height: 0}, 200);
			};
		});
		$('#n3-play').click(function(){
			if(options.navClickable){
				$(this).hide();
				$('#n3-pause').show();
				multi(1);
			};
		});
// sets the main slide widths to be the window width unless fixed =============
		function rest(){ 
			cnt = 0;
			if(options.fixedWidth){
				amnt = options.fixedWidth;
				$('#tree').width(options.fixedWidth);
			}else{
				amnt = $(window).width();
			};
			if(amnt<options.minWidth){amnt = options.minWidth;};
			$("li.nest", "ul#branch").each(function(){
				$(this).width(amnt);
				cnt++;
			});
			options.cX = -options.c * amnt;
			$("ul#branch").width(amnt*cnt).css({left: options.cX});
		};
		$(window).resize(function(){rest();});
		rest();
		slideConfig();	
	};
})( jQuery );
