To modify your javascript, simply update the SwapFirstLast() function:
Replace
$(this).animate({ 'top' : direction + $(this).height() + 'px' }, 'slow', function() { //animate the img above/under the gallery (assuming all pictures are equal height)
$(this).css('z-index', newZindex) //set new z-index
.animate({ 'top' : '0' }, 'slow', function() { //animate the image back to its original position
inAnimation = false; //reset the flag
});
});
with
$(this).animate({ 'left' : direction + $(this).height() + 'px' }, 'slow', function() { //animate the img above/under the gallery (assuming all pictures are equal height)
$(this).css('z-index', newZindex) //set new z-index
.animate({ 'left' : '0' }, 'slow', function() { //animate the image back to its original position
inAnimation = false; //reset the flag
});
});
Therefore, your function will now look like this:
function swapFirstLast(isFirst) {
if(inAnimation) return false; //return if already swapping pictures
else inAnimation = true; //set the flag indicating processing an image
var processZindex, direction, newZindex, inDeCrease; //variables for previous or next image
if(isFirst) { processZindex = z; direction = '-'; newZindex = 1; inDeCrease = 1; } //set variables for "next" action
else { processZindex = 1; direction = ''; newZindex = z; inDeCrease = -1; } //set variables for "previous" action
$('#pictures img').each(function() { //process each image
if($(this).css('z-index') == processZindex) { //if it's the image to be processed
$(this).animate({ 'left' : direction + $(this).height() + 'px' }, 'slow', function() { //animate the img above/under the gallery (assuming all pictures are equal height)
$(this).css('z-index', newZindex) //set new z-index
.animate({ 'left' : '0' }, 'slow', function() { //animate the image back to its original position
inAnimation = false; //reset the flag
});
});
} else { //not the image to process, only adjust z-index
$(this).animate({ 'top' : '0' }, 'slow', function() { //wait before swapping z-index when image is above/under the gallery
$(this).css('z-index', parseInt($(this).css('z-index')) + inDeCreate); //adjust the z-index by one
});
}
});
return false; //prevent following the clicked link
}
I hope this solution proves helpful.