/* Tool Tip Generator */
/* Rob Walker */
/* Requires jquery 1.6.4 */

/* Adds mouseover tooltips to specified elements */

function initTooltips(){
  /*  */
  var kvalueDefinition = 'K Value quantifies head loss. Lower values indicate less loss of pressure.',
      covDefinition = 'Coefficient of Variation quantifies mixing quality. Lower values are better, with a CoV between 0.01 and 0.05 as a typical goal.',
      kValueId = 'k_value_tip',
      covId = 'cov_tip';
  
  /* make the kvalue tooltip*/
  tooltip = makeTooltip (kValueId, kvalueDefinition);
  /* assign the tooptip to popup on each element with class 'k_value' */
  $('.k_value').hover(
	function(){
	  tooltip = $('#'+kValueId);
	  tooltip.pop({'top':$(this).position().top - tooltip.outerHeight() - tooltip.children('.bottom_open').outerHeight(), 'left':($(this).position().left + ($(this).width()/2)) - (tooltip.width()/2)});
	},
	function(){$('#'+kValueId).hide();}
	);
  
  /* make the cov tooltip*/
  tooltip = makeTooltip (covId, covDefinition);
  /* assign the tooptip to popup on each element with class 'cov_value' */
  $('.cov_value, .cov_data').hover(
	function(){
	  tooltip = $('#'+covId);
	  tooltip.pop({'top':$(this).position().top - tooltip.outerHeight() - tooltip.children('.bottom_open').outerHeight(), 'left':($(this).position().left + ($(this).width()/2)) - (tooltip.width()/2)});
	},
	function(){$('#'+covId).hide();}
	);
  return true;
  }

function makeTooltip (htmlId, text) {
  $('#main_content').append (tooltip = $('<p id="'+htmlId+'" class="tooltip image_border" style="position:absolute; display:none; top:0; left:0;">'+text+'</p>')
  .append(
	'<span class="top_open"></span><span class="top_right_open"></span><span class="right_open"></span><span class="bottom_right_open"></span><span class="bottom_open"></span><span class="bottom_left_open"></span><span class="left_open"></span><span class="top_left_open"></span>')
  );

  /* method to position and show the tooltip */
  /* trigger is a 'position' object with properties position.top and position.left */
  jQuery.fn.pop = function(trigger) {
    this.css('top', Math.floor(trigger.top)).css('left', Math.floor(trigger.left));
    this.show();
    return true;
  };
  return tooltip;
}
