// ********************************************************************
// Product      : Payage Test
// Date         : 28 September 2022
// Copyright    : Les Arbres Design 2022
// Contact      : https://www.lesarbresdesign.info
// Licence      : GNU General Public License
// *********************************************************************
//
jQuery(document).ready(function() 
{
    jQuery('.ptTooltip').hover(pt_hover_in, pt_hover_out);     // initialise tooltips
    pt_check_updates();
    window.setInterval(pt_check_updates, 6000);
});

//-------------------------------------------------------------------------------------
// Check for new messages
//
function pt_check_updates()
{
    var url = '?option=com_payage_test&task=ajax_get_updates&format=raw&tmpl=component';   
    jQuery.ajax({url: url, dataType: "text", type: "GET",
        success: function(responseText, status, xhr) {
            jQuery('.pt_messages').append(responseText);
            jQuery('.pt_messages').scrollTop(jQuery('.pt_messages')[0].scrollHeight);
            }
        });        
}

//-------------------------------------------------------------------------------------
// Show a tooltip
//
function pt_hover_in()
{
    if (jQuery(this).data('lad-title'))
        var content = jQuery(this).data('lad-title');
    else
        {
        var content = jQuery(this).attr('title');
        jQuery(this).data('lad-title', content);
        jQuery(this).removeAttr('title');
        }
    var parent_offset = jQuery(this).offset();
    var half_parent_width = jQuery(this).outerWidth() / 2;
    var window_width = jQuery(window).width();
    var html = '<div class="pt_tip_container"><div class="pt_tip_outer"><div class="pt_tip_inner">';
    html += content + '</div></div><span class="pt_tip_arrow"></span></div>';
    var new_element = jQuery(html);
    jQuery('body').append(new_element);
    var popup_width = new_element.outerWidth();
    var popup_height = new_element.outerHeight();
    var half_popup_width = popup_width / 2;
    var margin_left = half_popup_width - half_parent_width;                             // try to hit the centre of the icon with the tooltip
        var margin_top = popup_height + 10;                                             // add 10 to allow for the bottom arrow
    if ((parent_offset.left + half_popup_width + half_parent_width) > window_width)                     // if we would overflow the right side of the window ...
        margin_left += ((parent_offset.left + half_popup_width + half_parent_width + 1) - window_width);    // increase left margin by the amount of the overflow
    if (margin_left > parent_offset.left)                                                               // if we would overflow the left side of the window ...
        margin_left = parent_offset.left;
    new_element.css(parent_offset);
    new_element.css('margin-left', '-' + margin_left + 'px');
    new_element.css('margin-top', '-' + margin_top + 'px');
    new_element.find('.pt_tip_arrow').css('left','calc(50% - 5px)');
    new_element.fadeIn(100);     
}

//-------------------------------------------------------------------------------------
// Close a tooltip
//
function pt_hover_out()
{
    jQuery('.pt_tip_container').remove(); 
}

