/*
@author Derived from code posted by Caleb Tucker to http://rndnext.blogspot.com/2009/02/jquery-ajax-tooltip.html on February 12, 2009 and adapted by Kevin Haidl on September 7, 2009.

@copyright By posting his code to a blog with no copyright statement or terms of use, I believe Mr. Tucker put into the public domain.  Modifications are copyright Speedypin LLC, 2009 with all rights reserved.

 */
function ajaxTooltipReady() {
    jQuery(function()  
      {  
	  var hideDelay = 500;    
	  var currentID;  
	  var hideTimer = null;  
	  
	  // One instance that's reused to show info for the current person  
	  var container = jQuery('<div id="ajax-tooltip-container">'  
			    + '<table width="" border="0" cellspacing="0" cellpadding="0" align="center" class="ajax-tooltip-popup">'  
			    + '<tr>'  
			    + '   <td class="ajax-tooltip-corner ajax-tooltip-top-left"></td>'  
			    + '   <td class="ajax-tooltip-top"></td>'  
			    + '   <td class="ajax-tooltip-corner ajax-tooltip-top-right"></td>'  
			    + '</tr>'  
			    + '<tr>'  
			    + '   <td class="ajax-tooltip-left">&nbsp;</td>'  
			    + '   <td class="ajax-tooltip-content-cell"><div id="ajax-tooltip-content"></div></td>'  
			    + '   <td class="ajax-tooltip-right">&nbsp;</td>'  
			    + '</tr>'  
			    + '<tr>'  
			    + '   <td class="ajax-tooltip-corner ajax-tooltip-bottom-left">&nbsp;</td>'  
			    + '   <td class="ajax-tooltip-bottom">&nbsp;</td>'  
			    + '   <td class="ajax-tooltip-corner ajax-tooltip-bottom-right"></td>'  
			    + '</tr>'  
			    + '</table>'  
			    + '</div>');  
	  
	  jQuery('body').append(container);  
	  
	  jQuery('.ajax-tooltip-trigger').live('mouseover', function()  
					  {  
					      currentTriggerId = jQuery(this).attr('id');
					      // format of 'rel' tag: pageid,personguid  
					      var tipUrl = jQuery(this).attr('rel');
						  
						  
					      if (hideTimer)  
						  clearTimeout(hideTimer);  

					      /*
					      var pos = jQuery(this).offset();  
					      var width = jQuery(this).width();  
					      container.css({  
						      left: (pos.left + width) + 'px',  
							  top: pos.top - 5 + 'px'  
							  });  
					      */

					      jQuery('#ajax-tooltip-content').html('&nbsp;');  
					      
					      jQuery.ajax({  
						      type: 'GET',  
							  url: tipUrl,  
							  data: '', 
							  success: function(data)  
							  {  
							      // The first data appears to be the URL encoded key-value pairs for the GET.  The second data appears to be the text returned by the url. --KH
							      
							      // Verify that we're pointed to a page that returned the expected results.  
							      if (data.indexOf('ajax-tooltip-result') < 0) {
								  // The web server probably returned its own error result, but since we're calling a static resource, it has to be a 404.  Show that.
								  jQuery('#ajax-tooltip-content').html('<span >Error 404: The tip named "' + tipName + '" does not exist.</span>');  
							      }
							      else {
								  jQuery('#ajax-tooltip-content').html(data);  
							      }
							      
							      // The original script passed a session ID to a dynamic AJAX script with the intent
							      // of limiting the work done to respond to multiple AJAX requests for the same resource.
							      // By putting limitation fork here though, they only saved themselves the client side work. -KH
							      dockToElement('#' + currentTriggerId, "#ajax-tooltip-container", 20);
							      
							  }  
						  });  
					      
					      container.css('display', 'block');  
					  });  
	  
	  jQuery('.ajax-tooltip-trigger').live('mouseout', function()  
					  {  
					      if (hideTimer)  
						  clearTimeout(hideTimer);  
					      hideTimer = setTimeout(function()  
								     {  
									 container.css('display', 'none');  
								     }, hideDelay);  
					  });  
	  
	  // Allow mouse over of details without hiding details  
	  jQuery('#ajax-tooltip-container').mouseover(function()  
						 {  
						     if (hideTimer)  
							 clearTimeout(hideTimer);  
						 });  
	  
	  // Hide after mouseout  
	  jQuery('#ajax-tooltip-container').mouseout(function()  
						{  
						    if (hideTimer)  
							clearTimeout(hideTimer);  
						    hideTimer = setTimeout(function()  
									   {  
									       container.css('display', 'none');  
									   }, hideDelay);  
						});  
      });  
}

/*

Given two CSS selectors uniquely referring to distinct elements and an
optional number of pixels to leave between them, dock the second
element (the "ship") around the first (the "dock") at a position which
is optimal for the client context (i.e., available width and height).
If the ship can dock cleanly to the top-right, it will.

@param jQueryObject the element to dock to
@param jQueryObject the element to be docked
@param integer the number of pixels to leave between the dock and docker.

*/
function dockToElement(dock, ship, clearance) {
    clearance *= 1; // To ensure a default of 0 if no integer is passed.
    dock = jQuery(dock);
    ship = jQuery(ship);

    // Get the dimensions of the client.
    var clientWidth = jQuery(window).width();
    var clientHeight = jQuery(window).height();
    var clientXOffset = jQuery(document).scrollLeft();
    var clientYOffset = jQuery(document).scrollTop();

    // Get the dimensions of the dock and ship relative to the document.
    //
    var shipWidth = Math.round(ship.width());
    var shipHeight = Math.round(ship.height());
    var dockWidth = Math.round(dock.width());
    var dockHeight = Math.round(dock.height());
    var dockPosition = dock.offset();
    var dockX = Math.round(dockPosition.left);
    var dockY = Math.round(dockPosition.top);


    // Arguments posX and posY are assumed to be relative to the *page*.
    // We need the matching coordinates relative to the *screen*.
    var screenDockX = dockX - clientXOffset;
    var screenDockY = dockY - clientYOffset;

    // Calculate the clearance between the ship and the edge
    // of the client if the ship were to dock at the top, right, bottom or
    // left side of the dock.
    //
    var clearanceLeft = screenDockX - shipWidth - clearance;
    var clearanceRight = clientWidth - screenDockX - dockWidth - shipWidth - clearance;
    var clearanceTop = screenDockY - shipHeight - clearance;
    var clearanceBottom = clientHeight - screenDockY - dockHeight - shipHeight - clearance;

    // Calculate the most favorable top and left coordinates for the ship to dock at.
    var newShipX; var newShipY;
    if(clearanceRight > -1 || clearanceLeft < clearanceRight) {
	// Dock the ship to the right of the dock.
	if(clearanceRight < 0) {
	    // The ship must abut the right edge of the screen.
	    newShipX = clientWidth + clientXOffset - shipWidth;
	}
	else {
	    newShipX = dockX + dockWidth + clearance;
	}
    } else {
	// Dock the ship to the left of the dock.
	if(clearanceLeft < 0) {
	    // The ship must abut the left edge of the screen.
	    newShipX = 0;
	}
	else {
	    newShipX = dockX - shipWidth - clearance;
	}
    }
    if(clearanceTop > 0 || clearanceTop > clearanceBottom) {
	// Dock the ship above the dock.
	if(clearanceTop < 0) {
	    // The ship must abut the top edge of the screen.
	    newShipY = clientYOffset;
	}
	else {
	    newShipY = dockY - shipHeight - clearance;
	}
    } else {
	// Dock the ship below the dock.
	if(clearanceBottom < 0) {
	    // The ship must abut the bottom edge of the screen.
	    newShipY = clientYOffset + clientHeight - shipHeight;
	}
	else {
	    newShipY = dockY + dockHeight + clearance;
	}
    }

    // Dock the ship.
    ship.css({  
	    left: newShipX + 'px',  
		top: newShipY + 'px'  
		});

}

