jQuery(document).ready(function() {
  jQuery('#topnav > ul > li').each(function() { // iterate through topnav parent list items
    var l = jQuery(this).find('.childrens').length; // find the length of childrens in parent list item
    if(l > 0) { // .. childrens exist to the parent category
      jQuery(this).find('a:first').bind('click', function() { // Iterate through all list items
        /* First add the active class to the menu and then hide it to get the slidedown effect */
        jQuery(this).parent().find('.childrens').addClass('active').hide().slideDown('slow');
        
        jQuery(this).parent().find('.childrens > li > a').bind('mouseenter', function() { // Find all the childrens
          // Add hover class to active child category
          jQuery(this).addClass('hover');
          // .. remove hover class from all other child categories
          jQuery(this).parent().parent().find('li > a').not(this).removeClass('hover');
          
          // Slidedown posts from active category
          var current_posts = jQuery(this).parent().find('.posts').slideDown('slow');
          // .. slideup posts from all other categories
          jQuery(this).parent().parent().find('li .posts').not(current_posts).slideUp('slow');
        });
        return false; // Disable click effect
      })
    };
    jQuery('body').not(this).click(function() {
      jQuery(this).parent().find('.childrens').slideUp('slow');
    });    
  });
});