﻿function simple_tooltip(target_items, name) {
    $("." + name).remove();
    $(target_items).each(function (i) {
        id = name + i;        
        if ($(this).attr('title')) {
            $("body").append("<div class='" + name + "' id='" + id + "'><p>" + $(this).attr('title') + "</p></div>");
            var my_tooltip = $("#" + id);
            var show = true;

            originalTitle = $(this).attr('title');
            $(this).hover(function () {
                $(this).attr("title", "");
            },
            function () {
                $(this).attr("title", originalTitle);
            });
            $(this).mouseover(function () {
                show = true;
                setTimeout(function () {
                    if (show) {
                        my_tooltip.css({ opacity: 0.9, display: "none" }).fadeIn(400);
                    }
                }, 200);
            }).mousemove(function (kmouse) {
                var halfWidth = my_tooltip.innerWidth() / 2;
                var diffX = kmouse.pageX + halfWidth - document.documentElement.clientWidth;
                var offsetX = diffX > 0 ? diffX + 2 : 0;
                my_tooltip.css({ left: kmouse.pageX - offsetX + 20, top: kmouse.pageY + 15 });

            }).mouseout(function () {
                show = false;
                my_tooltip.fadeOut(400);
            });
        }
    });
}
