/**
 * @fileoverview
 * This file contains a rather messy DOM ready handler to initialise the final
 * version of the tooltip functionality.
 */

$(function() {
    // Enable tooltips for anchor elements with the tooltip class attached.
    $("a.tooltip").each(function() {
        // The ID of the tooltip content.
        var id = this.href.replace(/^.*#/, "#");
        var content = $(id).html();

        // A timeout ID for the timeout function registered when the mouse
        // leaves the tooltip.
        var hideTimeout;

        // The tooltip element itself.
        var tooltip;

        // Clears the hide timeout if one is registered.
        var clearTimeout = function() {
            if (hideTimeout) {
                window.clearTimeout(hideTimeout);
                hideTimeout = null;
            }
        };

        // Hides the tooltip after a predefined grace period controlled herein.
        hideTooltip = function(e) {
            if (!hideTimeout) {
                hideTimeout = window.setTimeout(function() {
                    if (tooltip) {
                        tooltip.fadeOut("fast", function() {
                            tooltip.remove();
                            tooltip = null;
                        });
                    }
                    hideTimeout = null;
                }, 500);
                // The 500 above is the number of milliseconds after the mouse
                // leaves the tooltip before the tooltip is hidden -- this can
                // be adjusted either way.
            }
        };

        // Shows the tooltip and registers all appropriate hover handlers.
        var showTooltip = function(e) {
            if (hideTimeout) {
                clearTimeout();
            }
            else {
                /* These variables control the positioning of the tooltip
                 * relative to the current mouse position. Adjust as required.
                 */
                var x = e.pageX;
                var y = e.pageY + 20;
                
                tooltip = $("<div class='tooltip_block'></div>");
                tooltip
                    .css("position", "absolute")
                    .css("top", y + "px")
                    .css("left", x + "px")
                    .addClass($(id).attr("class"))
                    .html(content);
                tooltip.hoverIntent(clearTimeout, hideTooltip);
                tooltip.hide();
                $("body").append(tooltip);

                // Shift tooltip if it's off the screen to the right.
                if (tooltip.outerWidth() + x > $(window).width()) {
                    x = $(window).width() - tooltip.outerWidth() - 20;
                    tooltip.css("left", x + "px");
                }
                
                tooltip.fadeIn("fast");
            }
        };

        // Set up the hover handlers on the tooltip link.
        $(this).hoverIntent(showTooltip, hideTooltip);
    });
});

// vim: set et ts=4 sw=4 cin nopaste:
