function nice_menu(element, target) {
	//debugger;

  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() {
	/* Main Menus */
	$('.k2menu>a').click(function() {
		//debugger;
		var reSub = /.*(sub).*/i;
		var hasSubMenus = reSub.test($(this).attr("class"));
		if (hasSubMenus) {
			var _menu_id = String($(this).parent().attr("id")).split("-")[1];
			nice_menu('#k2menu-m-' + _menu_id, this);
			return false;
		} else {
			window.location.href = $(this).attr("href");
			return false;
		}
	});
	
	/* Sub Menus */
	/*
	$('.menu>li').hover(function() {
		var hasActive = $(this).find('ul').find('li').find('a').is('li a.active');
		if(!hasActive){
			$(this).find('ul').show('slow');
		}
	},
	function() {
		var hasActive = $(this).find('ul').find('li').find('a').is('li a.active');
		if(!hasActive){
			$(this).find('ul').hide('slow');
		}
	});*/
	
	var navTimers = [];
	$(".menu>li").hover(function() {
		var hasActive = $(this).find('ul').find('li').find('a').is('li a.active');
		if (!hasActive) {
			var id = jQuery.data(this);
			var $this = $(this);
			navTimers[id] = setTimeout(function() {
				//$this.children('ul').fadeIn(500);
				$this.children('ul').slideDown(500);
				navTimers[id] = "";
			},300);
		}
	},
	function() {
		var hasActive = $(this).find('ul').find('li').find('a').is('li a.active');
		if (!hasActive) {
			var id = jQuery.data(this);
			if (navTimers[id] != "") {
				clearTimeout(navTimers[id]);
			} else {
				//$(this).children("ul").fadeOut(400);
				$(this).children("ul").slideUp(400);
			}
		}
	});

});

