﻿// Developed by Mike Kiska 6/23/2006

function FindPosY( ctl )
{
  var totaloffset = ctl.offsetTop;
  var parentCtl = ctl.offsetParent;
  while ( parentCtl != null )
  {
    totaloffset = totaloffset + parentCtl.offsetTop;
    parentCtl = parentCtl.offsetParent;
  }
  return totaloffset;
}

function FindPosX( ctl )
{
  var totaloffset = ctl.offsetLeft;
  var parentCtl = ctl.offsetParent;
  while ( parentCtl != null )
  {
    totaloffset = totaloffset + parentCtl.offsetLeft;
    parentCtl = parentCtl.offsetParent;
  }
  return totaloffset;
}

function ControlContainsPoint(ctl, x, y)
{
  var top = FindPosY(ctl);
  var left = FindPosX(ctl);

  if ( x >= left && x < left + ctl.offsetWidth && y >= top && y < top + ctl.offsetHeight )
    return true;
  else
    return false;
}

function PositionControlCenterScreen( ctl, offsetX, offsetY )
{
  if ( ctl != null )
  {  
    ctl.style.position = 'absolute';

    var addToLeft = 0;
    if ( offsetX == undefined ) {} else
      addToLeft = offsetX
    
    var addToTop = 0;
    if ( offsetY == undefined ) {} else
      addToTop = offsetY;

    ctl.style.left = ((document.documentElement.offsetWidth / 2) - (ctl.offsetWidth / 2) + document.documentElement.scrollLeft + addToLeft) + 'px';
    ctl.style.top = ((document.documentElement.offsetHeight / 2) - (ctl.offsetHeight / 2) + document.documentElement.scrollTop + addToTop) + 'px';
  }
}

function AttachControlToControlX(ctl, ctlBase, sideX, offset)
{
  if ( offset == undefined )
    offset = 0;

  var x = FindPosX(ctlBase);
  
  if ( sideX == 'left' )
    ctl.style.left = (x + offset) + 'px';
  else if ( sideX == 'right' )
    ctl.style.left = (x - parseInt(ctl.style.width) + ctlBase.offsetWidth + offset) + 'px';
};

function AttachControlToControlY(ctl, ctlBase, sideY, offset)
{
  if ( offset == undefined )
    offset = 0;

  var y = FindPosY(ctlBase);
  if ( sideY == 'top' )
    ctl.style.top = (y - ctl.offsetHeight + offset) + 'px';
  else if ( sideY == 'bottom' )
    ctl.style.top = (y + ctlBase.offsetHeight + offset) + 'px';
}

function IsControlInDocument(ctl)
{
  var left = FindPosX(ctl);
  var top = FindPosY(ctl);
  
  if ( top >= 0 && top + ctl.offsetHeight <= document.documentElement.offsetHeight )
    if ( left >= 0 && left + ctl.offsetWidth <= document.documentElement.offsetWidth )
      return true;
      
  return false;
}

function IsControlInWindow(ctl)
{
  var ctlLeft = FindPosX(ctl);
  var ctlTop = FindPosY(ctl);
  
  var width = screen.availWidth;
  var height = screen.availHeight;
  
  var startLeft = document.documentElement.scrollLeft;
  var startTop = document.documentElement.scrollTop;
  
  if ( ctlLeft >= startLeft && ctlLeft <= width - startLeft )
    if ( ctlTop >= startTop && ctlTop <= height - startTop )
      return true;
      
  return false;
}

function AutoSize(div, maxWidth, maxHeight)
{
  div.style.height = '0px';

  var originalOverflow = div.style.overflow;
  div.style.overflow = 'auto';

  if ( maxWidth == undefined )
    maxWidth = screen.availWidth / 3;
  if ( maxHeight == undefined )
    maxHeight = screen.availHeight / 8.5;
      
  div.style.width = maxWidth + 'px';
  
  if ( div.scrollHeight <= maxHeight )
    div.style.height = div.scrollHeight + 'px';
  else
    div.style.height = maxHeight + 'px';

  div.style.width = '0px';

  while ( div.offsetHeight < div.scrollHeight && div.offsetWidth < maxWidth )
    div.style.width = (div.offsetWidth + 1) + 'px';

  while ( div.offsetHeight == div.scrollHeight )
    div.style.width = (div.offsetWidth - 1) + 'px';

  div.style.width = (div.offsetWidth + 1) + 'px';

  if ( originalOverflow != 'auto' )
    div.style.overflow = originalOverflow;
}