// ************************************************
// This is a modified version of popup.txt originally provided by Apple.
// Due to the fact that I have modified it I am not allowed to redistribute it as "Apple sample code".
// For the original version, see http://www.krugle.org/examples/p-G6dzJGlp5NeYdBj4/popup.txt
// As far as I can tell the Apple license of the original does not require me to include the original license or copyright notification.
// I am not placing any sort of license, copyright or attribution requirement on this version, though you might want to be nice to Apple for the original.
// ************************************************

// store variables to control where the popup will appear relative to the cursor position
// positive numbers are below and to the right of the cursor, negative numbers are above and to the left
var x_offset = -150;
var y_offset = -150;

// ***********************
// Event handlers
// ***********************

// setup an event handler to hide popups for generic clicks on the document
//
// uncomment this if you want this functionality enabled
// document.onclick = hideFootnote;

function showPopup(footnote_id, event) {
    
    if(event) {
	  // hide any currently-visible popups
	  hideFootnote();
	  // stop event from bubbling up any farther
	  event.cancelBubble = true;
	  // move popup div to current cursor position
	  // (add scrollTop to account for scrolling for IE)
	
	  var event_x = 0;
	  var event_y = 0;
	  if (event.pageX || event.pageY) {
		event_x = event.pageX;
		event_y = event.pageY;
	  } else if (event.clientX || event.clientY) {
		event_x = event.clientX + document.body.scrollLeft; //+ document.documentElement.scrollLeft;
		event_y = event.clientY + document.body.scrollTop; // + document.documentElement.scrollTop;
	  }
	  // event_x and event_y contain the mouse position relative to the document
	
	  var newXCoordinate = event_x + x_offset;
	  var newYCoordinate = event_y + y_offset;
	
	  moveObject(footnote_id, newXCoordinate, newYCoordinate);
	  // and make it visible
	  
	  if( changeObjectVisibility(footnote_id, 'visible') ) {
	    // if we successfully showed the popup
	    // store its Id on a globally-accessible object
	    window.currentlyVisiblePopup = footnote_id;
	    
	    var popup = document.getElementById(footnote_id);
	    if (popup) {
			 popup.onclick = hideFootnote;
	    }
	    
	    return true;
	  } else {
	    // we couldn't show the popup, boo hoo!
	    return false;
	  }
    } else {
	  // there was no event object, so we won't be able to position anything, so give up
	  return false;
    }
}

function hideFootnote() {
    // note: we've stored the currently-visible popup on the global object window.currentlyVisiblePopup
    if(window.currentlyVisiblePopup) {
	  changeObjectVisibility(window.currentlyVisiblePopup, 'hidden');
	  window.currentlyVisiblePopup = false;
    }
}

function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
	  // W3C DOM
	  return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	  // MSIE 4 DOM
	  return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	  // NN 4 DOM.. note: this won't find nested layers
	  return document.layers[objectId];
    } else {
	  return false;
    }
}

function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	  styleObject.visibility = newVisibility;
	  return true;
    } else {
	  // we couldn't find the object, so we can't change its visibility
	  return false;
    }
}

function moveObject(objectId, newXCoordinate, newYCoordinate) {    
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
      styleObject.left = newXCoordinate + "px";
	styleObject.top = newYCoordinate + "px";
	return true;
    } else {
	  // we couldn't find the object, so we can't very well move it
	  return false;
    }
}
