function tooltip_get_module($element) {
    return window.location.pathname;
}

function tooltip_get_section($element) {
    return $element.parents().prev("h1,h2,h3,h4,h5,h6").text();
}

function tooltip_get_name($element) {
    return $element.text();
}

jQuery(function($) {
    if ( $('.hastooltip').size() ) {
        $.getJSON(
            '/siteadmin/tooltip_helper.phtml',
            { 'mode': 'is_enabled' },
            function(response) {
                if (response.is_enabled) {
                    var element_with_tooltip;

                    $('<div id="tooltip">&nbsp;</div>').appendTo('body');

                    $('.hastooltip').mouseenter(function(){
                        if ($(this).data('tooltip_created')) {
                            return;
                        }

                        if ( ! $(this).attr('title') ) {
                            var $element = $(this);
                            $.getJSON(
                                '/siteadmin/tooltip_helper.phtml',
                                {
                                    'mode':     'get',
                                    'module':   tooltip_get_module($element),
                                    'section':  tooltip_get_section($element),
                                    'name':     tooltip_get_name($element)
                                },
                                function(response) {
                                    $element.data('tooltip_created', true);
                                    $element.attr('title', response.tooltip);
                                    $element.tooltip({
                                        'position':     'top center',
                                        'tip':          '#tooltip',
                                        'delay':        1,
                                        'onShow':      function(ev) {
                                            element_with_tooltip = ev.originalTarget || ev.srcElement;
                                        },
                                        'onHide':      function(ev) {
                                            /*
                                             * in theory, we can get following calls chain:
                                             *      onShow(1);
                                             *      onShow(2);
                                             *      onHide(1);
                                             *
                                             * after this, in "onClick" below, "element_with_tooltip" will
                                             * be null, and so, tooltip will be shown, but will ignore
                                             * clicks, and so we need to clear "element_with_tooltip" only
                                             * if in this event it's same, as it was in onShow() call
                                             *
                                             */
                                            if ( element_with_tooltip == ev.originalTarget || element_with_tooltip == ev.srcElement) {
                                                element_with_tooltip = null;
                                            }
                                        }
                                    }).dynamic({
                                        'api': true,
                                        'top': {
                                            'direction':    'down',
                                            'bounce':       true
                                        }
                                    }).show();
                                }
                            );
                        }
                    });

                    $('#tooltip').bind('click', function() {
                        if (element_with_tooltip) {
                            var $element = $(element_with_tooltip);
                            window.location = '/siteadmin/tooltip_helper.phtml?' +
                                $.param({
                                    'module':   tooltip_get_module($element),
                                    'section':  tooltip_get_section($element),
                                    'name':     tooltip_get_name($element)
                                });
                        }
                    });
                }
            }
        );
    }
});


