var tomavtoOnSubmenu = 0, tomavtoDoHide = true;

function tomavtoHideMenu() {
  if ( tomavtoDoHide && tomavtoOnSubmenu <= 0 ) {
//    jQuery('.submenu').animate( { opacity: 0 }, 'fast', 'linear', function(){
      if ( tomavtoDoHide ) jQuery('.submenu').removeClass( 'hover' );
//    });
  }
}
  
function showActiveKwick() {
  var active = jQuery( '.kwicks .active a' ).add( '.kwicks .active span' );
  if ( active.css( 'display' ) != 'block' ) {
    jQuery( '.kwicks span' ).add( '.kwicks a' ).css( 'display', 'none' ).css( 'opacity', 0 );
    active.css( 'display', 'block' ).animate( { opacity: 1 } );
  }
}

jQuery(function(){
  jQuery('ul.submenu').hover(function(){
    tomavtoOnSubmenu++;
  },function(){
    tomavtoOnSubmenu--;
    tomavtoDoHide = true;
    setTimeout( 'tomavtoHideMenu()', 1000 );
  });
  jQuery('.mainnav>li').hover(function(){
    tomavtoDoHide = false; tomavtoOnSubmenu = 0;
//    jQuery('.submenu').animate( { opacity: 0 }, 0 );
    jQuery('.submenu').removeClass( 'hover' );
    jQuery('.submenu',this).addClass( 'hover' );
//    jQuery('.submenu',this).animate( { opacity: 0.9 }, 0 /*500*/ );
  },function(){
    tomavtoDoHide = true;
    setTimeout( 'tomavtoHideMenu()', 1000 );
  });
  // kwicks init
  if ( jQuery.prototype.kwicks ) {
	  jQuery( '.kwicks' ).kwicks({ max: 200, sticky: true, duration: 300, onAnimateEnd: showActiveKwick });
	  jQuery( '.kwicks span' ).add( '.kwicks a' ).css( 'display', 'none' ).css( 'opacity', 0 );
	  showActiveKwick();
  }
  // accordion init
  if ( jQuery.prototype.accordion ) {
    jQuery('#accordion').accordion({ header: 'h2', autoheight: false }).find('h2').addClass('acc-head');
  }
});
