// Base JavaScript code for Vis.Asp.
//
// Written by OrdinaSoft, Patrick Lanz, Lausanne
// First version: 3rd May 2005


// Returns the position of the given Elmt, in the current window.
// The returning object will have "x" and "y" properties

function GetElementPos (Elmt) {

  var
    Pos = new Object ();

  Pos.x = 0;
  Pos.y = 0;
  while (Elmt != null) {
    Pos.x += Elmt.offsetLeft;
    Pos.y += Elmt.offsetTop;
    Elmt = Elmt.offsetParent;
  }
  return Pos;
}


// Returns the viewport size.

function GetViewportSize () {

  var
    Size = new Object ();

  if (typeof window.innerWidth != 'undefined') {
    Size.cx = window.innerWidth;
    Size.cy = window.innerHeight;
  }
  else {
    var
      Elmt;
    if ((typeof document.documentElement != 'undefined') &&
        (typeof document.documentElement.clientWidth != 'undefined') &&
        (document.documentElement.clientWidth != 0))
      Elmt = document.documentElement;
    else Elmt = document.body;
    Size.cx = Elmt.clientWidth;
    Size.cy = Elmt.clientHeight;
  }
  return Size;
}


// Returns the scrolling position.

function GetScrollPosition () {

  var
    Pos = new Object ();

  if (typeof window.pageYOffset != 'undefined') {
    Pos.x = window.pageXOffset;
    Pos.y = window.pageYOffset;
  }
  else if ((typeof document.documentElement.scrollTop != 'undefined') &&
           (document.documentElement.scrollTop > 0)) {
    Pos.x = document.documentElement.scrollLeft;
    Pos.y = document.documentElement.scrollTop;
  }
  else if (typeof document.body.scrollTop != 'undefined') {
    Pos.x = document.body.scrollLeft;
    Pos.y = document.body.scrollTop;
  }
  else {
    Pos.x = 0;
    Pos.y = 0;
  }
  return Pos;
}


// Makes an absolute-positionned element always visible.

function MakeAbsElmtVisible (Elmt, PosX, PosY) {

  var
    Style = Elmt.style;
    WndSize = GetViewportSize (),
    ScrollPos = GetScrollPosition ();

  PosX = Math.max (Math.min (PosX - ScrollPos.x, WndSize.cx - Elmt.offsetWidth - 1), 1) +
         ScrollPos.x;
  Style.left = PosX.toString () + 'px';

  PosY = Math.max (Math.min (PosY - ScrollPos.y, WndSize.cy - Elmt.offsetHeight - 1), 1) +
         ScrollPos.y
  Style.top =  PosY.toString () + 'px';
}