(function($){
    $.fn.tiptop=function(params){
        params=$.extend({
            decal_x:10,
            decal_y:10,
            content:"",
            relative_to:"",
            position_x:0,
            id:"tiptop"
        },params);
        if ($('#tiptop').length==0) {
            $(document.body).prepend("<div id='"+params.id+"'></div>");
            $('#tiptop').css({
                position:"absolute",
                display:"none",
                zIndex:"1100"
            });
        }
        this.each(function() {
            if (params.content=="") {
                $(this).data("title",$(this).attr("title"));
                $(this).attr("title","");
            } else
                $(this).data("title",params.content);
            $(this).hover(
                    function() {
                        $('#tiptop').html($(this).data("title")).show();
                    },
                    function(e) {
                        $('#tiptop').hide();
                    }
                ).mousemove(function(e) {
                    if (params.relative_to != "") {
                        pos_x=params.position_x+$(('#'+params.relative_to)).offset().left;
                    } else {
                        pos_x=e.pageX+params.decal_x;
                        if (e.clientX+params.decal_x+$('#tiptop').outerWidth()>$(window).width())
                            pos_x=e.pageX-params.decal_x-$('#tiptop').outerWidth();                        
                    }
                    pos_y=e.pageY+params.decal_y;
                    if (e.clientY+params.decal_y+$('#tiptop').outerHeight()>$(window).height())
                        pos_y=e.pageY-params.decal_y-$('#tiptop').outerHeight();
                    $("#tiptop").css({
                        top:pos_y+"px",
                        left:pos_x+"px"
                    });
                });
        });
        return this;
    };
})(jQuery);
