var atmid = null;
var atm = null;

function HideMenu(dNum) {
    if (atm) {
        if (atm.attr("drop") != dNum) {
            atm.remove();
            atm = null;
            $("#top-menu td.hover").removeClass("hover");    
        }
        //if ($(dm).get(0) !== $(atm).get(0)) $(atm).hide().parent("td").removeClass("hover");
    }
    CancelHide();
}
function StartHide() {
    atmid = window.setTimeout(HideMenu, 500);
}
function CancelHide() {
    if (atmid) window.clearTimeout(atmid);
}
$(document).ready(function() {
    $("#top-menu td>a").each(function(idx) { $(this).attr("drop", idx)});
    $("#top-menu td>a").hover(
        function() {
            /*
            var dm = $(this).next("ul");            
            HideMenu(dm);
            if (!dm.length) return;
            atm = $(dm);
            atm.css("z-index", "50000");
            atm.fadeIn("fast");
            $(this).parent("td").addClass("hover");
            */
            var dNum = $(this).attr("drop");
            HideMenu(dNum);
            if (atm) return;
            var td = $(this).parent("td");
            var dm = $(this).next("ul").clone().attr("drop", dNum);
            if (!dm) return;
            var offsetTop = td.offset().top + td.outerHeight();
            var offsetLeft = td.offset().left;
            dm = dm.clone();
            $("#container").append(dm);
            dm.css({
               top: offsetTop,
               left: offsetLeft 
            }).hover(
                function() {
                    CancelHide();
                },
                function() {
                    StartHide();
                }
            );
            td.addClass("hover");
            atm = $(dm);
            atm.fadeIn("fast");
        },
        function() {
            StartHide();
        }
    );
    $("#top-menu ul").css("opacity", 0.97).hover(
        function() {
            CancelHide();
        },
        function() {
            StartHide();
        }
    );
    if ($.browser.msie) $("#top-menu ul").css("left", 0).find("li:first-child").addClass("first");
    $("ul#menu li li").hover(function() { $(this).addClass("hover"); }, function() { $(this).removeClass("hover"); });
});