var menu_top        = 0;
var menu_bottom     = 0;
var menu_left       = 0;  
var menu_right      = 0;
var submenu_top     = 0;
var submenu_bottom  = 0;
var submenu_left    = 0;
var submenu_right   = 0;
var menuactive;

$(document).ready(function(){				 
	$("li.menuactive").ready(function(){
	    menuactive = $("li.menuactive");
        var get_id = $("li.menuactive").attr("id");
        get_id = "#"+get_id+"_";
        //alert(get_id);
        $(".m_sub").css("display", "none");
        $(get_id).css("display", "block");
    });

    $("#primary-nav li").mouseover(function(){
        var myelem  = $(this);
        // get parent x y
        var pos     = myelem.offset();
        var width   = myelem.width();
        var height  = myelem.height();
        menu_top    = pos.top;
        menu_left   = pos.left;
        menu_right  = menu_left + width;
        var get_id = $(this).attr("id");
        var parent_id = "#"+get_id;
        get_id = "#"+get_id+"_";
        $("li.menuactive").addClass("menuparent");
        $("li.menuactive").removeClass("menuactive");
        $(this).addClass("menuactive");
        $(this).removeClass("menuparent");
        $(".m_sub").css("display", "none");
        $(get_id).css("display", "block");
        // get child x y
        var submyelem  = $(get_id);
        pos     = submyelem.offset();
        width   = submyelem.width();
        height  = submyelem.height();
        submenu_top     = pos.top;
        submenu_left    = pos.left;
        menu_bottom     = submenu_top + height;
        submenu_right   = submenu_left + width;
        submenu_bottom  = submenu_top + height;
    });
    
    $().mousemove(function(e){
        if ( (menu_top != 0) &&
             (menu_bottom != 0) &&
             (menu_left != 0) &&
             (menu_right != 0) &&
             (submenu_top != 0) &&
             (submenu_bottom != 0) &&
             (submenu_left != 0) &&
             (submenu_right != 0))
        {
            var x = e.pageX;
            var y = e.pageY;
            if (!((menu_top < y) && (menu_bottom > y) && (menu_right > x) || (menu_right < x))
                &&
                !((submenu_top < y) && (submenu_bottom > y) && (submenu_right > x) || (submenu_right< x)))
            {
                /*alert("top:"+menu_top+"\nleft:"+menu_left+"\nright:"+menu_right+"\nbottom"+menu_bottom+"\ntop:"+submenu_top+"\nleft:"+submenu_left+"\nright:"+submenu_right+"\nbottom"+submenu_bottom+"\nx"+x+"\ny"+y);*/
                menu_top        = 0;
                menu_bottom     = 0;
                menu_left       = 0;  
                menu_right      = 0;
                submenu_top     = 0;
                submenu_bottom  = 0;
                submenu_left    = 0;
                submenu_right   = 0;
                $("li.menuactive").addClass("menuparent");
                $("li.menuactive").removeClass("menuactive");
                menuactive.addClass("menuactive");
                menuactive.removeClass("menuparent");
                var get_id = menuactive.attr("id");
                get_id = "#"+get_id+"_";
                $(".m_sub").css("display", "none");
                $(get_id).css("display", "block");
            }
        }
    }); 


});
