1/**
2 * jQuery Tooltip Plugin
3 *@requires jQuery v1.2.6
4 * http://www.socialembedded.com/labs
5 *
6 * Copyright (c) Hernan Amiune (hernan.amiune.com)
7 * Dual licensed under the MIT and GPL licenses:
8 * http://www.opensource.org/licenses/mit-license.php
9 * http://www.gnu.org/licenses/gpl.html
10 *
11 * Version: 1.0
12 */
13
14(function($){ $.fn.invoiceTooltip = function(options){
15
16 var defaults = {
17 cssClass: "", //CSS class or classes to style the tooltip
18 delay : 0, //The number of milliseconds before displaying the tooltip
19 duration : 500, //The number of milliseconds after moving the mouse cusor before removing the tooltip.
20 xOffset : 15, //X offset will allow the tooltip to appear offset by x pixels.
21 yOffset : 15, //Y offset will allow the tooltip to appear offset by y pixels.
22 opacity : 0, //0 is completely opaque and 100 completely transparent
23 fadeDuration: 400 //[toxi20090112] added fade duration in millis (default = "normal")
24 };
25
26 var options = $.extend(defaults, options);
27
28
29 return this.each(function(index) {
30
31 var $this = $(this);
32
33 //use just one div for all tooltips
34 // [toxi20090112] allow the tooltip div to be already present (would break currently)
35 $tooltip=$("#divTooltip");
36 if($tooltip.length == 0){
37 $tooltip = $('<div id="divTooltip"></div>');
38 $('body').append($tooltip);
39 $tooltip.hide();
40 }
41
42 //displays the tooltip
43 $this.click( function(e){
44 //compatibility issue
45 e = e ? e : window.event;
46
47 //don't hide the tooltip if the mouse is over the element again
48 clearTimeout($tooltip.data("hideTimeoutId"));
49
50 //set the tooltip class
51 $tooltip.removeClass($tooltip.attr("class"));
52 $tooltip.css("width","");
53 $tooltip.css("height","");
54 $tooltip.addClass(options.cssClass);
55 $tooltip.css("opacity",1-options.opacity/100);
56 $tooltip.css("position","absolute");
57
58 //save the title text and remove it from title to avoid showing the default tooltip
59 $tooltip.data("title",$this.attr("title"));
60 $this.attr("title","");
61 $tooltip.data("alt",$this.attr("alt"));
62 $this.attr("alt","");
63
64 //set the tooltip content
65 $tooltip.html($tooltip.data("title"));
66 // [toxi20090112] only use ajax if there actually is an href attrib present
67 var href=$this.attr("href");
68 // [Peter] href!="" added
69 if(href!=undefined && href!="" && href != "#")
70 $tooltip.html($.ajax({url:$this.attr("href"),async:false}).responseText);
71
72 //set the tooltip position
73 winw = $(window).width();
74 w = $tooltip.width();
75 xOffset = options.xOffset;
76
77 //right priority
78 if(w+xOffset+50 < winw-e.clientX)
79 $tooltip.css("left", $(document).scrollLeft() + e.clientX+xOffset);
80 else if(w+xOffset+50 < e.clientX)
81 $tooltip.css("left", $(document).scrollLeft() + e.clientX-(w+xOffset));
82 else{
83 //there is more space at left, fit the tooltip there
84 if(e.clientX > winw/2){
85 $tooltip.width(e.clientX-50);
86 $tooltip.css("left", $(document).scrollLeft() + 25);
87 }
88 //there is more space at right, fit the tooltip there
89 else{
90 $tooltip.width((winw-e.clientX)-50);
91 $tooltip.css("left", $(document).scrollLeft() + e.clientX+xOffset);
92 }
93 }
94
95 winh = $(window).height();
96 h = $tooltip.height();
97 yOffset = options.yOffset;
98 //top position priority
99 if(h+yOffset + 50 < e.clientY)
100 $tooltip.css("top", $(document).scrollTop() + e.clientY-(h+yOffset));
101 else if(h+yOffset + 50 < winh-e.clientY)
102 $tooltip.css("top", $(document).scrollTop() + e.clientY+yOffset);
103 else
104 $tooltip.css("top", $(document).scrollTop() + 10);
105
106 //start the timer to show the tooltip
107 //[toxi20090112] modified to make use of fadeDuration option
108 $tooltip.data("showTimeoutId", setTimeout("$tooltip.fadeIn("+options.fadeDuration+")",options.delay));
109 });
110
111 $this.mouseout(function(e){
112 //restore the title
113 $this.attr("title",$tooltip.data("title"));
114 $this.attr("alt",$tooltip.data("alt"));
115 //don't show the tooltip if the mouse left the element before the delay time
116 clearTimeout($tooltip.data("showTimeoutId"));
117 //start the timer to hide the tooltip
118 //[toxi20090112] modified to make use of fadeDuration option
119 $tooltip.data("hideTimeoutId", setTimeout("$tooltip.fadeOut("+options.fadeDuration+")",options.duration));
120 });
121
122 $this.click(function(e){
123 e.preventDefault();
124 });
125
126 });
127
128}})(jQuery);