/*
** Dynamic Popup Support
*/

//---------------------------------------------------------------
// Constants
//---------------------------------------------------------------

//---------------------------------------------------------------
// Globals
//---------------------------------------------------------------

/*
 * This variable stores the current active popup when only 
 * allowing a single popup to be available at any one time
 */
var gActivePopup = null;

//---------------------------------------------------------------
// Functions
//---------------------------------------------------------------

/*
 * Returns true if the popup is currently being displayed
 */
function awpxIsPopupOpen(popup)
{
  var popupEl = awpxGetElement(popup);
  if(popupEl) {
      return (popupEl.style.display == "block");
  }
  return false;
}

/*
 * Performs a toggle of the state of the popup.
 */
function awpxPopupToggleAbsolute(popup, left, top, onClose)
{
  var popupEl = awpxGetElement(popup);
  if(awpxIsPopupOpen(popupEl))
  {
    awpxPopupClose(popupEl);
  } else {
    awpxPopupOpenAbsolute(popupEl, left, top, onClose);
  }
}

/*
 * Performs a toggle of the state of the popup.
 */
function awpxPopupToggle(popup, element, left, top, onClose)
{
  var popupEl = awpxGetElement(popup);
  if(awpxIsPopupOpen(popupEl))
  {
    awpxPopupClose(popupEl);
  } else {
    awpxPopupOpen(popupEl, element, left, top, onClose);
  }
}

/*
 * Opens the specified popup and positions it given the
 * specific absolute position
 */
function awpxPopupOpenAbsolute(popup, left, top, onClose)
{
  /*
   * We first check to see if there is another popup
   * which is active.  If so, we try to close it before
   * we bring this one up.  If it doesn't close, then
   * we just quit.
   */
  if(gActivePopup) 
  {
    if(!awpxPopupClose(gActivePopup))
    {
      return;
    }
    gActivePopup = null;
  }

  var popupEl = awpxGetElement(popup);

  /* Position Popup */
  popupEl.style.position = "absolute";
  popupEl.style.left = left + "px";
  popupEl.style.top  = top + "px";

  /* Render Popup */
  popupEl.style.display  = "block";

  /* Store onClose function */
  if(onClose) {
     popupEl.onClose = onClose;
  }

  /* Mark this as the active popup */
  gActivePopup = popupEl;
}

/*
 * Opens the specified popup and positions it relative
 * to the given element.
 * onClose: An optional javascript function that should
 *          run prior to allowing the popup to close
 */ 
function awpxPopupOpen(popup, element, left, top, onClose)
{
  var el = awpxGetElement(element);

  var absLeft = awpxAbsoluteLeft(el) + left;
  var absTop  = awpxAbsoluteTop(el) + top;

  awpxPopupOpenAbsolute(popup, absLeft, absTop, onClose);
}

/*
 * Closes the specified popup.  If there is an onClose function
 * registered on the popup, it will be executed first.  If it
 * returns false, then this will return false and the popup
 * will not close.
 * If noReturn is true, then no return value will be used.  This
 * is useful if this function is being used as a Javascript: href.
 */
function awpxPopupClose(popup, noReturn)
{
    var shouldClose = true;

    var popupEl = awpxGetElement(popup);
    if(popupEl.onClose)
    {
        shouldClose = popupEl.onClose();
    }

    if(shouldClose)
    {
        popupEl.style.display = "none";
        gActivePopup = null;
    }

    if(!noReturn) return shouldClose;
}
