var Popup = {
  popupMenu: function( event, menuId )
  {
    var menu=document.getElementById(menuId);
    menu.style.position='absolute';
    menu.style.display='block';
    var x = (event.clientX + document.body.scrollLeft) - 5;
    var y = (event.clientY + document.body.scrollTop) - 5;
    if ( x + menu.offsetWidth > document.body.clientWidth )
      x = document.body.clientWidth - menu.offsetWidth - 2;
    if ( event.clientY + menu.offsetHeight > document.body.clientHeight )
      y = document.body.clientHeight + document.body.scrollTop - menu.offsetHeight - 2;
    menu.style.width = menu.offsetWidth + "px";
    menu.style.left= x + "px";
    menu.style.top= y + "px";
  },

  openUnder: function( menuId, buttonId, maxheight )
  {
    $(menuId).style.position='absolute';
    var buttonPosition = Popup.elementPosition($(buttonId));
    $(menuId).style.left = buttonPosition.x + 'px';
    $(menuId).style.top = (buttonPosition.y + $(buttonId).offsetHeight) + 'px';
    $(menuId).style.width = $(buttonId).offsetWidth + 'px';
    Element.show(menuId)
    if ( maxheight )
    {
      if ( $(menuId).scrollHeight > maxheight )
        $(menuId).style.height = maxheight + 'px';
      else
        $(menuId).style.height = 'auto';
    }
  },

  hideOnMouseout: function(event,element)
  {
    if ( Popup.isMouseOutForThisDiv(event,element) )
      Element.hide(element);
  },

  isMouseOutForThisDiv: function(event, element)
  {
    if (!event) var event = window.event;
    var tg = $(element);
    return Popup.outside(tg, event);
  },

  elementPosition: function( element )
  {
    var e = $(element);
    var x = 0;
    var y = 0;

    while (e.offsetParent) {
      x += e.offsetLeft + (e.clientLeft || 0);
      y += e.offsetTop + (e.clientTop || 0);
      e = e.offsetParent;
    }
    return {x: x, y: y};
  },

  isDescendant: function( child, parent )
  {
    while ( child )
    {
      if ( child == parent )
        return true;
      child = child.parentNode;
    }
    return false;
  },

  bounds: function( element )
  {
    var e = $(element);
    var pos = Popup.elementPosition( e );
    return { x:pos.x, y:pos.y, width:e.offsetWidth, height:e.offsetHeight };
  },

  outside: function( tg, event )
  {
    var mouseX = event.clientX + document.body.scrollLeft;
    var mouseY = event.clientY + document.body.scrollTop;
    var position = Popup.elementPosition( tg );
    return (( mouseX <= position.x )
      || ( mouseX >= position.x + tg.offsetWidth )
      || ( mouseY <= position.y )
      || ( mouseY >= position.y + tg.offsetHeight ));
  },

  intersects: function( element1, element2 )
  {
    var bounds1 = Popup.bounds( element1 );
    var bounds2 = Popup.bounds( element2 );
    return (( bounds1.y + bounds1.height >= bounds2.y )
        && ( bounds1.y <= bounds2.y + bounds2.height )
        && ( bounds1.x + bounds1.width >= bounds2.x )
        && ( bounds1.x <= bounds2.x + bounds2.width ));
  },

  hideSelects: function( form, block )
  {
    var elements = $(form).elements;
    for ( var i=0; i < elements.length; i++ )
    {
      if (( elements[i].nodeName == 'SELECT' ) && ( Popup.intersects( elements[i], block ) ))
        Element.hide(elements[i]);
    }
  },

  restoreSelects: function( form )
  {
    var elements = $(form).elements;
    for ( var i=0; i < elements.length; i++ )
      if ( elements[i].nodeName == 'SELECT' )
        Element.show(elements[i]);
  },

  keepVisibleInDocument: function( pElement, top )
  {
    var element = $(pElement);
    var container = document.body;
    var windowHeight = window.offsetHeight ? window.offsetHeight : window.innerHeight;
    if (( Popup.keepTopVisible(element, container )) && ( top ))
      return;
    Popup.keepBottomVisible( element, container, windowHeight );
  },

  keepBottomVisible: function( element, container, containerHeight )
  {
    var offset = Popup.offsetFromContainer(element, container);
    if ( offset + element.offsetHeight > containerHeight + container.scrollTop )
    {
      container.scrollTop = offset + element.offsetHeight - containerHeight;
      return true;
    }
    return false;
  },

  keepTopVisible: function( element, container )
  {
    var offset = Popup.offsetFromContainer(element, container);
    if ( offset < container.scrollTop )
    {
      container.scrollTop = offset;
      return true;
    }
    return false;
  },

  offsetFromContainer: function( element, container )
  {
      var y = 0;
      while ((element.offsetParent) && ( element != container )) {
        y += element.offsetTop + (element.clientTop || 0);
        element = element.offsetParent;
      }
      return y;
  }
}
