/** *  hovertipL - easy and elegant tooltips *   *  By Dave Cohen <http://dave-cohen.com> *  With ideas and and javascript code borrowed from many folks. *  (See URLS in the comments) *   *  Licensed under GPL.  *  Requires jQuery.js.  <http://jquery.com>,  *  which may be distributed under a different licence. *   *  $Date: 2006-09-15 12:49:19 -0700 (Fri, 15 Sep 2006) $ *  $Rev: $ *  $Id:$ //// mouse events /////** * To make hovertipLs appear correctly we need the exact mouse position. * These functions make that possible. */// use globals to track mouse positionvar hovertipLMouseX;var hovertipLMouseY;function hovertipLMouseUpdate(e) {  var mouse = hovertipLMouseXY(e);  hovertipLMouseX = mouse[0];  hovertipLMouseY = mouse[1];}// http://www.howtocreate.co.uk/tutorials/javascript/eventinfofunction hovertipLMouseXY(e) {  if( !e ) {    if( window.event ) {      //Internet Explorer      e = window.event;    } else {      //total failure, we have no way of referencing the event      return;    }  }  if( typeof( e.pageX ) == 'number' ) {    //most browsers    var xcoord = e.pageX;    var ycoord = e.pageY;  } else if( typeof( e.clientX ) == 'number' ) {    //Internet Explorer and older browsers    //other browsers provide this, but follow the pageX/Y branch    var xcoord = e.clientX;    var ycoord = e.clientY;    var badOldBrowser = ( window.navigator.userAgent.indexOf( 'Opera' ) + 1 ) ||      ( window.ScriptEngine && ScriptEngine().indexOf( 'InScript' ) + 1 ) ||      ( navigator.vendor == 'KDE' );    if( !badOldBrowser ) {      if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {        //IE 4, 5 & 6 (in non-standards compliant mode)        xcoord += document.body.scrollLeft;        ycoord += document.body.scrollTop;      } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {        //IE 6 (in standards compliant mode)        xcoord += document.documentElement.scrollLeft;        ycoord += document.documentElement.scrollTop;      }    }  } else {    //total failure, we have no way of obtaining the mouse coordinates    return;  }  return [xcoord, ycoord];}//// target selectors /////** * These selectors find the targets for a given tooltip element.   * Several methods are supported.   *  * You may write your own selector functions to customize. *//** * For this model: * <span hovertipL="ht1">target term</span>... * <div class="hovertipL" id="ht1">tooltip text</div> */targetSelectById = function(el, config) {  var id;  var selector;  if (id = el.getAttribute('id')) {    selector = '*[@'+config.attribute+'=\''+id+'\']';    return $(selector);  }};/** * For this model: * <span id="ht1">target term</span>... * <div class="hovertipL" target="ht1">tooltip text</div> */targetSelectByTargetAttribute = function(el, config) {  target_list = el.getAttribute('target');  if (target_list) {    // use for attribute to specify targets    target_ids = target_list.split(' ');    var selector = '#' + target_ids.join(',#');    return $(selector);  }};/** * For this model: * <span>target term</span><span class="hovertipL">tooltip text</span> */targetSelectByPrevious = function(el, config) {  return $(el.previousSibling);}/** * Make all siblings targets.  Experimental. */targetSelectBySiblings = function(el, config) {  return $(el).siblings();}//// prepare tip elements /////** * The tooltip element needs special preparation.  You may define your own * prepare functions to cusomize the behavior. */// adds a close link to clicktipsclicktipPrepareWithCloseLink = function(o, config) {  return o.append("<a class='clicktip_close'><span>close</span></a>")  .find('a.clicktip_close').click(function(e) {      o.hide();      return false;    }).end(); };// ensure that hovertipLs do not disappear when the mouse is over them.// also position the hovertipL as an absolutely positioned child of body.hovertipLPrepare = function(o, config) {  return o.hover(function() {      hovertipLHideCancel(this);    }, function() {      hovertipLHideLater(this);    }).css('position', 'absolute').each(hovertipLPosition);};// do not modify tooltips when preparinghovertipLPrepareNoOp = function(o, config) {  return o;}//// manipulate tip elements //////** * A variety of functions to modify tooltip elements */// move tooltips to body, so they are not descended from other absolutely// positioned elements.hovertipLPosition = function(i) {  document.body.appendChild(this);}hovertipLIsVisible = function(el) {  return (jQuery.css(el, 'display') != 'none');}// show the tooltip under the mouse.// Introduce a delay, so tip appears only if cursor rests on target for more than an instant.hovertipLShowUnderMouse = function(el) {  hovertipLHideCancel(el);  if (!hovertipLIsVisible(el)) {    el.ht.showing = // keep reference to timer      window.setTimeout(function() {          el.ht.tip.css({              'position':'absolute',                'top': hovertipLMouseY + 'px',                'left': hovertipLMouseX + 'px'})            .show();        }, el.ht.config.showDelay);  }};// do not hidehovertipLHideCancel = function(el) {  if (el.ht.hiding) {    window.clearTimeout(el.ht.hiding);    el.ht.hiding = null;  }  };// Hide a tooltip, but only after a delay.// The delay allow the tip to remain when user moves mouse from target to tooltiphovertipLHideLater = function(el) {  if (el.ht.showing) {    window.clearTimeout(el.ht.showing);    el.ht.showing = null;  }  if (el.ht.hiding) {    window.clearTimeout(el.ht.hiding);    el.ht.hiding = null;  }  el.ht.hiding =   window.setTimeout(function() {      if (el.ht.hiding) {        // fadeOut, slideUp do not work on Konqueror        el.ht.tip.hide();      }    }, el.ht.config.hideDelay);};//// prepare target elements /////** * As we prepared the tooltip elements, the targets also need preparation. *  * You may define your own custom behavior. */// when clicked on target, toggle visibilty of tooltipclicktipTargetPrepare = function(o, el, config) {  return o.addClass(config.attribute + '_target')  .click(function() {      el.ht.tip.toggle();      return false;    });};// when hover over target, make tooltip appearhovertipLTargetPrepare = function(o, el, config) {  return o.addClass(config.attribute + '_target')  .hover(function() {      // show tip when mouse over target      hovertipLShowUnderMouse(el);    },    function() {      // hide the tip      // add a delay so user can move mouse from the target to the tip      hovertipLHideLater(el);    });};/** * hovertipLActivate() is our jQuery plugin function.  It turns on hovertipL or * clicktip behavior for a set of elements. *  * @param config  * controls aspects of tooltip behavior.  Be sure to define * 'attribute', 'showDelay' and 'hideDelay'. *  * @param targetSelect * function finds the targets of a given tooltip element. *  * @param tipPrepare * function alters the tooltip to display and behave properly *  * @param targetPrepare * function alters the target to display and behave properly. */jQuery.fn.hovertipLActivate = function(config, targetSelect, tipPrepare, targetPrepare) {  //alert('activating ' + this.size());  // unhide so jquery show/hide will work.  return this.css('display', 'block')  .hide() // don't show it until click  .each(function() {      if (!this.ht)        this.ht = new Object();      this.ht.config = config;            // find our targets      var targets = targetSelect(this, config);      if (targets && targets.size()) {        if (!this.ht.targets)          this.ht.targets = targetPrepare(targets, this, config);        else          this.ht.targets.add(targetPrepare(targets, this, config));                // listen to mouse move events so we know exatly where to place hovetips        targets.mousemove(hovertipLMouseUpdate);                // prepare the tooltip element        // is it bad form to call $(this) here?        if (!this.ht.tip)          this.ht.tip = tipPrepare($(this), config);      }          })  ;};/** * Here's an example ready function which shows how to enable tooltips. *  * You can make this considerably shorter by choosing only the markup style(s) * you will use. *  * You may also remove the code that wraps hovertipLs to produce drop-shadow FX *  * Invoke this function or one like it from your $(document).ready().  *   *  Here, we break the action up into several timout callbacks, to avoid *  locking up browsers. */function hovertipLInit() {  // specify the attribute name we use for our clicktips  var clicktipConfig = {'attribute':'clicktip'};    /**   * To enable this style of markup (id on tooltip):   * <span clicktip="foo">target</span>...   * <div id="foo" class="clicktip">blah blah</div>   */  window.setTimeout(function() {    $('.clicktip').hovertipLActivate(clicktipConfig,                                    targetSelectById,                                    clicktipPrepareWithCloseLink,                                    clicktipTargetPrepare);  }, 0);    /**   * To enable this style of markup (id on target):   * <span id="foo">target</span>...   * <div target="foo" class="clicktip">blah blah</div>   */  window.setTimeout(function() {    $('.clicktip').hovertipLActivate(clicktipConfig,                                    targetSelectByTargetAttribute,                                    clicktipPrepareWithCloseLink,                                    clicktipTargetPrepare);  }, 0);    // specify our configuration for hovertipLs, including delay times (millisec)  var hovertipLConfig = {'attribute':'hovertipL',                        'showDelay': 300,                        'hideDelay': 400};    // use <div class='hovertipL'>blah blah</div>  var hovertipLSelect = 'ul.hovertipL';    /**   * To enable this style of markup (id on tooltip):   * <span hovertipL="foo">target</span>...   * <div id="foo" class="hovertipL">blah blah</div>   */  /**   * To enable this style of markup (id on target):   * <span id="foo">target</span>...   * <div target="foo" class="hovertipL">blah blah</div>   */  window.setTimeout(function() {    $(hovertipLSelect).hovertipLActivate(hovertipLConfig,                                       targetSelectByTargetAttribute,                                       hovertipLPrepare,                                       hovertipLTargetPrepare);  }, 0);    /**   * This next section enables this style of markup:   * <foo><span>target</span><span class="hovertipL">blah blah</span></foo>   *    * With drop shadow effect.   *    */  var hovertipLSpanSelect = 'span.hovertipL';  // activate hovertipLs with wrappers for FX (drop shadow):  $(hovertipLSpanSelect).css('display', 'block').addClass('hovertipL_wrap3').    wrap("<span class='hovertipL_wrap0'><span class='hovertipL_wrap1'><span class='hovertipL_wrap2'>" +          "</span></span></span>").each(function() {           // fix class and attributes for newly wrapped elements           var tooltip = this.parentNode.parentNode.parentNode;           if (this.getAttribute('target'))             tooltip.setAttribute('target', this.getAttribute('target'));           if (this.getAttribute('id')) {             var id = this.getAttribute('id');             this.removeAttribute('id');             tooltip.setAttribute('id', id);           }         });  hovertipLSpanSelect = 'span.hovertipL_wrap0';  window.setTimeout(function() {    $(hovertipLSpanSelect)      .hovertipLActivate(hovertipLConfig,                        targetSelectByPrevious,                        hovertipLPrepare,                        hovertipLTargetPrepare);  }, 0);}
