$(function() {
    /**
     * Function to hide the weight display.
     */
    var hideWeightDisplay = function() {
        $(".weightTooltip").hide();
        $("#weightDiv").hide();
    };
    
    /**
     * Function to position the tooltip pointer appropriately using jQuery UI's
     * position utility method.
     */
    var positionTooltipPointer = function() {
        $(".weightTooltip")
            .show()
            .position({
                my: "center top",
                at: "center bottom",
                of: "#weightButton"                    
            });        
    };
    
    /**
     * Function to position the weight div appopriately using jQuery UI's
     * position utility method.
     */
    var positionWeightDiv = function() {
        $("#weightDiv")
            .show()
            .position({
                my: "center top",
                at: "center bottom",
                of: ".weightTooltip",
                collision: "none",
                offset: "0 -3px"
            });        
    };
    
    /**
     * Toggle the weight div display when the button is pressed.
     */
    $("#weightButton").bind("click", function() {
        if ($("#weightDiv").first().css("display") == "none") {
            positionTooltipPointer();
            positionWeightDiv();
        } else {
            hideWeightDisplay();
        }
    });
   
    /**
     * When the close button is pressed hide the display.
     */
    $("#weightDiv input").bind("click", function() {
        hideWeightDisplay();
    });
});
