/* Author: Edd Temple-Smithson. Many thanks to Marco Kuiper (http://www.marcofolio.net/) & Nathan Searles (http://nathansearles.com/)
*/

// Speed of the automatic slideshow
var slideshowSpeed = 1000;

// Variable to store the images we need to set as background
var photos = [ {
		"image" : "origami-1.png"
	}, {
		"image" : "origami-2.png"
	}, {
		"image" : "origami-3.png"
	}, {
		"image" : "origami-4.png"
	}, {
		"image" : "origami-5.png"
	}, {
		"image" : "origami-6.png"
	}, {
		"image" : "origami-7.png"
	}, {
		"image" : "origami-8.png"
	}, {
		"image" : "origami-9.png"
	}, {
		"image" : "origami-10.png"
	}, {
		"image" : "origami-11.png"
	}, {
		"image" : "origami-12.png"
	}, {
		"image" : "origami-13.png"
	}, {
		"image" : "origami-14.png"
	}, {
		"image" : "origami-15.png"
	}, {
		"image" : "origami-16.png"
	}, {
		"image" : "origami-17.png"
	}, {
		"image" : "origami-18.png"
	}, {
		"image" : "origami-19.png"
	}, {
		"image" : "origami-20.png"
	}, {
		"image" : "origami-21.png"
	}, {
		"image" : "origami-22.png"
	}, {
		"image" : "origami-23.png"
	}, {
		"image" : "origami-24.png"
	}, {
		"image" : "origami-25.png"
	}
];


var custardTurd = function(){
	
	var interval;	
	var activeContainer = 1;	
	var currentImg = 0;
	var playedImgs = 0;
	var animating = false;
	var navigate = function(direction) {
			
		if( playedImgs == 4 ) {
			stopAnimation();		
		}
	
		// Check if no animation is running. If it is, prevent the action
		if(animating) {
			return;
		}
		
		// Check which current image we need to show
		if(direction == "next") {
			currentImg++;
			if(currentImg == photos.length + 1) {
				currentImg = 1;
				
			}
		} else {
			currentImg--;
			if(currentImg == 0) {
				currentImg = photos.length;
			}
		}
		
		// Check which container we need to use
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}
		
		showImage(photos[currentImg - 1], currentContainer, activeContainer);
		
		playedImgs++;
		
	};
	
	var currentZindex = -1;
	var showImage = function(photoObject, currentContainer, activeContainer) {
		animating = true;
		
		// Make sure the new container is always on the background
		currentZindex--;
		
		// Set the background image of the new active container
		$("#jq-img" + activeContainer).css({
			"background-image" : "url(/wp-content/themes/leapfrogg/images/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
				
		// Fade out the current container
		$("#jq-img" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				animating = false;
			}, 500);
		});
	};
	
	var stopAnimation = function() {	
		// Clear the interval
		clearInterval(interval);
	};
	
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
	$('#slides').slides({
		animationComplete: function(current) {
			playedImgs = 0;
			currentImg = (current + ((current-1)*4)) -1;
			interval = setInterval(function() {
				navigate("next");
			}, slideshowSpeed);
		},
		preload: true,
		preloadImage: '/wp-content/themes/leapfrogg/images/loading.gif',
		play: 8000,
		fadeSpeed: 400,
		effect: 'fade',
		generateNextPrev: false	
	});
	
}

// execute your scripts when the DOM is ready. this is mostly a good habit
$(document).ready( function() {

	// Preload Images 
	for ( i=1; i<26; i++ ){
		var image = $('<img />').attr('src', '/wp-content/themes/leapfrogg/images/origami-' +i+ '.png');
	}
	
	custardTurd();
	
	// initialize scrollable
	$("div#makeMeScrollable").smoothDivScroll({ scrollStep: 3, scrollInterval: 10, autoScrollDirection: 'endlessloopright' });
	
	if (window.PIE) {
        $('.jq-rounded').each(function() {
            PIE.attach(this);
        });
		$('.awesome').each(function() {
            PIE.attach(this);
        });
		$('.awesome-prev').each(function() {
            PIE.attach(this);
        });
	}
	
	$('.triggers ul li a').click( function(){
		var index = $(this).parent('li').index();
		$("#makeMeScrollable").smoothDivScroll("moveToElement", "number", index);
		return false;
	});
	
});




















