function dG_showPicture(thumbnail_new, thumbnail_selected)
{
  $picture = thumbnail_selected.parent().parent().prev().children('.picture');
  $picture_desc = $picture.next().children('.preview_desc');
  $picture.css('background-image', 'url(/picture_w.php?ID='+thumbnail_new.attr('picid')+'&gallery=1&maxwidth=640&maxheight=426)');
  $picture_desc.html(((thumbnail_new.attr('title') != '')?thumbnail_new.attr('title'):'&nbsp;'));
}
function dG_navigate(direction, element)
{
  $group = element.parent().parent().next().find('.group:visible');
  $thumbnail_selected = $group.find('.gallerythumb.selected');
  
  if (direction == 'next')
    $thumbnail = $thumbnail_selected.next();
  else if (direction == 'prev')
    $thumbnail = $thumbnail_selected.prev();
  
  if (direction == 'prev')
    $preview_nav = element.next().next();
  else if (direction == 'next')
    $preview_nav = element.prev().prev();
  
  if ($thumbnail.length)
  {
    $group.children('.gallerythumb').css('opacity','0.5');
    $thumbnail_selected.removeClass('selected');
    
    dG_showPicture($thumbnail, $thumbnail_selected);
    
    $thumbnail.css('opacity', '1');
    $thumbnail.addClass('selected');
    
    if (direction == 'next' && $thumbnail.index() > 0 && $preview_nav.hasClass('prevdisabled'))
      $preview_nav.removeClass('prevdisabled');
    else if (direction == 'prev' && $thumbnail.index() < 9 && $preview_nav.hasClass('nextdisabled'))
      $preview_nav.removeClass('nextdisabled');
    
    if (direction == 'next' && !$thumbnail.next().length && element.hasClass('nextdisabled') == false && !$group.next().length)
      element.addClass('nextdisabled');
    else if (direction == 'prev' && !$thumbnail.prev().length && element.hasClass('prevdisabled') == false && !$group.prev().length)
      element.addClass('prevdisabled');
  }
  else if (!$thumbnail.length && (direction == 'next' && $group.next().length) || (direction == 'prev' && $group.prev().length))
  {
    $nav = element.parent().parent().next().next().find('.nav_'+direction);
    dG_navigateGroup(direction, $nav);
  }
}
function dG_navigateGroup(direction, element)
{
  $currentgroup = element.parent().find('.currentgroup');
  $group = element.parent().prev().find('.group:visible');
  $group_nav = (direction == 'next')?($group.next()):($group.prev());
  if ($group_nav.length)
  {
    start = $group_nav.index() * 10 + 1;
    end = start + $group_nav.children('.gallerythumb').length - 1;
    $currentgroup.text(start + '-' + end);
    $group.hide();
    $group_nav.show();
    
    $group_nav.find('.gallerythumb.selected').removeClass('selected');
    $group_nav.children('.gallerythumb').css('opacity','0.5');
    $thumbnail = $group_nav.find('.gallerythumb:'+((direction == 'next')?'first':'last'));
    dG_showPicture($thumbnail, $thumbnail);
    $thumbnail.css('opacity', '1');
    $thumbnail.addClass('selected');
    
    if (direction == 'next' && $group_nav.prev().length)
    {
      if (element.prev().prev().hasClass('prevdisabled'))
        element.prev().prev().removeClass('prevdisabled');
    }
    else if (direction == 'prev' && $group_nav.prev().length)
    {
      if (element.next().next().hasClass('nextdisabled'))
        element.next().next().removeClass('nextdisabled');
    }
    if (direction == 'next' && !$group_nav.next().length && element.hasClass('nextdisabled') == false)
      element.addClass('nextdisabled');
    else if (direction == 'prev' && !$group_nav.prev().length && element.hasClass('prevdisabled') == false)
      element.addClass('prevdisabled');
  }
}
function dG_changeState(thumbnail, group)
{
  $preview_next = thumbnail.parent().parent().prev().find('.navigation').find('.preview_next');
  $preview_prev = thumbnail.parent().parent().prev().find('.navigation').find('.preview_prev');
  
  if (thumbnail.index() > 0 && $preview_prev.hasClass('prevdisabled'))
    $preview_prev.removeClass('prevdisabled');
  if (thumbnail.index() < 9 && $preview_next.hasClass('nextdisabled'))
    $preview_next.removeClass('nextdisabled');
  
  if (!thumbnail.next().length && $preview_next.hasClass('nextdisabled') == false && !group.next().length)
    $preview_next.addClass('nextdisabled');
  if (!thumbnail.prev().length && $preview_prev.hasClass('prevdisabled') == false && !group.prev().length)
    $preview_prev.addClass('prevdisabled');
}
$(document).ready(function()
{
  $('.gallerythumb').css('opacity','0.5');
  $('.gallerythumbnail').click
  (
    function()
    {
      $('.gallerythumb.selected').css('opacity','1');
      $gallery = $('#gallery-'+$(this).attr('id'));
      $overlay = $gallery.children('#overlay');
      $window = $gallery.children('#overlaywindow');
      $close = $window.children('.overlaygalleryheader').children('.overlaygalleryclose');
      $('.overlaybg').css('opacity', '0.8');
      $overlay.fadeIn(500, function()
      {
        mLeft = $(window).width()/2 - $window.width()/2;
        mTop = $(window).height()/2 - $window.height()/2;
        $window.css({'left':mLeft, 'top':mTop});
        $window.fadeIn(500);
      });
      $overlay.click(function() { $window.fadeOut(500, function(){ $overlay.fadeOut(500); }); });
      $close.click(function() { $window.fadeOut(500, function(){ $overlay.fadeOut(500); }); });
    }
  );
  $('.gallerythumb').click
  (
    function()
    {
      $('.gallerythumb').css('opacity','0.5');
      $selected = $(this).parent().find('.gallerythumb.selected');
      $selected.removeClass('selected');
      
      dG_showPicture($(this), $selected);
      dG_changeState($(this), $(this).parent());
      
      $(this).css('opacity', '1');
      $(this).addClass('selected');
    }
  );
  $('.preview_next').click(function(){dG_navigate('next', $(this));});
  $('.preview_prev').click(function(){dG_navigate('prev', $(this));});
  $('.nav_next').click(function(){dG_navigateGroup('next', $(this));});
  $('.nav_prev').click(function(){dG_navigateGroup('prev', $(this));});
});
