jQuery(function ($) {
  if ($('.gallery').length) {
    $('.gallery .holder').after('<div class="switch"></div>')
      .find('ul').cycle({
          pager: 'div.switch',
          fx: 'fade',
          speed: 2000,
          timeout: 6000
      });
  }
  
  if ($('#subnav').length) {
    (function () {
      var t1, t2;
    
      $('#nav a').hover(
        function () {
          var el = this;
          t1 = setTimeout(function () {
            showSubnav(el);
          }, 50);
        }, function () {
          clearTimeout(t1);
        }
      );
    
      $('.nav-container').hover(function () {
        clearTimeout(t2);
      }, function (e) {
        t2 = setTimeout(showSubnav, 75);
      });
    
      function showSubnav(e) {
        $('.subnav-cats').addClass('hide');
        if (!e) {
          $('ul.subnav-cats.active').removeClass('hide');
        }
        else {
          var $sub = $('#' + $(e).attr('id').replace('cat-', 'subnav-'));
          if ($sub.length) {
            $sub.removeClass('hide');
          }
        }
      }
    
    }());
  }
  
});

