var realActive;
var currentActive;
var to;

$(document).ready(function () {
 


 	// set realActive Element
 	for (i = $('#page_header .nav>ul>li').length -1; i >= 0; i--) {
 		var element = $('.nav>ul>li')[i];
 		if ($(element).hasClass("active"))
 			realActive = $(element);
 	};


$("#page_header .nav>ul>li").hover(
  function () {
   		clearTimeout(to);
	        slowOut();            
	        currentActive = $(this);
	        $(currentActive).children('ul').css("display","block"); 
	        $(currentActive).children('ul').css("z-index","101"); 
	        $(currentActive).addClass("active");
  },
  function () {
   		to= setTimeout(function () {
			  slowOut();
		}, 2000);
  }
);



 /*
    $('#page_header .nav>ul>li').mouseover(function(){
      		clearTimeout(to);
	        slowOut();            
	        currentActive = $(this);
	        $(currentActive).children('ul').css("display","block"); 
	        $(currentActive).children('ul').css("z-index","101"); 
	        $(currentActive).addClass("active"); 	        
    }).mouseout(function(){
      		to= setTimeout(function () {
			  slowOut();
		}, 2000);
    });*/
});


function slowOut() {
 	$(currentActive).children('ul').css("display", "none");
 	$(currentActive).children('ul').css("z-index","100"); 
 	$(currentActive).removeClass("active");
 	if ($(realActive)[0] != null) {
 	 	$(realActive).addClass("active");
 		$(realActive).children('ul').css("display", "block");
 	}
    }

