2 * jQuery Beauty Tips plugin
3 * Version 0.7 (10/20/2008)
4 * @requires jQuery v1.2+ (not fully tested on versions prior to 1.2.6)
6 * Dual licensed under the MIT and GPL licenses:
7 * http://www.opensource.org/licenses/mit-license.php
8 * http://www.gnu.org/licenses/gpl.html
10 * No guarantees, warranties, or promises of any kind
19 * @author Jeff Robbins - Lullabot - http://www.lullabot.com
21 * @credit Inspired by Karl Swedberg's ClueTip
22 * (http://plugins.learningjquery.com/cluetip/), which in turn was inspired
23 * by Cody Lindley's jTip (http://www.codylindley.com)
26 * Beauty Tips is a jQuery tooltips plugin which uses the canvas drawing element
27 * in the HTML5 spec in order to dynamically draw tooltip "talk bubbles" around
28 * the descriptive help text associated with an item. This is in many ways
29 * similar to Google Maps which both provides similar talk-bubbles and uses the
30 * canvas element to draw them.
32 * The canvas element is supported in modern versions of FireFox, Safari, and
33 * Opera. However, Internet Explorer needs a separate library called ExplorerCanvas
34 * included on the page in order to support canvas drawing functions. ExplorerCanvas
35 * was created by Google for use with their web apps and you can find it here:
36 * http://excanvas.sourceforge.net/
38 * Beauty Tips was written to be simple to use and pretty. All of its options
39 * are documented at the bottom of this file and defaults can be overwritten
40 * globally for the entire page, or individually on each call.
42 * By default each tooltip will be positioned on the side of the target element
43 * which has the most free space. This is affected by the scroll position and
44 * size of the current window, so each Beauty Tip is redrawn each time it is
45 * displayed. It may appear above an element at the bottom of the page, but when
46 * the page is scrolled down (and the element is at the top of the page) it will
47 * then appear below it. Additionally, positions can be forced or a preferred
48 * order can be defined. See examples below.
51 * The function can be called in a number of ways.
53 * $(selector).bt('Content text');
54 * $(selector).bt('Content text', {option1: value, option2: value});
55 * $(selector).bt({option1: value, option2: value});
61 * This is probably the simplest example. It will go through the page finding
62 * every element which has a title attribute and give it a Beauty Tips popup
63 * which gets fired on hover.
66 * $('h2').bt('I am an H2 element!', {trigger: 'click', positions: 'top'});
67 * When any H2 element on the page is clicked on, a tip will appear above it.
71 * titleSelector: "attr('href')",
73 * cssStyles: {color: 'white', fontWeight: 'bold', width: 'auto'},
80 * positions: ['left', 'right', 'bottom'],
82 * This will find all <a> tags and display a red baloon with bold white text
83 * containing the href link. The box will be a variable width up to 400px with
84 * rounded corners and will fade in and animate position toward the target
85 * object when appearing. The script will try to position the box to the left,
86 * then to the right, and finally it will place it on the bottom if it does not
90 * $('#my-table td[title]').bt({
91 * preShow: function() {
92 * $(this).data('origBG', $(this).css('background-color'));
93 * $(this).css('background-color', 'yellow');
95 * postHide: function() {
96 * $(this).css('background-color', $(this).data('origBG'));
99 * Find every table cell within #mytable with a title attribute. Hilight the cell
100 * yellow before displaying the BeautyTip. Restore it to its original background
101 * when hiding/removing the BeautyTip.
104 * $().bt.defaults.fill = 'rgba(102, 102, 255. .8)';
106 * All bubbles will be filled with a semi-transparent light-blue background
107 * unless otherwise specified.
110 jQuery.fn.bt = function(content, options) {
112 if (typeof content != 'string') {
113 var contentSelect = true;
118 var contentSelect = false;
121 var tooltips_pool = [];
124 return this.each(function(index) {
126 tooltips_pool.push(this);
128 var opts = jQuery.extend(false, jQuery.fn.bt.defaults, options);
130 // clean up the options
131 opts.spikeLength = numb(opts.spikeLength);
132 opts.spikeGirth = numb(opts.spikeGirth);
133 opts.overlap = numb(opts.overlap);
135 var turnOn = function () {
137 for (var x in tooltips_pool) {
138 turnOff.apply(tooltips_pool[x]);
141 if (typeof $(this).data('bt-box') == 'object') {
142 // if there's already a popup, remove it before creating a new one.
146 // trigger preShow function
147 opts.preShow.apply(this);
151 if (opts.killTitle) {
152 // if we've killed the title attribute, it's been stored in 'bt-xTitle' so get it..
153 $(this).attr('title', $(this).attr('bt-xTitle'));
155 // then evaluate the selector... title is now in place
156 content = eval('$(this).' + opts.titleSelector);
157 if (opts.killTitle) {
158 // now remove the title again, so we don't get double tips
159 $(this).removeAttr('title');
163 var offsetParent = $(this).offsetParent();
164 var pos = $(this).btPosition();
165 var top = numb(pos.top) + numb($(this).css('margin-top')); // IE can return 'auto' for margins
166 var left = numb(pos.left) + numb($(this).css('margin-left'));
167 var width = $(this).outerWidth();
168 var height = $(this).outerHeight();
170 // get the dimensions of the text box
171 var $text = $('<div class="bt-content"></div>').append(content).css({padding: opts.padding + 'px', position: 'absolute', width: opts.width + 'px', zIndex: opts.textzIndex}).css(opts.cssStyles);
172 var $box = $('<div class="bt-wrapper"></div>').append($text).addClass(opts.cssClass).css({position: 'absolute', width: opts.width + 'px'}).appendTo(offsetParent);
174 $(this).data('bt-box', $box);
176 // see if the text box will fit in the various positions
177 var scrollTop = numb($(document).scrollTop());
178 var scrollLeft = numb($(document).scrollLeft());
179 var docWidth = numb($(window).width());
180 var docHeight = numb($(window).height());
181 var winRight = scrollLeft + docWidth;
182 var winBottom = scrollTop + docHeight;
183 var space = new Object();
184 space.top = $(this).offset().top - scrollTop;
185 space.bottom = docHeight - (($(this).offset().top + height) - scrollTop);
186 space.left = $(this).offset().left - scrollLeft;
187 space.right = docWidth - (($(this).offset().left + width) - scrollLeft);
188 var textOutHeight = numb($text.outerHeight());
189 var textOutWidth = numb($text.outerWidth());
190 if (opts.positions.constructor == String) {
191 opts.positions = opts.positions.replace(/ /, '').split(',');
193 if (opts.positions[0] == 'most') {
194 // figure out which is the largest
195 var position = 'top'; // prime the pump
196 for (var pig in space) { // pigs in space!
197 position = space[pig] > space[position] ? pig : position;
201 for (var x in opts.positions) {
202 var position = opts.positions[x];
203 if ((position == 'left' || position == 'right') && space[position] > textOutWidth + opts.spikeLength) {
206 else if ((position == 'top' || position == 'bottom') && space[position] > textOutHeight + opts.spikeLength) {
212 var horiz = left + ((width - textOutWidth)/2);
213 var vert = top + ((height - textOutHeight)/2);
214 var animDist = opts.animate ? numb(opts.distance) : 0;
215 var points = new Array();
216 var textTop, textLeft, textRight, textBottom, textTopSpace, textBottomSpace, textLeftSpace, textRightSpace, crossPoint, textCenter;
218 // Yes, yes, this next bit really could use to be condensed
219 // each switch case is basically doing the same thing in slightly different ways
223 $text.css('margin-bottom', opts.spikeLength + 'px');
224 $box.css({top: (top - $text.outerHeight(true) - animDist) + opts.overlap, left: horiz});
225 // move text left/right if extends out of window
226 textRightSpace = (winRight - opts.windowMargin) - ($text.offset().left + $text.outerWidth(true));
228 if (textRightSpace < 0) {
230 $box.css('left', (numb($box.css('left')) + textRightSpace) + 'px');
231 xShift -= textRightSpace;
233 // we test left space second to ensure that left of box is visible
234 textLeftSpace = ($text.offset().left + numb($text.css('margin-left'))) - (scrollLeft + opts.windowMargin);
235 if (textLeftSpace < 0) {
237 $box.css('left', (numb($box.css('left')) - textLeftSpace) + 'px');
238 xShift += textLeftSpace;
240 textTop = $text.btPosition().top + numb($text.css('margin-top'));
241 textLeft = $text.btPosition().left + numb($text.css('margin-left'));
242 textRight = textLeft + $text.outerWidth();
243 textBottom = textTop + $text.outerHeight();
244 textCenter = {x: textLeft + ($text.outerWidth()/2), y: textTop + ($text.outerHeight()/2)};
245 // points[points.length] = {x: x, y: y};
246 points[points.length] = spikePoint = {y: textBottom + opts.spikeLength, x: ((textRight-textLeft)/2) + xShift, type: 'spike'};
247 crossPoint = findIntersectX(spikePoint.x, spikePoint.y, textCenter.x, textCenter.y, textBottom);
248 // make sure that the crossPoint is not outside of text box boundaries
249 crossPoint.x = crossPoint.x < textLeft + opts.spikeGirth/2 + opts.cornerRadius ? textLeft + opts.spikeGirth/2 + opts.cornerRadius : crossPoint.x;
250 crossPoint.x = crossPoint.x > (textRight - opts.spikeGirth/2) - opts.cornerRadius ? (textRight - opts.spikeGirth/2) - opts.CornerRadius : crossPoint.x;
251 points[points.length] = {x: crossPoint.x - (opts.spikeGirth/2), y: textBottom, type: 'join'};
252 points[points.length] = {x: textLeft, y: textBottom, type: 'corner'}; // left bottom corner
253 points[points.length] = {x: textLeft, y: textTop, type: 'corner'}; // left top corner
254 points[points.length] = {x: textRight, y: textTop, type: 'corner'}; // right top corner
255 points[points.length] = {x: textRight, y: textBottom, type: 'corner'}; // right bottom corner
256 points[points.length] = {x: crossPoint.x + (opts.spikeGirth/2), y: textBottom, type: 'join'};
257 points[points.length] = spikePoint;
261 $text.css('margin-right', opts.spikeLength + 'px');
262 $box.css({top: vert + 'px', left: ((left - $text.outerWidth(true) - animDist) + opts.overlap) + 'px'});
263 // move text up/down if extends out of window
264 textBottomSpace = (winBottom - opts.windowMargin) - ($text.offset().top + $text.outerHeight(true));
266 if (textBottomSpace < 0) {
268 $box.css('top', (numb($box.css('top')) + textBottomSpace) + 'px');
269 yShift -= textBottomSpace;
271 // we ensure top space second to ensure that top of box is visible
272 textTopSpace = ($text.offset().top + numb($text.css('margin-top'))) - (scrollTop + opts.windowMargin);
273 if (textTopSpace < 0) {
275 $box.css('top', (numb($box.css('top')) - textTopSpace) + 'px');
276 yShift += textTopSpace;
278 textTop = $text.btPosition().top + numb($text.css('margin-top'));
279 textLeft = $text.btPosition().left + numb($text.css('margin-left'));
280 textRight = textLeft + $text.outerWidth();
281 textBottom = textTop + $text.outerHeight();
282 textCenter = {x: textLeft + ($text.outerWidth()/2), y: textTop + ($text.outerHeight()/2)};
283 points[points.length] = spikePoint = {x: textRight + opts.spikeLength, y: ((textBottom-textTop)/2) + yShift, type: 'spike'};
284 crossPoint = findIntersectY(spikePoint.x, spikePoint.y, textCenter.x, textCenter.y, textRight);
285 // make sure that the crossPoint is not outside of text box boundaries
286 crossPoint.y = crossPoint.y < textTop + opts.spikeGirth/2 + opts.cornerRadius ? textTop + opts.spikeGirth/2 + opts.cornerRadius : crossPoint.y;
287 crossPoint.y = crossPoint.y > (textBottom - opts.spikeGirth/2) - opts.cornerRadius ? (textBottom - opts.spikeGirth/2) - opts.cornerRadius : crossPoint.y;
288 points[points.length] = {x: textRight, y: crossPoint.y + opts.spikeGirth/2, type: 'join'};
289 points[points.length] = {x: textRight, y: textBottom, type: 'corner'}; // right bottom corner
290 points[points.length] = {x: textLeft, y: textBottom, type: 'corner'}; // left bottom corner
291 points[points.length] = {x: textLeft, y: textTop, type: 'corner'}; // left top corner
292 points[points.length] = {x: textRight, y: textTop, type: 'corner'}; // right top corner
293 points[points.length] = {x: textRight, y: crossPoint.y - opts.spikeGirth/2, type: 'join'};
294 points[points.length] = spikePoint;
298 $text.css('margin-top', opts.spikeLength + 'px');
299 $box.css({top: (top + height + animDist) - opts.overlap, left: horiz});
300 // move text up/down if extends out of window
301 textRightSpace = (winRight - opts.windowMargin) - ($text.offset().left + $text.outerWidth(true));
303 if (textRightSpace < 0) {
305 $box.css('left', (numb($box.css('left')) + textRightSpace) + 'px');
306 xShift -= textRightSpace;
308 // we ensure left space second to ensure that left of box is visible
309 textLeftSpace = ($text.offset().left + numb($text.css('margin-left'))) - (scrollLeft + opts.windowMargin);
310 if (textLeftSpace < 0) {
312 $box.css('left', (numb($box.css('left')) - textLeftSpace) + 'px');
313 xShift += textLeftSpace;
315 textTop = $text.btPosition().top + numb($text.css('margin-top'));
316 textLeft = $text.btPosition().left + numb($text.css('margin-left'));
317 textRight = textLeft + $text.outerWidth();
318 textBottom = textTop + $text.outerHeight();
319 textCenter = {x: textLeft + ($text.outerWidth()/2), y: textTop + ($text.outerHeight()/2)};
320 points[points.length] = spikePoint = {x: ((textRight-textLeft)/2) + xShift, y: 0, type: 'spike'};
321 crossPoint = findIntersectX(spikePoint.x, spikePoint.y, textCenter.x, textCenter.y, textTop);
322 // make sure that the crossPoint is not outside of text box boundaries
323 crossPoint.x = crossPoint.x < textLeft + opts.spikeGirth/2 + opts.cornerRadius ? textLeft + opts.spikeGirth/2 + opts.cornerRadius : crossPoint.x;
324 crossPoint.x = crossPoint.x > (textRight - opts.spikeGirth/2) - opts.cornerRadius ? (textRight - opts.spikeGirth/2) - opts.cornerRadius : crossPoint.x;
325 points[points.length] = {x: crossPoint.x + opts.spikeGirth/2, y: textTop, type: 'join'};
326 points[points.length] = {x: textRight, y: textTop, type: 'corner'}; // right top corner
327 points[points.length] = {x: textRight, y: textBottom, type: 'corner'}; // right bottom corner
328 points[points.length] = {x: textLeft, y: textBottom, type: 'corner'}; // left bottom corner
329 points[points.length] = {x: textLeft, y: textTop, type: 'corner'}; // left top corner
330 points[points.length] = {x: crossPoint.x - (opts.spikeGirth/2), y: textTop, type: 'join'};
331 points[points.length] = spikePoint;
335 $text.css('margin-left', (opts.spikeLength + 'px'));
336 $box.css({top: vert + 'px', left: ((left + width + animDist) - opts.overlap) + 'px'});
337 // move text up/down if extends out of window
338 textBottomSpace = (winBottom - opts.windowMargin) - ($text.offset().top + $text.outerHeight(true));
340 if (textBottomSpace < 0) {
342 $box.css('top', (numb($box.css('top')) + textBottomSpace) + 'px');
343 yShift -= textBottomSpace;
345 // we ensure top space second to ensure that top of box is visible
346 textTopSpace = ($text.offset().top + numb($text.css('margin-top'))) - (scrollTop + opts.windowMargin);
347 if (textTopSpace < 0) {
349 $box.css('top', (numb($box.css('top')) - textTopSpace) + 'px');
350 yShift += textTopSpace;
352 textTop = $text.btPosition().top + numb($text.css('margin-top'));
353 textLeft = $text.btPosition().left + numb($text.css('margin-left'));
354 textRight = textLeft + $text.outerWidth();
355 textBottom = textTop + $text.outerHeight();
356 textCenter = {x: textLeft + ($text.outerWidth()/2), y: textTop + ($text.outerHeight()/2)};
357 points[points.length] = spikePoint = {x: 0, y: ((textBottom-textTop)/2) + yShift, type: 'spike'};
358 crossPoint = findIntersectY(spikePoint.x, spikePoint.y, textCenter.x, textCenter.y, textLeft);
359 // make sure that the crossPoint is not outside of text box boundaries
360 crossPoint.y = crossPoint.y < textTop + opts.spikeGirth/2 + opts.cornerRadius ? textTop + opts.spikeGirth/2 + opts.cornerRadius : crossPoint.y;
361 crossPoint.y = crossPoint.y > (textBottom - opts.spikeGirth/2) - opts.cornerRadius ? (textBottom - opts.spikeGirth/2) - opts.cornerRadius : crossPoint.y;
362 points[points.length] = {x: textLeft, y: crossPoint.y - opts.spikeGirth/2, type: 'join'};
363 points[points.length] = {x: textLeft, y: textTop, type: 'corner'}; // left top corner
364 points[points.length] = {x: textRight, y: textTop, type: 'corner'}; // right top corner
365 points[points.length] = {x: textRight, y: textBottom, type: 'corner'}; // right bottom corner
366 points[points.length] = {x: textLeft, y: textBottom, type: 'corner'}; // left bottom corner
367 points[points.length] = {x: textLeft, y: crossPoint.y + opts.spikeGirth/2, type: 'join'};
368 points[points.length] = spikePoint;
372 var canvas = $('<canvas width="'+ (numb($text.outerWidth(true)) + opts.strokeWidth*2) +'" height="'+ (numb($text.outerHeight(true)) + opts.strokeWidth*2) +'"></canvas>').appendTo($box).css({position: 'absolute', top: $text.btPosition().top, left: $text.btPosition().left, zIndex: opts.boxzIndex}).get(0);
374 // if excanvas is set up, we need to initialize the new canvas element
375 if (typeof G_vmlCanvasManager != 'undefined') {
376 canvas = G_vmlCanvasManager.initElement(canvas);
379 if (opts.cornerRadius > 0) {
380 // round the corners!
381 var newPoints = new Array();
383 for (var i=0; i<points.length; i++) {
384 if (points[i].type == 'corner') {
385 // create two new arc points
386 // find point between this and previous (using modulo in case of ending)
387 newPoint = betweenPoint(points[i], points[(i-1)%points.length], opts.cornerRadius);
388 newPoint.type = 'arcStart';
389 newPoints[newPoints.length] = newPoint;
390 // the original corner point
391 newPoints[newPoints.length] = points[i];
392 // find point between this and next
393 newPoint = betweenPoint(points[i], points[(i+1)%points.length], opts.cornerRadius);
394 newPoint.type = 'arcEnd';
395 newPoints[newPoints.length] = newPoint;
398 newPoints[newPoints.length] = points[i];
401 // overwrite points with new version
406 var ctx = canvas.getContext("2d");
407 drawIt.apply(ctx, [points]);
408 ctx.fillStyle = opts.fill;
410 ctx.shadowOffsetX = 2;
411 ctx.shadowOffsetY = 2;
413 ctx.shadowColor = opts.shadowColor;
417 if (opts.strokeWidth > 0) {
418 ctx.lineWidth = opts.strokeWidth;
419 ctx.strokeStyle = opts.strokeStyle;
421 drawIt.apply(ctx, [points]);
427 $box.css({opacity: 0.1});
430 $box.css({visibility: 'visible'});
433 var overlay = $('<div class="bt-overlay"></div>').css({
434 position: 'absolute',
435 backgroundColor: 'blue',
441 }).appendTo(offsetParent);
442 $(this).data('overlay', overlay);
445 var animParams = {opacity: 1};
449 animParams.top = $box.btPosition().top + opts.distance;
452 animParams.left = $box.btPosition().left + opts.distance;
455 animParams.top = $box.btPosition().top - opts.distance;
458 animParams.left = $box.btPosition().left - opts.distance;
461 $box.animate(animParams, {duration: opts.speed, easing: opts.easing});
464 // trigger postShow function
465 opts.postShow.apply(this);
470 var turnOff = function() {
472 // trigger preHide function
473 opts.preHide.apply(this);
475 var box = $(this).data('bt-box');
476 var overlay = $(this).data('bt-overlay');
477 if (typeof box == 'object') {
479 $(this).removeData('bt-box');
481 if (typeof overlay == 'object') {
483 $(this).removeData('bt-overlay');
486 // trigger postHide function
487 opts.postHide.apply(this);
491 var refresh = function() {
497 * This is sort of the "starting spot" for the this.each()
498 * These are sort of the init functions to handle the call
501 if (opts.killTitle) {
502 $(this).find('[title]').andSelf().each(function() {
503 $(this).attr('bt-xTitle', $(this).attr('title')).removeAttr('title');
506 if (typeof opts.trigger == 'string') {
507 opts.trigger = [opts.trigger];
509 if (opts.trigger[0] == 'hover') {
519 else if (opts.trigger[0] == 'now') {
520 var box = $(this).data('bt-box');
521 if (typeof box == 'object') {
528 else if (opts.trigger.length > 1 && opts.trigger[0] != opts.trigger[1]) {
530 .bind(opts.trigger[0], function() {
533 .bind(opts.trigger[1], function() {
538 // toggle using the same event
539 $(this).bind(opts.trigger[0], function() {
540 if (typeof this.triggerToggle == 'undefined') {
541 this.triggerToggle = false;
543 this.triggerToggle = !this.triggerToggle;
544 if (this.triggerToggle) {
552 }); // </ this.each() >
555 function drawIt(points) {
556 this.moveTo(points[0].x, points[0].y);
557 for (i=1;i<points.length;i++) {
558 if (points[i-1].type == 'arcStart') {
559 // if we're creating a rounded corner
560 //ctx.arc(round5(points[i].x), round5(points[i].y), points[i].startAngle, points[i].endAngle, opts.cornerRadius, false);
561 this.quadraticCurveTo(round5(points[i].x), round5(points[i].y), round5(points[(i+1)%points.length].x), round5(points[(i+1)%points.length].y));
563 //ctx.moveTo(round5(points[i].x), round5(points[i].y));
566 this.lineTo(round5(points[i].x), round5(points[i].y));
572 * Round to the nearest .5 pixel to avoid antialiasing
573 * http://developer.mozilla.org/en/Canvas_tutorial/Applying_styles_and_colors
575 function round5(num) {
576 return Math.round(num - .5) + .5;
580 * Ensure that a number is a number... or zero
583 return parseInt(num) || 0;
587 * Given two points, find a point which is dist pixels from point1 on a line to point2
589 function betweenPoint(point1, point2, dist) {
590 // figure out if we're horizontal or vertical
592 if (point1.x == point2.x) {
594 y = point1.y < point2.y ? point1.y + dist : point1.y - dist;
595 return {x: point1.x, y: y};
597 else if (point1.y == point2.y) {
599 x = point1.x < point2.x ? point1.x + dist : point1.x - dist;
600 return {x:x, y: point1.y};
604 function centerPoint(arcStart, corner, arcEnd) {
605 var x = corner.x == arcStart.x ? arcEnd.x : arcStart.x;
606 var y = corner.y == arcStart.y ? arcEnd.y : arcStart.y;
607 var startAngle, endAngle;
608 if (arcStart.x < arcEnd.x) {
609 if (arcStart.y > arcEnd.y) {
610 // arc is on upper left
611 startAngle = (Math.PI/180)*180;
612 endAngle = (Math.PI/180)*90;
615 // arc is on upper right
616 startAngle = (Math.PI/180)*90;
621 if (arcStart.y > arcEnd.y) {
622 // arc is on lower left
623 startAngle = (Math.PI/180)*270;
624 endAngle = (Math.PI/180)*180;
627 // arc is on lower right
629 endAngle = (Math.PI/180)*270;
632 return {x: x, y: y, type: 'center', startAngle: startAngle, endAngle: endAngle};
636 * Find the intersection point of two lines, each defined by two points
637 * arguments are x1, y1 and x2, y2 for r1 (line 1) and r2 (line 2)
638 * It's like an algebra party!!!
640 function findIntersect(r1x1, r1y1, r1x2, r1y2, r2x1, r2y1, r2x2, r2y2) {
643 return findIntersectY(r1x1, r1y1, r1x2, r1y2, r2x1);
646 return findIntersectX(r1x1, r1y1, r1x2, r1y2, r2y1);
649 // m = (y1 - y2) / (x1 - x2) // <-- how to find the slope
650 // y = mx + b // the 'classic' linear equation
651 // b = y - mx // how to find b (the y-intersect)
652 // x = (y - b)/m // how to find x
653 var r1m = (r1y1 - r1y2) / (r1x1 - r1x2);
654 var r1b = r1y1 - (r1m * r1x1);
655 var r2m = (r2y1 - r2y2) / (r2x1 - r2x2);
656 var r2b = r2y1 - (r2m * r2x1);
658 var x = (r2b - r1b) / (r1m - r2m);
659 var y = r1m * x + r1b;
665 * Find the y intersection point of a line and given x vertical
667 function findIntersectY(r1x1, r1y1, r1x2, r1y2, x) {
669 return {x: x, y: r1y1};
671 var r1m = (r1y1 - r1y2) / (r1x1 - r1x2);
672 var r1b = r1y1 - (r1m * r1x1);
674 var y = r1m * x + r1b;
680 * Find the x intersection point of a line and given y horizontal
682 function findIntersectX(r1x1, r1y1, r1x2, r1y2, y) {
684 return {x: r1x1, y: y};
686 var r1m = (r1y1 - r1y2) / (r1x1 - r1x2);
687 var r1b = r1y1 - (r1m * r1x1);
689 // y = mx + b // your old friend, linear equation
690 // x = (y - b)/m // linear equation solved for x
691 var x = (y - r1b) / r1m;
697 }; // </ jQuery.fn.bt() >
700 * jQuery's compat.js (used in Drupal's jQuery upgrade module, overrides the $().position() function
701 * this is a copy of that function to allow the plugin to work when compat.js is present
702 * once compat.js is fixed to not override existing functions, this function can be removed
703 * and .btPosion() can be replaced with .position() above...
705 jQuery.fn.btPosition = function() {
707 function num(elem, prop) {
708 return elem[0] && parseInt( jQuery.curCSS(elem[0], prop, true), 10 ) || 0;
711 var left = 0, top = 0, results;
714 // Get *real* offsetParent
715 var offsetParent = this.offsetParent(),
717 // Get correct offsets
718 offset = this.offset(),
719 parentOffset = /^body|html$/i.test(offsetParent[0].tagName) ? { top: 0, left: 0 } : offsetParent.offset();
721 // Subtract element margins
722 // note: when an element has margin: auto the offsetLeft and marginLeft
723 // are the same in Safari causing offset.left to incorrectly be 0
724 offset.top -= num( this, 'marginTop' );
725 offset.left -= num( this, 'marginLeft' );
727 // Add offsetParent borders
728 parentOffset.top += num( offsetParent, 'borderTopWidth' );
729 parentOffset.left += num( offsetParent, 'borderLeftWidth' );
731 // Subtract the two offsets
733 top: offset.top - parentOffset.top,
734 left: offset.left - parentOffset.left
739 }; // </ jQuery.fn.btPosition() >
743 * Defaults for the beauty tips
745 * Note this is a variable definition and not a function. So defaults can be
746 * written for an entire page by simply redefining attributes like so:
748 * jQuery.fn.bt.defaults.width = 400;
750 * This would make all Beauty Tips boxes 400px wide.
752 * Each of these options may also be overridden during
754 * Can be overriden globally or at time of call.
757 jQuery.fn.bt.defaults = {
758 trigger: 'hover', // trigger to show/hide tip
759 // use [on, off] to define separate on/off triggers
760 // also use space character to allow multiple events to trigger
762 // ['focus', 'blur'] // focus displays, blur hides
763 // 'dblclick' // dblclick toggles on/off
764 // ['focus mouseover', 'blur mouseout']
765 // 'now' // shows/hides tip without event
767 width: 200, // width (in px) of tooltip box
768 // when combined with cssStyles: {width: 'auto'}, this becomes a max-width for the text
769 padding: 10, // padding for content (get more fine grained with cssStyles)
770 spikeGirth: 10, // width of spike
771 spikeLength: 15, // length of spike
772 overlap: 0, // spike overlap (px) onto target
773 overlay: false, // display overlay on target (use CSS to style) -- BUGGY!
774 killTitle: true, // kill title tag to avoid double tooltips
776 textzIndex: 9999, // z-index for the text
777 boxzIndex: 9990, // z-index for the "talk" box (should always be less than textzIndex)
778 positions: ['most'], // preference of positions for tip (will use first with available space)
779 // possible values 'top', 'bottom', 'left', 'right' as an array in order of
780 // preference. Last value will be used if others don't have enough space.
782 // or use 'most' to use the area with the most space
783 fill: "rgb(255, 255, 102)", // fill color for the tooltip box
784 windowMargin: 10, // space (px) to leave between text box and browser edge
786 strokeWidth: 1, // width of stroke around box, **set to 0 for no stroke**
787 strokeStyle: "#000", // color/alpha of stroke
789 cornerRadius: 5, // radius of corners (px), set to 0 for square corners
791 shadow: false, // use drop shadow? (only displays in Safari and FF 3.1)
792 shadowOffsetX: 2, // shadow offset x (px)
793 shadowOffsetY: 2, // shadow offset y (px)
794 shadowBlur: 3, // shadow blur (px)
795 shadowColor: "#000", // shadow color/alpha
797 animate: false, // animate show/hide of box - EXPERIMENTAL (buggy in IE)
798 distance: 15, // distance of animation movement (px)
799 easing: 'swing', // animation easing
800 speed: 200, // speed (ms) of animation
802 cssClass: '', // CSS class to add to the box wrapper div
803 cssStyles: {}, // styles to add the text box
804 // example: {fontFamily: 'Georgia, Times, serif', fontWeight: 'bold'}
806 titleSelector: "attr('title')", // if there is no content argument, use this selector to retrieve the title
808 preShow: function(){return;}, // function to run before popup is built and displayed
809 postShow: function(){return;}, // function to run after popup is built and displayed
810 preHide: function(){return;}, // function to run before popup is removed
811 postHide: function(){return;} // function to run after popup is removed
813 }; // </ jQuery.bt.defaults >