function menu_interval_action() {
  $('#menu li ul').each(function() {
    if ($(this).attr('rel') == 'hide') $(this).slideUp('fast');
  });
  if ($('#menu li ul:visible').length == 0) $menu_z = 0;
}

$(document).ready(function() {
  /**** otevirani a zavirani contentu *****/
  $('#bgnd div').hide();
  $('#lang').prepend('<a id="open" class="state-open"></a>');
  $('#open').click(function() {
    if ($(this).hasClass('state-open')) {
      $('#bgnd div').show();
      $('#content').slideUp('slow', function() { $('#open').removeClass('state-open'); });
    } else {
      $('#content').slideDown('slow', function() { $('#open').addClass('state-open'); $('#bgnd div').hide(); });
    }
  });
  /***** menu *****/
  $menu_z = 0;
  $('#menu a').each(function() {
    if ($(this).siblings('ul').length == 0) return;
    $(this).mouseover(function() {
      $(this).siblings('ul').attr('rel','show').css('z-index', ++$menu_z).slideDown('fast');
    }).mouseout(function() {
      $(this).siblings('ul').attr('rel','hide');
    });
    $(this).click(function(e) { e.preventDefault; return false; }); // pokud to ma submenu, tak je to neklikatelny
  });
  $('#menu li ul').mouseover(function() {
    $(this).attr('rel','show');
  }).mouseout(function() {
    $(this).attr('rel','hide');
  });
  setInterval('menu_interval_action()', 500);

  /***** pozadi *****/
  $(window).resize(function() {
    $img = $('#bgnd img');
    $ratio = $img.attr('height') / $img.attr('width');
    $img.width = $(window).width();
    $img.height = Math.round($ratio * $img.width);
    $img.left = 0;
    /*$img.top = $(window).height() - $img.height;
    if ($img.top > 0) {
      $img.top = 0;
      $img.height = $(window).height();
      $img.width = Math.round($img.height / $ratio);
      $img.left = -Math.round(($img.width - $(window).width()) / 2);
    }*/
    $img.css({'top': 0, 'left' : $img.left, 'width' : $img.width, 'height' : $img.height});
    //$('#debug').html('width: '+$img.width+"\nheight: "+$img.height+"\ntop: "+$img.top+"\nleft: "+$img.left);
  }).trigger('resize');
  
  /***** galerie *****/
  $('div.gallery a').colorbox({
    height: 600,
    initialHeight: 600,
    current: '{current}/{total}',
    opacity: 1,
    title: function() {
      $gal_popis = $(this).attr('title').split(',', 2);
      if ($gal_popis[1] === undefined) $gal_popis[1] = '';
      return '<strong>'+$gal_popis[0]+'</strong> '+$gal_popis[1];
    }
  });
  $('div.gallery a.video').colorbox({
    height: 600,
    initialHeight: 600,
    iframe:true, innerWidth:425, innerHeight:344,
    current: '{current}/{total}',
    opacity: 1,
    title: function() {
      $gal_popis = $(this).attr('title').split(',', 2);
      if ($gal_popis[1] === undefined) $gal_popis[1] = '';
      return '<strong>'+$gal_popis[0]+'</strong> '+$gal_popis[1];
    }
  });
});
