/* ------------------------------------------------------------

jQuery.CDAAShowcase();
Denham Haynes & David Baines

Version: 0.95
	
Usage:

	$("#yourid").cdaaShowcase();

------------------------------------------------------------ */


(function ($) {
	
    $.fn.cdaaShowcase = function (options) {
    
			
		// List Default Values
		var defaults = {
			
			// Slider Settings
			portfolioMode:				false,
			animationStyle:				"slide",
			animationLoop:				true,
			autoSlide:					false,
			autoSlideDelay:				4000,
			autoSlideRestart:			false,
			pauseOnHover:				false,
			slideGroups:				false,
			defaultPage: 				"clients",
			tabWidth:					72,
			slideWidth:					940,
			slideGap:					1000,
			slideOpacity:				0.3,
			bulletNavMain:				true,
			bulletNavProject:			true,
			bulletNavHide:				true,
			
			// Arrow Settings
			lastArrowOpacity:			0.3,
			lastArrowAnimateDuration:	500,
			showNext:					true,
			showPrev:					true,
			nextText:					"next",
			prevText:					"previous",
			closeText:					"See more Work",
			nextImage:					"images/new_sprite.png",
			prevImage:					"images/new_sprite.png",
			closeImage:					"images/close.png",
			
			// Multi-Slider Functionality
			cssClass:					"cdaaShowcaseContainer",
			
			// Portfolio Mode Settings
			projectThumbs:				"#dnn_thumbs .Normal",
			projectSlides: 				"#dnn_slides",
			projectQuote: 				"#dnn_blockquote .module_body .Normal",
			projectContent: 			"#dnn_ContentPane .Normal",
			projectScope:				"#dnn_scope .Normal",
			pageFormat:					"aspx",
			projectFolder:				"Work",
			
			// Feature Support
			DNN:						true,
			HTML5: 						true,
			slideShadow: 				true,
			hoverIntent:				true,
			keyboardNav:				true,
			facebook:					false,
			hashbang:					false,
			onProject:					false
		}
		
		// Extending Options
		var options = $.extend(defaults,options);
		
		/* URL Variable Functions 
		-------------------------------------------- */
		// Read a page's GET URL variables and return them as an associative array.
		// http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
		function getUrlVars() {
			var vars = [], hash;
			var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
			for(var i = 0; i < hashes.length; i++) {
				hash = hashes[i].split('=');
				vars.push(hash[0]);
				vars[hash[0]] = hash[1];
			}
			return vars;
		}
		
		// Remove variable from URL
		// http://stuarteske.com/2011/02/how-to-remove-variables-from-a-url-with-javascript/				
		function removeVariableFromURL(url_string, variable_name) {
			URL = String(url_string);
			var regex = new RegExp( "\\?" + variable_name + "=[^&]*&?", "gi");
			URL = URL.replace(regex,'?');
			regex = new RegExp( "\\&" + variable_name + "=[^&]*&?", "gi");
			URL = URL.replace(regex,'&');
			URL = URL.replace(/(\?|&)$/,'');
			regex = null;
			return URL;
		}
		
		// HTML5 element replacement
		if(options.HTML5) {
			var elArticle = "article";
			var elSection = "section";
		} else {
			var elArticle = "div";
			var elSection = "div";
		}
		
		// Slide Widths
		var slideOffset = options.slideWidth + options.slideGap;
		
		// Grab title for portfolio mode later
		var oldTitle = $("title").html();
		
		// check for pushState support is hashbang is disabled, and force hashbang fallback.
		if (typeof(window.history.pushState) != 'function') {
			options.hashbang = true; options.facebook = false;
			
			// If coming in from a ?t= (ie. from a shared facebook link)
			var thisProject = getUrlVars()["t"];
			if(thisProject) {
				var stripped_url = removeVariableFromURL(top.location.href, "t");
				window.location = stripped_url + "#!" + thisProject;
			}
		}
		
		// Start the magic!
		return this.each(function() {

			// Add class to this slider			
			$(this).addClass(options.cssClass);
			
			// thisSlider for internal use, so we don't need to call $("."+options.class) every time :D
			var thisSlider = "."+options.cssClass;
			var thisTabs = $(thisSlider).children(".tabs");
			var thisTab = $(thisTabs).children(".tab");
			$(thisSlider).css({
				"position": "relative"
			});
			
			/* Tab Groups
			------------------------------------------------------------ */
			$(this).wrapInner('<div class="tabs"></div>');
			if(options.slideGroups) {

				// Start the first tab button npx in
				var tabLeft = 20;
				
					if(options.tabWidth) {
						options.tabWidth = options.tabWidth + "px";
					} else {
						options.tabWidth = "auto";
					}
				
				// Do a function for every direct child with a title
				$(thisSlider).children(".tabs").children(elSection+"[data-title]").each(function() {
					
					//alert(tabLeft);
					
					// Add tab class to div
					$(this).addClass("tab");
					
					// Grab Title, put in to var
					var groupTitle = $(this).attr("data-title");
					var groupID = $(this).attr("id");
					
					// Check for Default Page and add active state to button
					if(options.defaultPage == groupID) {
						var currentClass = "active";
						$("#"+options.defaultPage).addClass("currentPage");
					} else {
						var currentClass = ""
					}
					
					// Add Button to .tabs
					$(this).parent(".tabs").prepend('<a class="'+groupID+'Button showcaseButton '+currentClass+'" href="#'+groupID+'" style="left: '+tabLeft+'px; width: '+options.tabWidth+';">'+groupTitle+'</a>');
					
					// Wrap a slide container around the slides
					$(this).wrapInner('<div class="slides '+groupTitle+'_slides"></div>');
					
					// Increment TabLeft
					if(options.tabWidth == "auto") {
					tabLeft += $(".tabs").children("a:first").outerWidth();	
					} else {
					tabLeft += options.tabWidth;
					}
					
				});
				
				// Tab Buttons
				$('.showcaseButton').click(function(){
					if($(this).hasClass("active")) {return false} else {
						
						// Grab target ID
						var thisPage = $(this).attr("href");
						
						// Getting around a horrendous IE7 bug where it will pull the entire URL (http://.....#href)
						if($.browser.msie) {
							var hrefArr = $(this).attr('href').split('#');
							thisPage = hrefArr[hrefArr.length-1];
							thisPage = "#"+thisPage;
						}
						
						// Add hashbang for Google readability
						thisGroup = thisPage.substring(1, thisPage.length);
						//alert(thisPage);
						
						// Fade Out all Pages
						$(thisSlider).children(".tabs").children('.tab').fadeOut(350).removeClass("currentPage").children('.next, .prev').fadeOut(350);
						
						// Fade In New Page!
						$(thisSlider).children(".tabs").children(thisPage).fadeIn(350, function() {$(this).css('filter','');}).addClass("currentPage").children('.next, .prev').fadeIn(350, function() {$(this).css('filter','');});
						$(thisSlider).children(".tabs").children('.showcaseButton').removeClass('active');
						$(this).addClass('active');
						
						// Hide irrelevant buttons, remove the # from the id
						thisGroup = thisPage.substring(1, thisPage.length);
						checkNextPrev(thisGroup);
					}
					
					return false
				});
				
				// Hide all tabs and show the default page
				if(options.defaultPage != undefined) {
					$(".tab").hide();
					$("#"+options.defaultPage).addClass("currentPage").show();
				} else {				
					$(".tab").hide();
					$(".tab:first").addClass("currentPage").show();
					$(".showcaseButton:last").addClass("active");
				}
				
			} else {
				$(thisSlider).children(".tabs").wrapInner('<div class="tab currentPage"></div>');
				$(thisSlider).children(".tabs").children(".tab").wrapInner('<div class="slides '+options.cssClass+'_slides"></div>');
				
			}

			
			/* Setting Up Generic Slide Environment 
			------------------------------------------------------------ */
			
			// Check for DNN usage, if not in DNN, rebuild DNN foundation
			if(!options.DNN) {
				$(thisSlider).children(".tabs").children(".tab").children('.slides').children(elArticle).addClass("module").wrap("<div></div>");
			}
			
			// Graceful degradation - If no slide is found, chuck up a module saying so!
			$(thisSlider).children(".tabs").children(".tab").children(".slides").each(function() {
				if(options.DNN) {
					if($(this).children(".DNNEmptyPane").length > 0) { 
						$(this).children(".DNNEmptyPane").append("<div class='slide_container s0 current_slide' style='left: 0px; opacity: 1; z-index: 90;'><div class='module'><em>No Slides Found!</em></div></div>");
					}
				} else {
					if($(this).length > 0) { var noSlides = true; }
				}
				if(noSlides == true) {
					
				}
			});
			
			// Adding Prev/Next Buttons
			if(options.showPrev) {
				$(thisSlider).children(".tabs").children(".tab").prepend('<div class="prev" style="background-image: url('+options.prevImage+'">'+options.prevText+'</div>');
			}
			if(options.showNext) {
				$(thisSlider).children(".tabs").children(".tab").prepend('<div class="next" style="background-image: url('+options.nextImage+'">'+options.nextText+'</div>');
			}
			
			if(options.onProject) {
				
				// Layout Slides
				$(thisSlider).children(".tabs").children(".tab").children('.slides').children("#dnn_slides").each(function() {
					$(".module", this).children("div").children("div").children("div").children("div").each(function(i){
						$(this).wrap('<div class="slide_container s'+i+'"></div>');
						
						// Setting up slides, depeing on styl
						if(options.animationStyle == "slide") {
							$('.s'+i).css({'left':i*slideOffset, opacity:options.slideOpacity});
							$('.s0').addClass('current_slide').css({opacity:1}).children().children('.module').css({opacity:1, "filter":""});
						} else if (options.animationStyle == "fade") {
							$('.s'+i).css({'left':0, 'top': 0, 'z-index': 0, 'opacity':0});
							$('.s0').addClass('current_slide').css({opacity:1, "z-index":90}).children().children('.module').css({opacity:1, "filter":""});
						}
						
					});
				});
			
			} else {
			
				// Layout Slides
				$(thisSlider).children(".tabs").children(".tab").children('.slides').each(function() {
					$('.module', this).each(function(i){
						$(this).parent('div').wrap('<div class="slide_container s'+i+'"></div>');
						
						// Setting up slides, depeing on styl
						if(options.animationStyle == "slide") {
							$('.s'+i).css({'left':i*slideOffset, opacity:options.slideOpacity});
							$('.s0').addClass('current_slide').css({opacity:1}).children().children('.module').css({opacity:1, "filter":""});
						} else if (options.animationStyle == "fade") {
							$('.s'+i).css({'left':0, 'top': 0, 'z-index': 0, 'opacity':0});
							$('.s0').addClass('current_slide').css({opacity:1, "z-index":90}).children().children('.module').css({opacity:1, "filter":""});
						}
						
					});
				});
			
			// end onProject check
			}
			
			
			// Mini-Slides / Bullet Navigation for Slider
			if(options.bulletNavMain) {
				$(".tab").append("<div class='minislides'></div>");
				$(".tab").each(function() {
					$(this).find(".slide_container").each(function(i) {
						extraI = i+1;
						if(options.slideGroups) {
							var thisTab = $(this).parent("div").parent(".slides").parent(".tab").attr("id");
							thisTab = "#"+thisTab;
						} else {
							var thisTab = ".tab";
						}
						//alert(thisTab)
						
						$thisProject = $(thisSlider).find(thisTab);
						$('<div class="ms ms'+i+'" data-num='+i+'>'+extraI+'</div>').appendTo($(thisTab).find(".minislides"));
						$('.ms'+i).css({'left':(i*36)+20});
					});
					
					// Hiding mini-slides if there's only one slide
					if(options.bulletNavHide) {
					if(extraI == 1) {$(this).children(".minislides").css("display","none");}
					}
				});
				$(".tab").find('.ms0').addClass('current');
			}
			
			/* onLoad Callback
			------------------------------------------------------------ */
			if(options.loadCallback) {
				options.loadCallback;
			}
			

			/* Hiding first/last Arrows
			------------------------------------------------------------ */
				
			// Call first time
			$(thisSlider).children(".tabs").children(".tab").each(function() {
				thisGroup = $(this).attr("id");
				checkNextPrev(thisGroup,0);
			});
			
			// Hiding first previous/last next button
			function checkNextPrev(group,animationSpeed) {

				if(options.animationLoop) {
					return false;
				} else {
				
				if(animationSpeed != 0) {
					animationSpeed = options.lastArrowAnimateDuration;
				}
				
				// Check for Group support, if not using groups use .tabs instead.
				if(group == null) {
					group = ".tab";
				} else {
					// Chuck a hash up in there
					group = "#"+group;
				}
				if(!options.slideGroups) {group = ".tab";}
				//alert(group);
				
				// Check for "Next" availability
				$(group).each(function() {
					if ($(this).children(".slides").find(".slide_container:last").hasClass("current_slide")) {
					
						if($.browser.msie) {
							$(this).children(".next").addClass("disabled").css("opacity",0);
						} else {
							$(this).children(".next").addClass("disabled").animate({"opacity": options.lastArrowOpacity}, {duration: animationSpeed, queue: false});
						}
					} else {
						if($.browser.msie) {
							$(this).children(".next").removeClass("disabled").css("opacity",1);
						} else {
							$(this).children(".next").removeClass("disabled").animate({"opacity": 1}, {duration: animationSpeed, queue: false});
						}
					}
				});
				
				// Check for "Prev" availability
				$(group).each(function(){
					if($(this).children(".slides").find(".slide_container:first").hasClass("current_slide")) {
						if($.browser.msie) {
							$(this).children(".prev").addClass("disabled").css("opacity",0);
						} else {
							$(this).children(".prev").addClass("disabled").animate({"opacity": options.lastArrowOpacity}, {duration: animationSpeed, queue: false});
						}
					} else {
						if($.browser.msie) {
							$(this).children(".prev").removeClass("disabled").css("opacity",1);
						} else {
							$(this).children(".prev").removeClass("disabled").animate({"opacity": 1}, {duration: animationSpeed, queue: false});
						}
					}
				});
				
				}
				
			}
			
			/* AutoSlide
			------------------------------------------------------------ */
			if(options.autoSlide) {
				
				function autoSlide() {
					//$("body").prepend("sent!<br />");
					nextSlide(".currentPage");
				}
				autoSlideGo = setInterval(autoSlide, options.autoSlideDelay);

				// Pause on Hovers!
				if(options.pauseOnHover){
					$(".module",this).hover(function() {
						clearInterval(autoSlideGo);
					}, function() {
						autoSlideGo = setInterval(autoSlide, options.autoSlideDelay);
					});
				}
				
				// Restart Interval Function
				function restartAutoSlide() {
					//$("body").prepend("restarted!");
					// Stop Intervals
					clearInterval(autoSlideGo);
					// Restart if restarted is enabled, otherwise permenantly stop
					if(options.autoSlideRestart) {
						autoSlideGo = setInterval(autoSlide, options.autoSlideDelay);
					}
				}
				
				// Restart conditions
				$(".next").click(restartAutoSlide);
				$(".prev").click(restartAutoSlide);
				$(".showcaseButton").click(restartAutoSlide);
				
				// Keyboard Navigation			
				if(options.keyboardNav) {
					// Keypress Listeners
					function ALcheckKey(e){
						 switch (e.keyCode) {
							case 37:
								restartAutoSlide();
								break;
							case 39:
								restartAutoSlide();
								break;
							default:
								}
					}
					
					// Keypress Listener Support
					if ($.browser.mozilla) {
						$(document).keypress(ALcheckKey);
					} else {
						$(document).keydown(ALcheckKey);
					}
				}
				
			}
			
			/* Slide functionality
			------------------------------------------------------------ */
			
			// Callback for first slide on slider load
			slideCallback('.s0');

			// Keyboard Navigation
			if(options.keyboardNav) {
				// Keypress Listeners
				function checkKey(e){
					 switch (e.keyCode) {
						case 37:
							e.preventDefault();
							if($('#project').css("display") == "block") {
								$(thisSlider).find("#project").find(".prev").click();
							}else if(options.slideGroups)  {
								$(thisSlider).find(".currentPage").find(".prev").click();
							}else{
								$(thisSlider).find(".prev").click();
							}
							break;
						case 39:
							e.preventDefault();
							if($('#project').css("display") == "block") {
								$(thisSlider).find("#project").click();
							}else if(options.slideGroups)  {
								$(thisSlider).find(".currentPage").find(".next").click();
							}else{
								$(thisSlider).find(".next").click();
							}
							break;
						case 27:
							e.preventDefault();
							if($('#project').css("display") == "block") {
								$("#project").children(".close").click();
							}
							break;
						default:
							}
				}
				
				// Keypress Listener Support
				if ($.browser.mozilla) {
					$(document).keypress(checkKey);
				} else {
					$(document).keydown(checkKey);
				}
			}
			
			// Next Button
			$(thisSlider).children(".tabs").children('.tab').children('.next').click(function() {
				nextSlide('.currentPage');
			});
			function nextSlide(parent){
				if(!parent) {parent = ".slides";}
				
				var checkNextExists = $(thisSlider).find(parent).find(".current_slide").next('.slide_container').length;
				//alert("clicked");
				if (checkNextExists == 0){
					//$(this).hide();
					//console.log('no next item');
					if(options.animationLoop) {
						gotoFirst(parent);
						slideCallback('.slide_container:first');
					}
				}else{
					$(thisSlider).children(".tabs").children('.currentPage').children(".prev").stop().show();
					if( $('.current_slide').is(':animated') ) {
						//alert('no its moving dont touch it youll break oh god look what youve done you broke it');
					}else{

						// Animation Style
						if (options.animationStyle == "fade") {
							$(thisSlider).find(parent).find('.slide_container').animate({opacity:0},{queue: false}).css("z-index",0);
							$(thisSlider).find(parent).find('.current_slide').removeClass('current_slide').next('.slide_container').addClass('current_slide').animate({opacity:1},{queue: false}).css("z-index", 90);
							
						} else if (options.animationStyle == "slide") {
							$(thisSlider).find(parent).find('.slide_container').animate({opacity:options.slideOpacity,'left': '-='+slideOffset},{queue: false})
							$(thisSlider).find(parent).find('.current_slide').removeClass('current_slide').next('.slide_container').addClass('current_slide').animate({opacity:1},{queue: false});
							slideCallback('.current_slide');
							
						}
						
						// Bullet Navigation Update
						$(thisSlider).find(parent).find('.minislides .current').removeClass('current').next('.ms').addClass('current');
						
						//alert(checkNextExists);
						if (checkNextExists == 0){
							//$(this).hide();
						}
					}
				}
				
				// Hide irrelevant buttons
				if(parent == "#project") {
					var thisGroup = "project";
				} else {
					var thisGroup = $(thisSlider).children(".tabs").children(".currentPage").attr("id");
				}
				checkNextPrev(thisGroup, options.lastArrowAnimationDuration);
				
			}
			
			// Prev Button
			$(thisSlider).children(".tabs").children('.tab').children('.prev').click(function() {
				prevSlide('.currentPage');
			});
			function prevSlide(parent){
				if(!parent) {parent = ".slides";}
				
				var checkPrevExists = $(thisSlider).find(parent).find(".current_slide").prev('.slide_container').length;
				if (checkPrevExists == 0){
					//alert('no prev item');
					if(options.animationLoop) {
						gotoLast(parent);
						slideCallback('.slide_container:last');
					}
				}else{
					$(this).prev(".next").show();
					if( $('.current_slide').is(':animated') ) {
						//alert('no its moving dont touch it youll break oh god look what youve done you broke it');
					}else{
						
						// Animation Style
						if(options.animationStyle == "slide") {
							$(thisSlider).find(parent).find('.slide_container').animate({opacity:options.slideOpacity,'left': '+='+slideOffset},{queue: false})
							$(thisSlider).find(parent).find('.current_slide').removeClass('current_slide').prev('.slide_container').addClass('current_slide').animate({opacity:1},{queue: false});
							slideCallback('.current_slide');
						
						} else if (options.animationStyle == "fade") {
							$(thisSlider).find(parent).find('.slide_container').animate({opacity:0},{queue: false}).css("z-index",0);
							$(thisSlider).find(parent).find('.current_slide').removeClass('current_slide').prev('.slide_container').addClass('current_slide').animate({opacity:1},{queue: false}).css("z-index",90);
						}

						// Bullet Navigation Update
						$(thisSlider).find(parent).find('.minislides .current').removeClass('current').prev('.ms').addClass('current')
						
					}
				}
				
				// Hide irrelevant buttons
				if(parent == "#project") {
					var thisGroup = "project";
				} else {
					var thisGroup = $(thisSlider).children(".tabs").children(".currentPage").attr("id");
				}
				checkNextPrev(thisGroup, options.lastArrowAnimationDuration);
			}
			
			// Go to First
			function gotoFirst(parent) {
				
				var thisLeft = 0;
				$(thisSlider).find(parent).find('.slide_container').each(function() {
					if(options.animationStyle == "fade") {
						$(this).animate({opacity: 0}).css("z-index",0);
						$(thisSlider).find(parent).find('.current_slide').removeClass('current_slide');
					} else if (options.animationStyle == "slide") {
						$(this).animate({opacity:options.slideOpacity,'left': thisLeft},{queue: false});
						$(thisSlider).find(parent).find('.current_slide').removeClass('current_slide');
						
						// Increment Left Size
						thisLeft += slideOffset;
					}
					
					// Bullet Navigation Update
					$(thisSlider).find(parent).find('.minislides .current').removeClass('current');
					$(thisSlider).find(parent).find('.minislides .ms:first-child').addClass('current');
				});
				$(thisSlider).find(parent).find('.slide_container:first').addClass('current_slide').animate({opacity:1},{queue: false}).css("z-index",90);
			}
			
			// Go to Last
			function gotoLast(parent) {
				// Get number of children
				var thisLeft = $(thisSlider).find(parent).find('.slide_container').size();
				thisLeft = (thisLeft - 1) * -slideOffset;
				$(thisSlider).find(parent).find('.slide_container').each(function() {
					
					if(options.animationStyle == "fade") {
						$(this).animate({opacity:0},{queue: false}).css("z-index",0);
						$(thisSlider).find(parent).find('.current_slide').removeClass('current_slide');
					} else if(options.animationStyle == "slide") {
						// Animate each one
						$(this).animate({opacity:options.slideOpacity,'left': thisLeft},{queue: false});
						$(thisSlider).find(parent).find('.current_slide').removeClass('current_slide');
						
						// Increment Left Size
						thisLeft += slideOffset;
					}
					
					// Bullet Navigation Update
					$(thisSlider).find(parent).find('.minislides .current').removeClass('current');
					$(thisSlider).find(parent).find('.minislides .ms:last').addClass('current');
					
				});
				$(thisSlider).find(parent).find('.slide_container:last').addClass('current_slide').animate({opacity:1},{queue: false}).css("z-index",90);
			}
			
			/* Mini-Slides / Bullet Navigation Controls
			------------------------------------------------------------ */
			$(thisSlider).find(".ms").live("click",function() {
				
				// Don't do anything if you're clicking on the current one.
				if($(this).hasClass("current")) { return false; }
				
				// Grab parent for multislider support
				thisParentID = $(this).parent(".minislides").parent().attr("id");
				if(options.slideGroups) {
				thisParent = "#"+thisParentID;
				} else {
				thisParent = ".currentPage";
				}
				
				// Get "slide" id, remove first two letters ("ms"), parse it as an integer and +1 to get real child #
				var thisId = $(this).attr("data-num");
				thisId = parseInt(thisId);
				
				// Mark mini-slide as active
				$(thisParent+' .minislides .current').removeClass('current');
				$(thisParent+' .minislides .ms'+thisId).addClass('current');
				
				// Animate to that slide.
				// Get number of children
				//var thisLeft = $(thisSlider).find("#project").find('.slide_container').size();
				thisLeft = (thisId) * -slideOffset;
				$(thisSlider).find(thisParent).find('.slide_container').each(function() {
					
					if(options.animationStyle == "fade") {
						$(this).animate({opacity:0},{queue: false}).css("z-index",0);
						$(thisSlider).find(thisParent).find('.current_slide').removeClass('current_slide');
						
					} else if(options.animationStyle == "slide") {
						// Animate each one
						
						$(this).animate({opacity:options.slideOpacity,'left': thisLeft},{queue: false});
						$(thisSlider).find(thisParent).find('.current_slide').removeClass('current_slide');
						
						
						// Increment Left Size
						thisLeft += slideOffset;
						
					}
					$(thisSlider).find(thisParent).find('.s'+thisId).addClass("current_slide").animate({opacity:1},{queue: false}).css({"z-index": 90});
				});
				checkNextPrev(thisParentID, options.lastArrowAnimationDuration);
				slideCallback('.current_slide');
				
			});
			
			/* Mini-slides - Hide if one slide only
			------------------------------------------------------------ */			
			$(thisSlider).find(".minislides").each(function() {
				var countChildren = parseInt($(this).size);
			});
			
			/* Adding slideshow shadows
			------------------------------------------------------------ */
			if(options.slideShadow){
				$(this).find(".slide_container").prepend('<div class="bg png_bg"></div>');
			}
			
			
			/* ------------------------------------------------------------
			
			Fading Transition Callbacks
			
			------------------------------------------------------------ */
			function slideCallback(frame){
				
				// if slideCallback is called with no frame defined, we'll target ".current_slide"
				if(frame==="") {
					frame = ".current_slide";
				}
				
				// defining $frame as an element to make our lives a bit easier
				var $frame = $(frame);
				
				// Debugging
				// console.log($(frame).attr("class"));
				
				
				/* Homepage Animations
				------------------------------------------------------------ */
				if(options.cssClass === "home") {
				
					// Adding animatedElement classes so we can move them all
					$('.home_slides .current_slide h2').addClass('animatedElement');
					$('.home_slides .current_slide p').addClass('animatedElement');
					$('.home_slides .current_slide a').addClass('animatedElement');	
					$('.home_slides .current_slide .submit_shadow').addClass('animatedElement');	
					$('.home_slides .current_slide .capabilities_shadow').addClass('animatedElement');
					//$('.home_slides .current_slide img').addClass('animatedElement_img');	
	
					// Targetting elements
					var sub = $('.home_slides .current_slide .slide_animate_img'),
					text = $('.home_slides .current_slide .animatedElement');
						$('.spud_main').removeClass('spud_animate');




// Check for s0
					if($frame.hasClass("s0")) {
							// Moving the elements in to original position
			
		if(	$.browser.msie){

 $('.aec_mobile_bg').stop().css({top:'0px',left:'0px'});	
							
var animScenes = animScenes || {};
animScenes = {
    aec : $('.aec_mobile_bg'),
    	
	triggerScene1: function(){
       var sq = this;
       this.aec.animate({'top':'-682px'},3000, 
	   function(){ sq.triggerScene2();});
    },
	
	triggerScene2: function(){
       var sq = this;
       this.aec.animate({'top':'0px'},3000, 
	   function(){ sq.triggerScene3();});
    },
	
	
	triggerScene3: function(){
     	var sq = this;
       this.aec.animate({'left':'-184px'},1000, 
	   function(){ sq.triggerScene4();});
    },
	
	triggerScene4: function(){
     	var sq = this;
       this.aec.animate({'left':'-184px'},2000, 
	   function(){ sq.triggerScene5();});
    },
	
	triggerScene5: function(){
       var sq = this;
       this.aec.animate({'left':'0px'},1000,
	   function(){ sq.triggerScene6();});
    },
	
	triggerScene6: function(){
       var sq = this;
       this.aec.animate({'top':'-515px'},3000,
	   function(){ sq.triggerScene7();});
    },
	
	triggerScene7: function(){
       var sq = this;
       this.aec.animate({'left':'-184px'},1000,
	   function(){ sq.triggerScene8();});
    },
	
	triggerScene8: function(){
       var sq = this;
       this.aec.animate({'left':'-184px'},3000,
	   function(){ sq.triggerScene9();});
    },
	
	triggerScene9: function(){
       var sq = this;
       this.aec.animate({'left':'0px'},1000,
	   function(){ sq.triggerScene10();});
    },
	
	triggerScene10: function(){
       var sq = this;
       this.aec.animate({'top':'0px'},3000);
    },
	
	
};
animScenes.triggerScene1();
var animationloop=setInterval(function() {
         animScenes.triggerScene1();
    }, 16000);

						}
					
					}




// Check for s1
					if($frame.hasClass("s1")) {
					// Moving the elements in to original position
					
				
					
					
					$('.beckie_eyes').stop().css({left:'228px',top:'-331px'});
					
					var animScenes = animScenes || {};
					animScenes = {
					beckie : $('.beckie_eyes'),
					
					triggerScene1: function(){
					var sq = this;
					this.beckie.animate({left:'228px',top:'-331px'},3000, 
					function(){ sq.triggerScene2();});
					},
					
					triggerScene2: function(){
					var sq = this;
					this.beckie.animate({left:'237px',top:'-333px'},500, 
					function(){ sq.triggerScene3();});
					},
					
					triggerScene3: function(){
					var sq = this;
					this.beckie.animate({left:'237px',top:'-333px'},3000, 
					function(){ sq.triggerScene4();});
					},
					
					triggerScene4: function(){
					var sq = this;
					this.beckie.animate({left:'235px',top:'-310px'},500, 
					function(){ sq.triggerScene5();});
					},
					
									triggerScene5: function(){
					var sq = this;
					this.beckie.animate({left:'235px',top:'-310px'},3000, 
					function(){ sq.triggerScene6();});
					},
					
					triggerScene6: function(){
					var sq = this;
					this.beckie.animate({left:'228px',top:'-331px'},500);
					}
					
					
					};
					animScenes.triggerScene1();
					
								
					}




			// Check for s2
					if($frame.hasClass("s2")) {
							// Moving the elements in to original position

	
	
	
					/*
					if(	$.browser.msie){
					
					$('.ie_spots').stop().css({opacity:'0'});	
					
					var animScenes = animScenes || {};
					animScenes = {
					dr : $('.ie_spots'),
					lr : $('.light_right'),
					ll : $('.light_left'),
				  
					triggerScene1: function(){
					var sq = this;
					this.lr.rotate({animateTo:2});
					this.lr.animate({'left':'231px'}, 1000);
					
					this.ll.rotate({animateTo:2});
					this.ll.animate({'left':'75px'}, 1000);
					
					this.dr.animate({'left':'-50px'}, 1000);
					this.dr.animate({'opacity':'1'},1000, 
					function(){ sq.triggerScene2();});
					},
					
					triggerScene2: function(){
					var sq = this;
					this.lr.rotate({animateTo:-2});
					this.lr.animate({'left':'234px'}, 1000);
					
					this.ll.rotate({animateTo:-2});
					this.ll.animate({'left':'78px'}, 1000);
					
					this.dr.animate({'left':'50px'}, 1000);
					this.dr.animate({'opacity':'0.8'},1000, 
					function(){ sq.triggerScene1();});
					
					}
					
					};
					animScenes.triggerScene1();
					
					}
					*/
					
					}
					

					// Check for s3
					if($frame.hasClass("s3")) {
							// Moving the elements in to original position

					$('.spud_main').addClass('spud_animate');
	
					}


					
					// Check for s4
					if($frame.hasClass("s5")) {
					// Moving the elements in to original position
					
					if(	$.browser.msie){
					
					
					$('.optimum_lifesaver').stop().css({left:'-5px'});	
					
					var animScenes = animScenes || {};
					animScenes = {
					aec : $('.optimum_lifesaver'),
					
					triggerScene1: function(){
					var sq = this;
					this.aec.animate({'left':'-5px'},1000, 
					function(){ sq.triggerScene2();});
					},
					
					triggerScene2: function(){
					var sq = this;
					this.aec.animate({'left':'5px'},1000, 
					function(){ sq.triggerScene1();});
					}
					
					
					};
					animScenes.triggerScene1();
					
					}
					
					}



				
					// Check for s5
					if($frame.hasClass("s4")) {
					// Moving the elements in to original position
					
			
					
					
					$('.wendys_beat').stop().css({display:'block',opacity:'0'});		
					
					var animScenes = animScenes || {};
					animScenes = {
					wendys : $('.wendys_beat'),
					
					triggerScene1: function(){
					var sq = this;
					this.wendys.animate({'opacity':'1'},200, 
					function(){ sq.triggerScene2();});
					},
					
					triggerScene2: function(){
					var sq = this;
					this.wendys.animate({'opacity':'0'},200, 
					function(){ sq.triggerScene3();});
					},
					
					triggerScene3: function(){
					var sq = this;
					this.wendys.animate({'opacity':'1'},200, 
					function(){ sq.triggerScene4();});
					},
					
					triggerScene4: function(){
					var sq = this;
					this.wendys.animate({'opacity':'0'},200, 
					function(){ sq.triggerScene5();});
					},
					
					triggerScene5: function(){
					var sq = this;
					this.wendys.animate({'opacity':'0'},1000, 
					function(){ sq.triggerScene1();});
					}
					
					
					};
					animScenes.triggerScene1();
					
				
					
					}





					
					
					
					// Every other frame does this
					else {
						
						
					}
						clearInterval(animationloop);
				// Moving the elements in to original position
						$('.home_slides .current_slide .slide_animate_img').css({bottom:'100px',left:'600px',opacity:0});						
						text.css({'left':'900px','opacity':'0'});
						
						// Animating to new position
						$('.home_slides .current_slide .slide_animate_img').animate({opacity:0},800, function() {
							$('.home_slides .current_slide .slide_animate_img').animate({bottom:"-10px",left:"-50px",opacity:1},500);
							text.stop().animate({'left':'390px','opacity':'1'},500);
							
						});					
				}
			}
			
			/* End of magic :( */
		});

    }
	
})(jQuery);
