// n3std Multi-Slider by MikeGulline.com 
// Get 20 more slide layouts at n3std.com
// enjoy!
(function( $ ){
	$.fn.n3std = function(options) {
		options = $.extend({
			minWidth 		: 960,				// min slider width
			height			: 320,				// slide height
			slideView 		: 10000,  			// main slide viewing time
			slideTrans 		: 2000,  			// main slide transition time
			slideEasing		: 'easeInOutBack',	// main slide easing
			innerTrigger 	: 5000,				// inner slide trigger time
			innerDomino 	: 200, 				// Time Between Inner slide domino
			innerTrans 		: 500,				// inner slide transition time
			innerEasing		: 'easeInOutQuad',	// main slide easing
			cX 				: 0,				// current slide pos (don't edit)
			c 				: 0,				// current slide (don't edit)
			s 				: 1,				// for naming slide class (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);
				i++;
			});
			wrap = '<div class="pos"><div class="text">'+thisSlide.html()+'</div></div>';
			thisSlide.html(wrap).addClass('s'+options.s);
			options.s++;
		});
		$('#tree').append('<div class="runnerT"></div><div class="runnerB"></div>');
		$('#tree, #branch, .pos').height(options.height);
		// 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(){ 
				thisImage = $(this);
				w = thisImage.find('img').width()/2;
				h = thisImage.find('img').height();
				thisImage.width(w).height(h);
				if(w==0) nogood++;
			});	
			if(nogood){ 
				setTimeout(function(){slideConfig();}, 100);
			}else {
				multi(options.c);
				setTimeout(function(){
					switchSlide();				
				}, options.slideView);
			};
		};
		// sets nested slide queues
		function multi(s){ 
			curSlide = '.s'+(s+1);
			loop = 1;
			$(curSlide+' .egg').each(function(){
				img = curSlide+' .img'+loop+' img';
				ani(img, $(img).width()/2, options.innerTrigger+options.innerDomino*loop);
				loop++;
			});
		};
		// nested slide animation
		function ani(ob, am, d){ 
			if($(ob).css('left')=='0px')  useAm = -am;
			else useAm = 0;
			$(ob).delay(d).animate({left: useAm},  options.innerTrans, options.innerEasing);
		};
		// sets the main slide widths to be the window width =============
		function rest(){ 
			cnt = 0;
			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});
		};
		// main slide and nested slide triggers
		function switchSlide(){ 
			options.cX -= amnt;
			options.c++;
			if(options.cX== -$("ul#branch").width()){options.cX = options.c = 0;};
			$("ul#branch").animate({left: options.cX},  options.slideTrans, options.slideEasing, function(){
				multi(options.c);
				setTimeout(function(){switchSlide();}, options.slideView);
			});
		};
		$(window).resize(function(){rest();});
		rest();
		slideConfig();		
	};
})( jQuery );
	
