﻿$j(function() {

    var controlmenu = $j("#controlmenu:has(a[href])");
    if (controlmenu.length == 0) {
        return;
    }

    controlmenu.toggle = function() {
        if (this.is(":hidden")) {
            this.css("margin-top", -this.outerHeight());
            this.css("display", "block");
            this.animate({ marginTop: 0 }, "slow", "easeInOutSine");
        }
        else {
            this.animate({ marginTop: -this.outerHeight() }, { complete: function() { $j(this).hide(0) } });
        }
    }

    controlmenu.bind("click", controlmenu, function(e) {
        if (!e.data.data("redirecting")) {
            e.data.toggle();
        }
    });

    var handle = $j("#controlmenuhandle");
    handle.css("display", "block");
    handle.before("<div style='clear: both;' />");

    handle.bind("click", controlmenu, function(e) {
        e.data.toggle();
    });

    controlmenu.find("a[href]").bind("click", controlmenu, function(e) {
        e.data.data("redirecting", true);
    });

    controlmenu.find("li").each(function() {
        var item = $j(this);
        if (item.find("a[href]").length == 0) {
            item.hide();
        }
    });
});

