$(document).ready(function() {
   $("#imgbox img").click(function() {
     currentImg = parseInt(this.id.substr(5));
     $("#imgbox img, #infobox, #linkbox, #logobox, .footer").fadeTo("fast", 0.1);
     setImg(galid, currentImg);
     $("#hoverimg").show();
   });

   $("#hoverimg .close").click(function() {
     $("#hoverimg").hide();
     $("#imgbox img, #infobox, #linkbox, #logobox, .footer").fadeTo("fast", 1);
   });

   $("#hoverimg .prev").click(function() {
     prevImg();
   });

   $("#hoverimg .next").click(function() {
     nextImg();
   });
});

var currentImg = 0;

function nextImg() {
  currentImg = (currentImg + 1) % imgCount ;
  setImg(galid, currentImg);
}

function prevImg() {
  currentImg = (currentImg - 1 + imgCount) % imgCount;
  setImg(galid, currentImg);
}

function setImg(gal, img) {
  $("#imgitself").fadeOut('fast', function() {
    $("#imgitself").html('<img onclick="nextImg();" src="werfoto_files/'+gal+'/'+img+'.jpg" alt="" />');
  });
  $("#imgitself").fadeIn('fast');
  //prefetch
  var prefetch_img = $('<img />').attr('src', 'werfoto_files/'+gal+'/'+(currentImg + 1) % imgCount+'.jpg');
}


