function nice_menu(element, target) {
  var $theDiv = $(element);
  var $visibleSiblings = $theDiv.siblings(':visible');

  if ($visibleSiblings.length) {
    // underline the clicked target
    $('.k2menu>a').removeClass("active");
    $(target).addClass("active");
    
    // hide element
    $theDiv.animate({
      "opacity" : 0
    }, 200);
    
    // hide the siblings and wait till ready
    $visibleSiblings.animate({
      "opacity" : 0
    }, 200, 'swing', function() {
      
      if ($visibleSiblings.height() == $theDiv.height()) {
        // if no height change needed quickly change the height to avoid line jitter
        
        // take siblings out
        $visibleSiblings.animate({
          "height" : "hide"
        }, 1, 'swing', function() {
          $visibleSiblings.animate({
            "opacity" : 1
          }, 400);
        });
        // show element
        $theDiv.animate({
          "height" : "show"
        }, 1, 'swing', function() {
          $theDiv.animate({
            "opacity" : 1
          }, 400);
        });
      }
      else {
        // if height change IS needed transition the height
      
        // take siblings out
        $visibleSiblings.animate({
          "height" : "hide"
        }, 400, 'swing', function() {
          $visibleSiblings.animate({
            "opacity" : 1
          }, 200);
        });
        // show element
        $theDiv.animate({
          "height" : "show"
        }, 400, 'swing', function() {
          $theDiv.animate({
            "opacity" : 1
          }, 200);
        });
      }
      
    });
  } 
  else {
    // toggle the element
    $theDiv.animate({
      "height" : "toggle",
      "opacity" : "toggle"
    }, 400);
    // underline handling
    $(target).toggleClass("active");
  }
}

$(function() {
  $('#k2menu-clothing_equipment>a').click(function() {
    nice_menu('#k2menu-m-clothing_equipment', this);
    return false;
  });

  $('#k2menu-marmotpro>a').click(function() {
    nice_menu('#k2menu-m-marmotpro', this);
    return false;
  });

  $('#k2menu-support>a').click(function() {
    nice_menu('#k2menu-m-support', this);
    return false;
  });

  $('#k2menu-search>a').click(function() {
    nice_menu('#k2menu-m-search', this);
    return false;
  }); 

});
