// *** utility.txt
function getStyleObject(objectId)
{
   // cross-browser function to get an object's style object given its id
   if(document.getElementById && document.getElementById(objectId))
   {
      return document.getElementById(objectId).style; // W3C DOM
   }
   else if (document.all && document.all(objectId))
   {
      return document.all(objectId).style; // MSIE 4 DOM
   }
   else if (document.layers && document.layers[objectId])
   {
      return document.layers[objectId]; // NN 4 DOM
   }
   else
   {
      return false;
   }
}

function changeObjectVisibility(objectId, newVisibility)
{
   var styleObject = getStyleObject(objectId);
   
   if(styleObject)
   {
      styleObject.visibility = newVisibility;
      return true;
   }
   else
   {
      return false;
   }
}

function moveObject(objectId, newXCoord, newYCoord)
{
   // get a reference to the cross-browser style object and make sure the object exists
   var styleObject = getStyleObject(objectId);
   
   if(styleObject)
   {
      styleObject.left = newXCoord+'px';
      styleObject.top = newYCoord+'px';
      return true;
   }
   else
   {
      return false;
   }
}


var xOffset = 10;
var yOffset = -5;

function showPopup (targetObjectId, eventObj)
{
   if(eventObj)
   {
      hideCurrentPopup(); // hide any currently-visible popups
      eventObj.cancelBubble = true; // stop event from bubbling up any farther
      if (eventObj.stopPropagation) eventObj.stopPropagation();

      // move popup div to current cursor position (add scrollTop to account for scrolling for IE)

      var newXCoord = (eventObj.pageX) ? eventObj.pageX + xOffset : eventObj.x + xOffset + ((document.body.scrollLeft) ? document.body.scrollLeft : 0);
      var newYCoord = (eventObj.pageY) ? eventObj.pageY + yOffset : eventObj.y + yOffset + ((document.body.scrollTop) ? document.body.scrollTop : 0);


      //if (eventObj.clientX) { newXCoord = eventObj.clientX + xOffset;  }
      //else if (eventObj.pageX) newXCoord =  eventObj.pageX + xOffset;
      //////else if (eventObj.x) newXCoord = eventObj.x + xOffset;
      //else return false;


      //if (eventObj.clientY) newYCoord = eventObj.clientY + yOffset;
      //else if (eventObj.pageY) newYCoord =  eventObj.pageY + yOffset;
      //////else if (eventObj.y) newYCoord = eventObj.y + yOffset;
      //else return false;

      moveObject(targetObjectId, newXCoord, newYCoord);
      
      // and make it visible
      if(changeObjectVisibility(targetObjectId, 'visible'))
      { 
         window.currentlyVisiblePopup = targetObjectId; // if we successfully showed the popup store its Id on a globally-accessible object
         return true;
      }
      else
      {
         return false; // we couldn't show the popup, boo hoo!
      }
   }
   else
   {
      return false; // there was no event object, so we won't be able to position anything, so give up
   }
}


function hideCurrentPopup()
{
   if(window.currentlyVisiblePopup)
   {
      changeObjectVisibility(window.currentlyVisiblePopup, 'hidden');
      window.currentlyVisiblePopup = false;
   }
}


// ***********************
// hacks and workarounds *
// ***********************

// initialize hacks whenever the page loads
window.onload = initializeHacks;

// setup an event handler to hide popups for generic clicks on the document
document.onclick = hideCurrentPopup;

function initializeHacks()
{
   // this ugly little hack resizes a blank div to make sure you can click anywhere in the window for Mac MSIE 5
   if ((navigator.appVersion.indexOf('MSIE 5') != -1) && (navigator.platform.indexOf('Mac') != -1) && getStyleObject('blankdiv'))
   {
      window.onresize = explorerMacResizeFix;
   }

   resizeblankdiv();
   
   createFakeEventObj(); // this function creates a placeholder object for older browsers
}

function createFakeEventObj()
{
   // create a fake event object for older browsers to avoid errors in function call when we need to pass the event object to functions
   if (!window.event)
   {
      window.event = false;
   }
}

function resizeblankdiv()
{
    // resize blank placeholder div so IE 5 on mac will get all clicks in window
    if ((navigator.appVersion.indexOf('MSIE 5') != -1) && (navigator.platform.indexOf('Mac') != -1) && getStyleObject('blankdiv'))
    {
	   getStyleObject('blankdiv').width = document.body.clientWidth - 20;
	   getStyleObject('blankdiv').height = document.body.clientHeight - 20;
    }
}

function explorerMacResizeFix()
{
    location.reload(false);
}

