$(function() {
    $('div.dialog').hide();
    wireHelpDialogs();
});

function wireHelpDialogs()
{
    // wire dialog divs
    $('div.dialog').each(function(i) {
        var dialog = $(this);

        var style = 'right';
        if (dialog.hasClass('left')) style = 'left';
        else if (dialog.hasClass('center')) style = 'center';
        dialog.data('style', style);
    });

    // wire dialog buttons
    $('a.dialog').each(function(i) {
        var a = $(this);
        var selector = a.attr('href');
        if (selector == '' || selector == "#") return;
        var dialog = $(selector);

        a.unbind('click');
        a.click(function(e) { e.preventDefault(); });

        a.unbind('hover');
        a.hover(
            // over
            function() {
                var anchor = $(this);
                var dialog = $(anchor.attr('href'));
                dialog.data('opener', anchor);
                dialog.show();
                positionHelpDialog(dialog);
            },
            // out
            function() {
                var anchor = $(this);
                var dialog = $(anchor.attr('href'));
                dialog.hide();
            }
        );
    });
}

function positionHelpDialog(dialog)
{
    var a = dialog.data('opener');

    // there has never been a hover event for this dialog - try the 1st visible button
    if (typeof (a) == "undefined")
    {
        a = $('a.dialog[href=#' + dialog.attr('id') + ']:visible');
        if (a.size() == 0) return;
    }

    if (dialog.data('style') == 'right')
    {
        // position dialog vertically
        dialog.css('top', a.position().top + a.height() + 15);

        // correct for dialog running outside of container (on the right)
        var left = a.position().left;
		
        var containerRight = $('div.content').position().left + $('div.content').width();
        if (left + dialog.width() > containerRight)
            left = containerRight - dialog.width();
            
        // position dialog horizontally
        dialog.css('left', left);
    }
    else if (dialog.data('style') == 'left')
    {
        // position dialog horizontally
        dialog.css('left', a.position().left + a.width());

        // calculate optimal top position (centered vertically)
        var top = a.position().top - (dialog.height() / 2);

        // correct for dialog going off the bottom of the viewport
        var belowBottom = Math.max((top - $(document).scrollTop() + dialog.height()) - ($(window).height() - 30), 0);
        if (belowBottom > 0) {
            top = Math.max(top - belowBottom, a.position().top + a.height() - dialog.height() + 10);
        }

        // position dialog vertically
        dialog.css('top', top);
    }
    else if (dialog.data('style') == 'center')
    {
        var top = (($(window).height() / 2) - (dialog.height() / 2)) - 100;
        var left = ($(window).width() / 2) - (dialog.width() / 2);
        dialog.css('top', top);
        dialog.css('left', left);
        dialog.css('position', 'fixed');
    }
}

function positionAllHelpDialogs() {
    $('a.dialog').each(function() {
        var a = $(this);
        positionHelpDialog($(a.attr('href')));
    });
}