// ==============================
// = Poptip - a tooltip variant =
// ==============================

// The related css currently lives in eos.css
jQuery.fn.poptip = (function($) {
    
    var current_poptip = null;
    var poptip = $('<div class="poptip_global hidden"><span></span><div class="arrow bottom"></div></div>');
    $(document.body).mouseup(onBodyMouseUp);
        
    function onMouseEnter(e) {
        var title = $(this).data('title');
        
        var el = poptip;
        $('span', el).html(title);
        poptip.removeClass('hidden');
        
        var halfPoptipWidth = el.outerWidth() / 2;
        var halfButtonWidth = $(this).outerWidth() / 2;
        var buttonLeft = $(this).offset().left;
        

        var left = Math.round(buttonLeft - (halfPoptipWidth - halfButtonWidth));
        
        var rightSideX = left + el.outerWidth();
        var bodyWidth = $(document.body).outerWidth();
        if(rightSideX > bodyWidth) {
            // partly outside the screen, right side, move it a bit to left
            var leftAdjust = rightSideX - bodyWidth;
            left -= leftAdjust;
            $('.arrow.bottom', el).css('left', halfPoptipWidth + leftAdjust);
        }
        else if(left < 0) {
            // partly outside the screen, left side, move it to left:0
            
            $('.arrow.bottom', el).css('left', halfPoptipWidth - (left * -1));
            left = 0;
        }
        else {
            $('.arrow.bottom', el).css('left', halfPoptipWidth - 2);
        }
        
        var top = Math.round($(this).offset().top - (el.height() + 4));
        el.css({
            left: left + 'px',
            top:  top + 'px'
        });
        return false;
    }
    function onMouseLeave(e) {
        poptip.addClass('hidden');
    }
    function onBodyMouseUp(e) {
        poptip.addClass('hidden');
    }

    
    return function() {
        document.body.appendChild(poptip[0]);
        this.each(function() {
            // Create a poptip element 
            var tagName = this.tagName.toLowerCase(), text;
            if($(this).attr('title')) {
                text = $(this).attr('title');
            }
            else {
                text = $('*[title]', this).attr('title');
            }
            if(text) {
                $(this).mouseenter(onMouseEnter);
                $(this).mouseleave(onMouseLeave);
                $(this).data('title', text);
                $(this).attr('title', '').find('*[title]').attr('title', '');
            }
        });
    };
    
})(jQuery);

jQuery(function() {
    var $ = jQuery;
    $('.toolbar li, a.poptip').poptip();
    $('.hypbox .close, .frame-button').poptip();
});



// ==============================
// = Poptip - a tooltip variant =
// ==============================

// The related css currently lives in eos.css


jQuery.fn.poptip2 = (function($) {
    
    var current_poptip = null;
    var poptip = $('<div class="poptip_global hidden"><span></span><div class="arrow bottom"></div></div>');
        
    function onMouseOver(e) {
        var title;
        // var target = $(e.target);
        var target = $(e.target).closest('*[title]');

        if(!target[0]) {
            var target = $(e.target).closest('*[title2]');
        }

        if(target[0]) {
            var title = target.attr('title') ||target.attr('title2');
        }
        else {
            poptip.addClass('hidden');
        }
        if(target[0] && title && target[0].tagName.toLowerCase() != 'form') {
            // Put the title attribute in data
            target.attr('title', '');
            target.attr('title2', title);
            
        

            current_poptip = target[0];
            var el = poptip;
            $('span', el).html(title);
            poptip.removeClass('hidden');
        
            var left1 = (el.outerWidth() / 2) - (target.outerWidth() / 2);
            var left2 = Math.round(target.offset().left - left1);
            var top = Math.round(target.offset().top - (el.height() + 4));
            el.css({
                left: (left2-2) + 'px',
                top:  top + 'px'
            });
        }
    }
    


    
    return function() {
        document.body.appendChild(poptip[0]);
        this.each(function() {
            $(this).mouseover(onMouseOver);
        });
    };
    
})(jQuery);




