
$(document).ready(function() {
	
	var animationSpeed = 1000;
	var allowAnimation = true;
	
	$('a.next').click(function() {
    	var self = $(this).closest('.page');
    	var next = self.next();
    	if (!allowAnimation || !next.length)
    		return false;
		allowAnimation = false;
		self.animate({ left: '-100%' }, animationSpeed, 'easeInOutExpo');
		next.animate({ left: '0%' }, animationSpeed, 'easeInOutExpo', function() { allowAnimation = true; });
		return false;
	});
	
	$('a.previous').click(function() {
    	var self = $(this).closest('.page');
    	var previous = self.prev();
    	if (!allowAnimation || !previous.length)
    		return false;
		allowAnimation = false;
		self.animate({ left: '100%' }, animationSpeed, 'easeInOutExpo');
		previous.animate({ left: '0%' }, animationSpeed, 'easeInOutExpo', function() { allowAnimation = true; });
		return false;
	});
	
	$('a.first').click(function() {
    	var self = $(this).closest('.page');
    	var first = $('.page:first');
    	if (!allowAnimation || !first.length)
    		return false;
		allowAnimation = false;
		$('.page').not(first).not(self).css('left', '100%');
		self.animate({ left: '100%' }, animationSpeed, 'easeInOutExpo');
		first.animate({ left: '0%' }, animationSpeed, 'easeInOutExpo', function() { allowAnimation = true; });
		return false;
	});
	
	$('.pages a').click(function() {
		var self = $(this).closest('.page');
		var selfIndex = self.index();
		var selectedIndex = $(this).index();
		var selected = $('.page:nth-child(' + (1 + selectedIndex) + ')');
		if (!allowAnimation || selfIndex == selectedIndex || !selected.length)
    		return false;
    	allowAnimation = false;
    	selected.prevAll().not(self).css('left', '-100%');
    	selected.nextAll().not(self).css('left', '100%');
		self.animate({ left: (selfIndex < selectedIndex ? '-100%' : '100%') }, animationSpeed, 'easeInOutExpo');
		selected.animate({ left: '0%' }, animationSpeed, 'easeInOutExpo', function() { allowAnimation = true; });
		return false;
	});
	
});

