/**
 * $Id: jtip.js 34885 2010-12-16 18:18:15Z jed $
 *
 * Adapted from jtip by Cody Lindley.  The only things left from the original are the styles and divs, plus some of the
 * location calculations.  Everything else has been reworked to pull the tip data from an "em" element following the
 * link rather than parsing the href attribute for a request param that was used for an AJAX request to load the tip
 * data.
 * $Author: jed $
 * $Revision: 34885 $ $Date: 2010-12-16 13:18:15 -0500 (Thu, 16 Dec 2010) $
 */
$(initTips);

/**
 * Registers hover functions with all links whose class is "jtip"
 */
function initTips() {
   $("a.jtip").hover(function() { showTip(this); }, removeTip);
}

/**
 * Displays the tip over the given link.  Tip text is retrieved from the next "em" element after the link.
 * @param link a link that needs a tooltip
 */
function showTip(link) {
   var tip = $(link).next("em").text();
   if (tip !== undefined) {
      var left = renderTip(link);
      $('#JT').css({left: left + "px", top: computeTop(link) + "px"});
      $('#JT_copy').text(tip);
      $('#JT').show();
   }
}

/**
 * Renders the tip over the given link by appending the tip div to the body and computing the
 * left position of the tip for later use.
 * @param link a link
 * @return the left position of the tip, to be dynamically plugged into the CSS for the tip
 */
function renderTip(link) {
   var tipWidth = 250;
   var element = document.documentElement;
   var width = self.innerWidth || (element && element.clientWidth) || document.body.clientWidth;
   var leftOffset = width - getAbsoluteLeft(link.id);
   var left;

   if (leftOffset > (tipWidth + 75)) {
      $("body").append("<div id='JT' style='width:" + tipWidth
               + "px'><div id='JT_arrow_left'></div><div id='JT_close_left'>"
               + "</div><div id='JT_copy'><div class='JT_loader'><div></div></div>");//right side
      var arrowOffset = link.offsetWidth + 11;
      left = getAbsoluteLeft(link.id) + arrowOffset; //set x position
   } else {
      $("body").append("<div id='JT' style='width:" + tipWidth
               + "px'><div id='JT_arrow_right' style='left:" + (tipWidth + 1)
               + "px'></div><div id='JT_close_right'>"
               + "</div><div id='JT_copy'><div class='JT_loader'><div></div></div>");//left side
      left = getAbsoluteLeft(link.id) - (tipWidth + 15); //set x position
   }

   return left;
}

/**
 * Computes the top of the tooltip based on the given link
 * @param link a link
 * @return the top position of the tooltip, to be dynamically plugged into the CSS for the tip
 */
function computeTop(link) {
   return getAbsoluteTop(link.id) - 3;
}

/**
 * Computes the left position of an object from the upper left viewport corner
 * @param objectId the ID of the object
 * @return the left position of the object with <code>objectId</code>
 */
function getAbsoluteLeft(objectId) {
   var element = document.getElementById(objectId);
   var left = element.offsetLeft;
   while (element.offsetParent != null) {
      var parent = element.offsetParent;
      left += parent.offsetLeft;
      element = parent;
   }

   return left;
}

/**
 * Computes the top position of an object from the upper left viewport corner
 * @param objectId the ID of the object
 * @return the top position of the object with <code>objectId</code>
 */
function getAbsoluteTop(objectId) {
   var element = document.getElementById(objectId);
   var top = element.offsetTop;
   while (element.offsetParent != null) {
      var parent = element.offsetParent;
      top += parent.offsetTop;
      element = parent;
   }

   return top;
}

/**
 * Removes the tip from the DOM.
 */
function removeTip() {
   $('#JT').remove();
}

