var tooltip = new Tooltip();
	  
if (window.addEventListener) window.addEventListener("load", init, false);
else if (window.attachEvent) window.attachEvent("onload", init);
	  
function init() {	    
  var links = document.getElementsByName('foodPopup');

  // Loop through all the links and add tooltips to them
  for (var i=0; i<links.length; i++) {
    if (links[i].href) addTooltipToLink(links[i]);
    //window.alert(links[i].href);	    
  }
}
	  
function addTooltipToLink(link) {
  // Add event handlers
  if (link.addEventListener) { // Standard way	      
      link.addEventListener("mouseover", showTooltip, false);
      link.addEventListener("mouseout", hideTooltip, false);
  }
  else if (link.attachEvent) { // IE-specific
      link.attachEvent("onmouseover", showTooltip);
      link.attachEvent("onmouseout", hideTooltip);
  }
}
	  
var timer;
	 
function showTooltip(event) {	
    var e = event || window.event; // IE doesn't pass event as argument
    var target = event.target || event.srcElement; // IE doesn't use .target 
    var link = target.toString();
    //window.alert(link);
    var foodPattern = "[a-z_]+.html";
    var foodMatch = link.match(foodPattern).toString();
    // window.alert(foodMatch);
    var food = foodMatch.replace('.html', '');
    // window.alert(food);
    link = "../tooltip/food.xhtml?filename=" + food;
    
    var x = e.clientX + Geometry.getHorizontalScroll() + 25;
    var y = e.clientY + Geometry.getVerticalScroll() + 15;
    
    // If a tool tip is pending, cancel it
    if (timer) window.clearTimeout(timer);
    // Schedule a tool tip to appear after x milliseconds
    timer = window.setTimeout(show, 100); // Must put mouse over link for at least 100 ms to launch show method
    
    function show() {
      HTTP.getText(link, 
	function(tip) {
	  if (timer) { // Only show when mouse is over
	    tooltip.show(tip, x, y);
	  }
		  } );	      
    }
  }
	  
function hideTooltip() {
    if (timer) window.clearTimeout(timer);
    timer = null;
    tooltip.hide();
}