﻿/* jQuery Tooltip Script (c)2010 John Davenport Scheuer
   participant in http://www.dynamicdrive.com/forums/
   username: jscheuer1 - This Notice Must Remain for Legal Use
   all rights reserved.
   */
jQuery.fn.toolTip = function(fig){
	fig = fig || {};
	var topOff = fig.topOff || 0, leftOff = fig.leftOff || 0, fadeInTime = (fig.fadeInTime == 0 || fig.fadeInTime? fig.fadeInTime : 1000),
	disappear = fig.disappear, cleanimages = false, loading, delay = (fig.delay == 0 || fig.delay? fig.delay : 500),
	triggers = this.selector || '.tooltip', tt, fixed = fig.fixed, nomove = fig.nomove, useTriggerHeight = fig.useTriggerHeight;
	if(fig.loading){
		loading = new Image();
		loading.onload = function(){
			loading = loading.src;
		}
		loading.src = fig.loading;
	}
	jQuery(function($){
		if($.browser.msie){
			var oldimages = $('<div style="visibility: hidden; position: absolute; top: -999999px; left: -999999px;"></div>');
			$('body').append(oldimages);
			cleanimages = function (){
				var im = tt.get(0).getElementsByTagName('img')[0];
				if(im){
					oldimages.append(im.parentNode.removeChild(im));
					$(im).css({position: 'absolute', top: 0, left: 0});
				}
			};
		}
		tt = triggers.substr(1);
		triggers = $(triggers);
		if(triggers.size() === 0){
			return;
		}
		if($('#tooltip').size() === 0){
			$('body').append(tt = $('<div id="' + tt + '"></div>').data('re', [/\boffset-?\d+_-?\d+/, /\boffset/]));
		} else {
			tt = $('#tooltip');
		}
		function ttimer(to){
			return to || to === 0? tt.data('timer', to) : tt.data('timer');
		}
		triggers.each(function(){
			var t = $(this), m;
			if(t.attr('rel')){
				t.data('title', '<img src="' + (loading || t.attr('rel')) + '" alt="LOADING">' + (this.title? '<br>' + this.title : '')).attr('title', '');
			} else {
				t.data('title', this.title).attr('title', '');
			}
			if((m = tt.data('re')[0].exec(this.className))){
				m = m[0].replace(tt.data('re')[1], '').split('_');
				t.data('topOff', m[0]).data('leftOff', m[1]);
			}
		}).hover(function(e){
			var t = $(this), o = fixed? t.offset() : {top: e.pageY, left: e.pageX}, top = o.top + (useTriggerHeight? t.height() : 0), left = o.left,
			w = $(window), bot = w.height() + w.scrollTop(), right = w.width() + w.scrollLeft();
			if(t.data('topOff')){
				top += +t.data('topOff');
				left += +t.data('leftOff');
			} else {
				top += topOff;
				left += leftOff;
			}
			clearTimeout(ttimer());
			if(tt.data('t') !== this){
				tt.css({display: 'none'});
			}
			if(fixed && left + tt.outerWidth() > right){
				left = left - tt.outerWidth() + t.outerWidth();
			}
			if(fadeInTime){
				tt.html(t.data('title')).css({top: top, left: left}).fadeIn(fadeInTime, function(){
					if(this.style.removeAttribute){
						this.style.removeAttribute('filter');
					}
				});
			} else {
				tt.html(t.data('title')).css({top: top, left: left}).show();
			}
			if(t.attr('rel') && loading){
				var im = new Image();
				im.onload = function(){
					tt.find('img').attr('src', im.src);
				}
				im.src = t.attr('rel');
			}
			if(top + tt.outerHeight() > bot){
				tt.css({top: o.top - (t.data('topOff') || topOff) - tt.outerHeight()});
			}
			if(fixed && left + tt.outerWidth() > right){
				tt.css({left: left - tt.outerWidth() + t.outerWidth()});
			} else {
				while(left + tt.outerWidth() > right){
					tt.css({left: --left});
				}
			}
			if(fixed && !disappear){
				tt.unbind('mouseenter mouseleave');
				tt.hover(function(){
					clearTimeout(ttimer());
				}, function(){
					clearTimeout(ttimer());
					ttimer(setTimeout(function(){tt.css({display: 'none'});}, delay));
				});
			} else {
				tt.unbind('mouseenter mouseleave');
			}
		},
		function(){
			clearTimeout(ttimer());
			tt.data('t', this);
			if(delay){
				ttimer(setTimeout(function(){
					tt.css({display: 'none'});
					if(cleanimages){
						cleanimages();
					}
				}, delay));
			} else {
				tt.css({display: 'none'});
				if(cleanimages){
					cleanimages();
				}
			}
		});
		if(!fixed && !nomove){
			triggers.mousemove(function(e){
				var t = $(this), o = {top: e.pageY, left: e.pageX}, top = o.top + t.height(), left = o.left,
				w = $(window), bot = w.height() + w.scrollTop(), right = w.width() + w.scrollLeft();
				if(t.data('topOff')){
					top += +t.data('topOff');
					left += +t.data('leftOff');
				} else {
					top += topOff;
					left += leftOff;
				}
				tt.css({top: top, left: left});
				if(top + tt.outerHeight() > bot){
					tt.css({top: o.top - tt.outerHeight()});
				}
				while(left + tt.outerWidth() > right){
					tt.css({left: --left});
				}
			});
		}
	});
	return triggers;
};
jQuery().toolTip({useTriggerHeight: true, fadeInTime: 0, fixed: true, disappear: true, delay: 0});
