function show_gallery(show)
{
  if (show == true)
  {
    $('#gallery .content').css('left', ($('#gallery').width() - 732) / 2);
    var document_height = $(document).height();
    var main_height = $('#absolute_tree_container').height();
    var content_height = $('#main_block').height() + 121;
    $('#gallery').css('height', Math.max(Math.max(document_height,main_height),content_height));
    $('#gallery .background').css('opacity', '0.8');
    $('#gallery').show();
  }
  else
  {
    $('#gallery').hide();
  }
}
function init_gallery()
{
  $('#gallery-pictures img').click(function(event)
  {
    var imgID = ($(this).attr('id')).split('-')[1];
    $('#gallery .content .image').html('<img src="/picture_w.php?ID='+imgID+'&width=640&height=480)" alt="" title="" />');
    selectedImg = imgID;
    show_gallery(true);
  });
  $('#gallery .content .image').click(function(event)
  {
    show_gallery(false);
  });
  $('#gallery .content .left .arrow_left').click(function(event)
  {
    jQuery.each(imgArr, function(i, val)
    {
      if (imgArr[i][0] == selectedImg)
      {
        if ((i-1) >= 0)
        {
          $('#gallery .content .image').html('<img src="/picture_w.php?ID='+imgArr[i-1][0]+'&width=640&height=480)" alt="" title="" />');
          selectedImg = imgArr[i-1][0];
          return false;
        }
      }
    });
  });
  $('#gallery .content .right .arrow_right').click(function(event)
  {
    jQuery.each(imgArr, function(i, val)
    {
      if (imgArr[i][0] == selectedImg)
      {
        if ((i+1) <= (imgArr.length-1))
        {
          $('#gallery .content .image').html('<img src="/picture_w.php?ID='+imgArr[i+1][0]+'&width=640&height=480)" alt="" title="" />');
          selectedImg = imgArr[i+1][0];
          return false;
        }
      }
    });
  });
}
$(document).ready(function()
{
  var selectedImg;
  init_gallery();
});
