// Copyright © 2004 - 2012 Interact Computer Services Ltd.
// All rights reserved. Use is subject to license terms.
// For more information contact Interact at info@interact-uk.com

var AL_ResizeInit       = false;
var AL_Resize_Objects   = new Array ();

var AL_WindowHeight     = 0;
var AL_WindowWidth      = 0;

function resizeObject (obj)
{
  if (obj)
  {
    if (obj.autoResize_Width || obj.autoResize_Height)
    {
      var pobj  = obj.parentNode;
      
      if (!pobj)
      {
        pobj = document.body;
      }
      
      try
      {
        if (obj.autoResize_Width)  obj.style.width  = (((pobj.clientWidth  > 0) ? pobj.clientWidth  : pobj.offsetWidth)  - obj.autoResize_Width)  + "px";
        if (obj.autoResize_Height) obj.style.height = (((pobj.clientHeight > 0) ? pobj.clientHeight : pobj.offsetHeight) - obj.autoResize_Height) + "px";
//      if (obj.autoResize_Width)  obj.style.width  = (((pobj.offsetWidth  > 0) ? pobj.offsetWidth  : pobj.clientWidth)  - obj.autoResize_Width)  + "px";
//      if (obj.autoResize_Height) obj.style.height = (((pobj.offsetHeight > 0) ? pobj.offsetHeight : pobj.clientHeight) - obj.autoResize_Height) + "px";
      }
      
      catch (e)
      {
      }
    }
  }
}

function windowResized (e)
{
  getWindowHeight ();
  getWindowWidth  ();
  
  if (AL_ProcessAction)
  {
    AL_ProcessAction ("WindowResized");
  }
  
  var obj = null;
  var idx = -1;
  
  while (++idx < AL_Resize_Objects.length)
  {
    obj = AL_Resize_Objects[idx];

    if (obj.object)
    {
      obj = obj.object;
    }
    
    resizeObject (obj);

    obj = AL_Resize_Objects[idx];
    
    if (obj.windowResized) obj.windowResized ();
    if (obj.notifyResize)  obj.notifyResize  ();
  }

  if (AL_ProcessAction)
  {
    AL_ProcessAction ("WindowResizedComplete");
  }
}

function addResizeListener (obj)
{
  if (!AL_ResizeInit)
  {
    window.onresize = windowResized;
    AL_ResizeInit   = true;
  }
  
  var idx   = AL_Resize_Objects.length;
  var doit  = true;
  
  while (idx-- > 0)
  {
    if (AL_Resize_Objects[idx] == obj)
    {
      doit = false;
    }
  }
  
  if (doit)
  {
    AL_Resize_Objects.push (obj);
  }
}

function remResizeListener (obj)
{
  if (AL_ResizeInit)
  {
    var idx = AL_Resize_Objects.length;
    
    while (idx-- > 0)
    {
      if (AL_Resize_Objects[idx] == obj)
      {
        AL_Resize_Objects.splice (idx, 1); idx = 0;
      }
    }
  }
}

function getWindowWidth ()
{
  var width = 0;
  
  if (window.innerWidth)
  {
    width = window.innerWidth;
  }
  else
  {
    if (document.documentElement) if (document.documentElement.clientWidth)
    {
      width = document.documentElement.clientWidth;
    }
    
    if (width == 0)
    {
      if (document.body.clientWidth)
      {
        width = document.body.clientWidth;
      }
    }
  }
  
  AL_WindowWidth = width;
  
  return (width);
}

function getWindowHeight ()
{
  var height  = 0;
  
  if (window.innerHeight)
  {
    height = window.innerHeight;
  }
  else
  {
    if (document.documentElement) if (document.documentElement.clientHeight)
    {
      height = document.documentElement.clientHeight;
    }
    
    if (height == 0)
    {
      if (document.body.clientHeight)
      {
        height = document.body.clientHeight;
      }
    }
  }
  
  AL_WindowHeight = height;
  
  return (height);
}

