To assign to the non-flipped elements, in order, this should do the trick:
var max = $('div.book div.page:not(.flipped)').length;
$('div.book div.page:not(.flipped)').each(
function() {
$(this).css('z-index', max--);
}
);
Then, to assign to the flipped elements:
var count = 0;
$('div.book div.flipped').each(
function() {
$(this).css('z-index', ++count);
}
);
Since you have multiple book elements on a page, then you would construct it somewhat differently:
$('div.book').each(
function() {
var max = $(this).children('div.page:not(.flipped)').size();
$(this).children('div.page:not(.flipped)').each(
function() {
$(this).css('z-index', max--);
}
);
var count = 0;
$(this).children('div.flipped').each(
function() {
$(this).css('z-index', ++count);
}
);
}
);
To cause this to get reassigned when you click on the book, then put the above code into a function, and call that function in your book click event, like so:
// Slightly modified to work properly, and calls the index function
$('.page').click(function() {
$(this).removeClass('no-anim').toggleClass('flipped');
e.stopPropagation();
indexPages();
});
// Putting zindex assignment into a function for reuse
function indexPages() {
$('div.book').each(
function() {
var max = $(this).children('div.page:not(.flipped)').size();
$(this).children('div.page:not(.flipped)').each(
function() {
$(this).css('z-index', max--);
}
);
var count = 0;
$(this).children('div.flipped').each(
function() {
$(this).css('z-index', ++count);
}
);
}
);
}