$(document).ready(function() {

    margin_show = $("ul.top-nav li.first-nav ul.second-nav").css("margin-top");
    margin_show = px2em(parseInt(margin_show));

    margin_hide = margin_show - 2.5;

    if (isIE7()) {
        margin_show += 1.5;
        margin_hide += 1.5;
    }

	if (isIE8()) {
        margin_hide += 1;
    }

    margin_show_em = margin_show + "em";
    margin_hide_em = margin_hide + "em";

    $("ul.top-nav li.first-nav ul.second-nav").css("margin-top", margin_hide_em);

    showNav($("ul.top-nav li.first-nav").find('.second-nav.nav-on'));

    function px2em(px) {
        return parseInt(px) / parseInt($("body").css("font-size"));
    }

    function showNav(m) {
		if (isIE7() || isIE8()) {
			$(m).css("margin-left", "0");
		}
	
        if ($(m).css('display') == "none") {
            if (!isIE7() && !isIE8()) {
                $(m).css('opacity', 0);
            }
            $(m).css('display', "block");
        }

        $(m).stop(true);

        $(m).animate({"margin-top": margin_show_em}, {queue:false, duration:400, easing:'easeInQuad'});

        if (!isIE7() && !isIE8()) {
            $(m).animate({"opacity": 1}, {queue:false, duration:500, easing:'easeInQuad'});
        }
    }

    function hideNav(m) {
        $(m).stop(true);

        $(m).animate({"margin-top": margin_hide_em}, {queue:false, duration:500, easing:'easeInQuad', complete: function() {
            $(this).css("display", "none")
        } });

        if (!isIE7() && !isIE8()) {
            $(m).animate(
                { "opacity": 0 },
                { queue:false, duration:400, easing:'easeInQuad' }
            );
        }

    }

    $("ul.top-nav li.first-nav").mouseenter(function() {
        showNav($(this).find('.second-nav.nav-off, .second-nav.nav-on'));
        hideNav($(this).siblings().find('.second-nav.nav-on'));
    });

    $("ul.top-nav li.first-nav").mouseleave(function() {
        hideNav($(this).find('.second-nav.nav-off'));
        showNav($(this).siblings().find('.second-nav.nav-on'));
    });


});
