/* top menu start*/
var topMenuDelay = 200;
var menuWidth = 958;
var totalSetWidth = 0;

$(document).ready(function()
{
    $(".nav ul").data("tocloseid", 0);

//	var ulKids = $("ul.nav").children().length;
//	var liWidth =  Math.round(menuWidth / ulKids);
//	$("ul.nav").children().css("width", liWidth + "px");
//	
//	$("ul.nav .mn_inlinewidth").each(function(){
//		var mnWidth = $(this).css("width").replace("px", "") * 1;
//		totalSetWidth = totalSetWidth + mnWidth;
//	});
//	var ulOutlineKids = $("ul.nav .mn_outlinewidth").length;
//	var liWidth = Math.round((menuWidth - totalSetWidth) / ulOutlineKids);
//	$("ul.nav .mn_outlinewidth").css("width", liWidth + "px");
//	
//	
//	$("ul.nav ul, ul.nav li").filter(
//		function(){ 
//			return $(this).width() == 1;
//		}
//	).css("width", "150px");
    

    $(".nav li").hover(
    /*On hover function*/
    function()
    {
    	
    	if ($(this).children("ul").data("tocloseid") == 0)
        {

            $(this).siblings().children("ul").css("visibility", "hidden");
            $(this).siblings().children("ul").data("tocloseid", 0);
            //$(".nav ul").css("visibility", "hidden");
        }
    	
        /*check that the LI has a sub menu*/
        if ($(this).children("ul").attr("tagName") == "UL")
        {
            /*check if the submenu has a timer running to close it, and cancel the timer if it exists*/
            if ($(this).children("ul").data("tocloseid") > 0)
            {
                clearTimeout($(this).children("ul").data("tocloseid"));
            }
            
            //$("#alertText").text( $(this).parent().attr("class") );
          	if ($(this).parent().attr("class") != "nav"){
            	$(this).children("ul").css("margin-left", $(this).parent().width());
            }
               
            /*set the sub menu's visibility on*/
            $(this).children("ul").css("visibility", "visible");
        }
        
        $(this).parent().parent().children("a").addClass("menuOn");
        
        $(this).children(".ptr").addClass("menuOn");
        $(this).parent().parent().children(".ptr").addClass("menuOn");
//      $("#alertText").text( $(this).parent().parent().children("a").attr("class") );
    },
    /*Mouse out*/
    function()
    {
        /*check that the LI has a sub menu*/
        if ($(this).children("ul").attr("tagName") == "UL")
        {
            var toClose = $(this).children("ul");
            /*set a delay on the close of the submenu, to allow correction of accidental mouseout*/
            var timer = setTimeout(function()
            {
                toClose.data("tocloseid", 0);
                toClose.css("visibility", "hidden");

            }, topMenuDelay);
            /*add the timer data to the element so that the correct timer can be canceled later if needed*/
            toClose.data("tocloseid", timer);
        }
        
        $(this).parent().parent().children("a").removeClass("menuOn");
        $(this).children(".ptr").removeClass("menuOn");
        $(this).parent().parent().children(".ptr").removeClass("menuOn");

    });
    
    $("#topmenu").css("visibility", "visible");
    
    
    /* SUBNAV ON PAGE */    
    $(".subnav ul").data("tocloseid", 0);
    $(".subnav li").hover(function()
    {

        if ($(this).children("ul").data("tocloseid") == 0)
        {

            $(this).siblings().children("ul").css("visibility", "hidden");
            $(this).siblings().children("ul").data("tocloseid", 0);
        }

    }, function()
    {

    });

    $(".subnav li").hover(
    /*On hover function*/
    function()
    {
        /*check that the LI has a sub menu*/
        if ($(this).children("ul").attr("tagName") == "UL")
        {
            /*check if the submenu has a timer running to close it, and cancel the timer if it exists*/
            if ($(this).children("ul").data("tocloseid") > 0)
            {
                clearTimeout($(this).children("ul").data("tocloseid"));
            }
            /*set the sub menu's visibility on*/
            $(this).children("ul").css("visibility", "visible");
        }
    },
    /*Mouse out*/
    function()
    {
        /*check that the LI has a sub menu*/
        if ($(this).children("ul").attr("tagName") == "UL")
        {
            var toClose = $(this).children("ul");
            /*set a delay on the close of the submenu, to allow correction of accidental mouseout*/
            var timer = setTimeout(function()
            {
                toClose.data("tocloseid", 0);
                toClose.css("visibility", "hidden");

            }, topMenuDelay);
            /*add the timer data to the element so that the correct timer can be canceled later if needed*/
            toClose.data("tocloseid", timer);
        }

    });
    
  
});
/* top menu end*/