﻿
;(function($){
$.tooltip = {
defaults: {
'class': 'tooltip',
css: {},
event: 'mouseover',
smart: false,
track: true,
offset: {x: 12, y: 18}
},
setup: function(opt){ $.extend($.tooltip.defaults, opt) }
}
var tip = function(src, opt) {
var self = this;
var html = '';
src = $(src);
if($.metadata) { opt = $.extend({}, opt, src.metadata().tooltip) }
if(opt.smart) {
if(!opt.title){ opt.title = src.attr('title') }
if(!opt.text){ opt.text = src.attr('alt') }
if(!opt.href){ opt.href = src.attr('href') }
}
var html = '';
if(opt.title) { html += '<div class="title">'+opt.title+'</div>' }
if(opt.text) { html += '<div class="text">'+opt.text+'</div>' }
if(opt.href) { html += '<div class="url">'+opt.href+'</div>' }
if(html == ''){ delete this; return }
src
.removeAttr('title')
.removeAttr('alt')
$.data(src, 'tooltip', self)
self.dst = opt.dst;
src
.bind(opt.event+'.tooltip', over)
.bind('mouseout.tooltip', out)
.bind('focus.tooltip', over)
.bind('blur.tooltip', out)
.bind('click.tooltip', hide)
function destroy_timers() {
if(self.timein){
clearTimeout(self.timein);
delete self.timein
}
if(self.timeout){
clearTimeout(self.timeout);
delete self.timeout
}
}
function over(ev){
destroy_timers();
if(self.st == out){ return }
self.st = over;
if(!self.dst) {
self.dst = $('<div>')
.appendTo(document.body)
.css({visibility:'hidden'})
}
self.ev = ev;
self.timein = setTimeout(show, opt['in'] || 0)
}
function show(){
destroy_timers();
if(!self.dst) { return }
self.dst
.addClass(opt['class'])
.css(opt.css)
.html(html);
if(opt.duration > 0) {
self.timein = setTimeout(hide, opt.duration)
}
if(opt.track) {
self.dst
.css({
position:'absolute',
visibility:'visible'
})
src
.bind('mousemove.tooltip', move);
$('body')
.bind('click.tooltip', hide)
}
self.w = self.dst.width();
self.h = self.dst.height();
if(opt.onshow){ opt.onshow.apply(self.dst) }
if(opt.track){ move() }
}
function out(){
destroy_timers();
if(self.st != over){ return }
self.timeout = setTimeout(hide, opt['out'] || 0)
}
function hide() {
destroy_timers();
$('body').unbind('.tooltip');
$('src').unbind('mousemove.tooltip');
if(!self.dst) { return }
self.dst.empty();
if(opt.onhide){ opt.onhide.apply(self.dst) }
if(self.dst != opt.dst) {
self.dst.remove();
delete self.dst
}
}
function move(ev){
if(!ev){ ev = self.ev } else { self.ev = ev}
if(!self.dst) { return }
var p = {
x: ev.pageX + opt.offset.x, y: ev.pageY + opt.offset.y,
w: self.w, h: self.h
}
w = window;
var v = {
x: w.scrollX, y: w.scrollY,
w: w.innerWidth - 20,
h: w.innerHeight - 20
};
if(p.x + p.w > v.x + v.w) { p.x = v.x + v.w - p.w }
if(p.y + p.h > v.y + v.h) { p.y = ev.pageY - p.h - opt.offset.y }
if(p.x < v.x){ p.x = v.x }
if(p.y < v.y){ p.y = v.y }
self.dst.css({top:p.y, left:p.x})
}
}
function	setup(){
return this.each(function(){
t = $.data(this, 'tooltip');
if(t){$.extend(t.opt, opt)}
return this
})
}
function	remove(){
return
this
.unbind('.tooltip')
.removeData('tooltip')
}
function	create(opt){
remove.apply(this);
opt = $.extend({}, $.tooltip.defaults, opt);
return this.each(function(){
new tip(this, opt);
});
}
$.fn.tooltip = function(a, o) {
if(!o && (typeof a != 'string')) {
o = a;
a = 'create'
}
return (f = ({
setup: setup,
remove: remove,
create: create
})[a]) && f.apply(this, [o])
}
})(jQuery);
