function getPageSizeWithScroll(){ 
  if (window.innerHeight && window.scrollMaxY) {// Firefox 
    yWithScroll = window.innerHeight + window.scrollMaxY; 
    xWithScroll = window.innerWidth + window.scrollMaxX; 
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac 
    yWithScroll = document.body.scrollHeight; 
    xWithScroll = document.body.scrollWidth; 
  } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari 
    yWithScroll = document.body.offsetHeight; 
    xWithScroll = document.body.offsetWidth; 
  } 
  arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll); 
  //alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll ); 
  return arrayPageSizeWithScroll; 
} 
    
function GetPopupAnchor(popupElement, nextToElement, offsetFromElement, marginFromWindowEdges)
{
  var cumulativeOffset = Position.cumulativeOffset(nextToElement);
  
  var popupDimensions = Element.getDimensions(popupElement);
  
  var wholePageSize = getPageSizeWithScroll();
  
  var windowWidth = wholePageSize[0];
   
  var x = ((cumulativeOffset[0] + popupDimensions.width + offsetFromElement.x) > windowWidth) ? (windowWidth - popupDimensions.width - marginFromWindowEdges.x) : cumulativeOffset[0] + offsetFromElement.x;
  
  var windowHeight = wholePageSize[1];
  
  var y = ((cumulativeOffset[1] + popupDimensions.height + offsetFromElement.y) > windowHeight) ? (windowHeight - popupDimensions.height - marginFromWindowEdges.y) : cumulativeOffset[1] + offsetFromElement.y;
  
  return {x: x, y: y};
}
