<!--

/************************************************************************/
/* gui_elements.js                                                      */
/*----------------------------------------------------------------------*/
/* Functions that move and display elements in the document in order to */
/* implement the graphical user interface.                              */
/*----------------------------------------------------------------------*/
/* File created 30Apr08 acurtis                                         */
/*----------------------------------------------------------------------*/
/* Copyright (c) 2008-2009 Avaya Inc.                                   */
/************************************************************************/

// Sets the position and size of divId as specified by provided arguments
function MoveAndSizeElem(divId, x, y, height, width)
{
  if( divId )
  {
    var elem = document.getElementById( divId );
    if( elem )
    {
      var the_left = x;
      var the_top = y;
      if (document.layers)
      {
        elem.style.left = the_left;
        elem.style.top = the_top;
        elem.style.width = width;
        elem.style.height = height;
      }
      else
      {
        elem.style.left = the_left + "px";
        elem.style.top = the_top + "px";
        elem.style.width = width + "px";
        elem.style.height = height + "px";
      }
    }
  }
}

// Places the specified element in the top-centre of the screen
function CentreElem( elemId )
{
  var posW;
  var posH;
  var posX;
  var posY;
  if( gBrowser.type == eBT_IE )
  {
    //posW = document.body.clientWidth;
    //posH = document.body.clientHeight;
    posW = document.documentElement.clientWidth;
    posH = document.documentElement.clientHeight;
    posX = document.documentElement.scrollLeft + document.body.scrollLeft;
    posY = document.documentElement.scrollTop  + document.body.scrollTop;
  }
  else
  {
    posW = window.innerWidth;
    posH = window.innerHeight;
    posX = window.scrollX;
    posY = window.scrollY;
  }

  var elem = document.getElementById(elemId);
  var x = posX+(posW-elem.offsetWidth)/2;
  var y = gBrowser.IsTiny()?0:(posY+(posH-elem.offsetHeight)/2);
  if( x < 0 )
  {
    x = 0;
  }
  if( y < 0 )
  {
    y = 0;
  }

  // Ensure it is somewhere in the top part of the screen.
  while( y > 300 )
  {
    y /= 2;
  }


  MoveElem(elemId, x, y);
}

// Sets the position of the specified element as specified by the other arguments
function MoveElem( elemId, x, y, z )
{
  if( elemId )
  {
    var elem = document.getElementById( elemId );
    if( elem )
    {
      var the_left = x;
      var the_top = y;
      if (document.layers)
      {
        elem.style.left = the_left;
        elem.style.top = the_top;
      }
      else
      {
        elem.style.left = the_left + "px";
        elem.style.top = the_top + "px";
      }
      if( z )
      {
        elem.style.zIndex = z;
      }
    }
  }
}

// Gets the x coordinate of the given element
function GetX( oElement )
{
  var iReturnValue = 0;
  while( oElement != null )
  {
    iReturnValue += oElement.offsetLeft;
    oElement = oElement.offsetParent;
  }
  return iReturnValue;
}


// Gets the y coordinate of the given element
function GetY( oElement )
{
  var iReturnValue = 0;
  while( oElement != null )
  {
    iReturnValue += oElement.offsetTop;
    oElement = oElement.offsetParent;
  }
  return iReturnValue;
}

// Sets the style of the cursor to a busy icon, or sets it to default.
function SetBusyCursor( busy )
{
  gBusyCursorCount += busy?1:-1;

  // document.body behaves differently to other elements, so handle it here, outside recursive loop.
  if( gBusyCursorCount == 1 )
  {
    if( document.body )
    {
      SetBusyCursor_i( document.body, busy );
    }
    document.body.style.cursor = "wait";
  }
  else if( gBusyCursorCount == 0 )
  {
    if( document.body )
    {
      SetBusyCursor_i( document.body, busy );
    }
    document.body.style.cursor = "default";
  }
}

// Recursive function used by SetBusyCursor() to traverse the DOM
function SetBusyCursor_i( elem, busy )
{
  var still_to_do = new Array( elem );

  var i = 0;
  while( still_to_do.length > 0 )
  {
    var elem = still_to_do.pop();
    while( elem.childNodes[i] )
    {
      var child = elem.childNodes[i++];
      if( child.style )
      {
        if( busy )
        {
          if( child.style.cursor )
          {
            child.prevcursor = child.style.cursor;
            child.style.cursor = "wait";
          }
        }
        else
        {
          if( child.prevcursor )
          {
            child.style.cursor = child.prevcursor;
            child.prevcursor = null;
          }
        }
      }
      still_to_do.push( child );
    }
  }
}

//-->
