XML Gallery

I am using a XML gallery slideshow and on the next and prev. buttons I would like them when they are either at the end or at the begining to go to the one before and keep looping instead of stopping.
What do I need to change on my next or prevs?

function nextImage() {
if (p<(total-1)) {
p++;
if (loaded == filesize) {
picture._alpha = 0;
picture.loadMovie(image[p], 1);
picture_num();
slideshow();
}
}
}
function prevImage() {
if (p>0) {
p–;
if (loaded == filesize) {
picture._alpha = 0;
picture.loadMovie(image[p], 1);
picture_num();
}
}
}
Any help is gladly appreciated. This is from the tutorials slideshow.