I think it was just the repeated jquery include causing the problems. I fixed the problem by adding this above the gallery code:
$js_jquery=true;
That tells the gallery that the jquery library has already been loaded to the page so it won't load it again.