// General-purpose lightweight info popover.
// Replaces Bootstrap popover for cases where only a small HTML tooltip with an
// optional link is needed. Automatically uses skin CSS variables so all themes work.
//
// API:
//   ShowInfoPopover(iconElement, sHtmlContent)  — opens a popover below iconElement
//   CloseInfoPopover()                          — closes any open popover

(function () {
    var style = document.createElement('style');
    style.id = 'st-popover-styles';
    style.textContent =
        '.st-popover {' +
        '    position: absolute;' +
        '    z-index: 9999;' +
        '    max-width: 320px;' +
        '    padding: 8px 12px;' +
        '    background: var(--st-bg-overlay);' +
        '    color: var(--st-text-body);' +
        '    border: 1px solid var(--st-grid-border);' +
        '    border-radius: var(--st-border-radius);' +
        '    box-shadow: 0 2px 8px rgba(0,0,0,.25);' +
        '    font-size: 0.875em;' +
        '    line-height: 1.4;' +
        '}' +
        '.st-popover a { color: var(--st-accent); }' +
        '.st-popover a:hover { color: var(--st-accent-hover); }';
    document.head.appendChild(style);
})();







// ShowInfoPopover(iconElement, sHtmlContent, sLearnUrl)
//
// Opens a lightweight popover positioned just below iconElement and dismisses
// it automatically on the next click anywhere on the page.
//
// Parameters:
//   iconElement  — DOM element (e.g. a small info icon span) that anchors the popover.
//                  The popover appears 4px below the element's bottom edge, aligned
//                  to its left edge and clamped to stay within the viewport.
//   sHtmlContent — HTML string rendered inside the popover. May include plain text,
//                  formatted markup, or an <a> link. Links are styled automatically
//                  with --st-accent so they are readable on all skins.
//   sLearnUrl    — Optional. When non-empty, a "Learn more" link is appended to the
//                  popover pointing to this URL (opens in a new tab). Pass
//                  sBrandUrls.learn (from Varinfo.BrandUrls) so the URL stays
//                  brand-aware across products.
//
// Behavior:
//   - Any previously open popover is closed before the new one opens.
//   - A document-level click handler is registered (deferred one tick via setTimeout
//     to prevent the triggering click from immediately closing the popover).
//   - Clicking anywhere, including outside the popover, dismisses it.
//   - Call CloseInfoPopover() to dismiss programmatically.
//
// Example:
//   ShowInfoPopover(this, 'Rate limit reached.', sBrandUrls.learn);
function ShowInfoPopover(iconElement, sHtmlContent, sLearnUrl) {
    CloseInfoPopover();

    var sContent = sHtmlContent;
    if (sLearnUrl)
        sContent += '<br><a href="' + sLearnUrl + '" target="_blank">Learn more &rarr;</a>';

    var $pop = $('<div class="st-popover"></div>').html(sContent);
    $('body').append($pop);

    var rect  = iconElement.getBoundingClientRect();
    var nTop  = rect.bottom + window.scrollY + 4;
    var nLeft = rect.left   + window.scrollX;

    // Clamp so the popover doesn't overflow the right edge of the viewport
    nLeft = Math.min(nLeft, window.scrollX + window.innerWidth - 340);

    $pop.css({ top: nTop, left: nLeft });

    // Defer adding the document click handler so the click that opened the
    // popover doesn't immediately close it via bubbling
    setTimeout(function () {
        $(document).one('click', CloseInfoPopover);
    }, 0);
}






function CloseInfoPopover() {
    $('.st-popover').remove();
    $(document).off('click', CloseInfoPopover);
}
