// JavaScript Document

var TWX_RelDivCounter = 0;
var TWX_RelDivs = new Array();
var TWX_RelDivsEnable = true;
var TWX_RelDivTimerControl = true;

var TWX_RelDiv_CallBack_Changed = "";

function TWX_CRelativeDiv(name_div_parent, name_div, offset_x, offset_y, from_bottom, from_right)
{
   this.type = 1;
   this.control = 1;
   this.npar = name_div_parent;
   this.ndiv = name_div;
   this.ox = offset_x;
   this.oy = offset_y;
   this.fb = from_bottom;
   this.fr = from_right;
   this.lastx = -1;
   this.lasty = -1;
   this.lasth = -1;
   this.lastw = -1;      
}

function TWX_CRelativeIndDiv(name_div_parent, name_div, offset_x, offset_y, from_bottom, from_right)
{
   this.type = 1;
   this.control = 0;
   this.npar = name_div_parent;
   this.ndiv = name_div;
   this.ox = offset_x;
   this.oy = offset_y;
   this.fb = from_bottom;
   this.fr = from_right;
   this.lastx = -1;
   this.lasty = -1;   
   this.lasth = -1;
   this.lastw = -1;      
}

function TWX_CAnchorDiv(name_div_parent, name_div, offset_y, from_bottom)
{
   this.type = 2;
   this.control = 1;
   this.npar = name_div_parent;
   this.ndiv = name_div;
   this.oy = offset_y;
   this.fb = from_bottom;
   this.lastx = -1;
   this.lasty = -1;   
   this.lasth = -1;
   this.lastw = -1;      
}

function TWX_CSizeDiv(name_div, max_h)
{
   this.type = 3;
   this.control = 1;
   this.ndiv = name_div;
   this.max_h = max_h; 
   this.lastx = -1;
   this.lasty = -1;   
   this.lasth = -1;
   this.lastw = -1;      
}

function TWX_AddAnchorDiv(name_div_parent, name_div, offset_y, from_bottom)
{
    for(i = 0; i < TWX_RelDivs.length; i++)
        if(TWX_RelDivs[i].ndiv == name_div && TWX_RelDivs[i].type == 2)
           return; 

    TWX_RelDivs[TWX_RelDivCounter] = new TWX_CAnchorDiv(name_div_parent, name_div, offset_y, from_bottom);
    TWX_RelDivCounter++;

    return TWX_RelDivCounter - 1;
}


function TWX_AddSizeDiv(name_div, max_h)
{
    for(i = 0; i < TWX_RelDivs.length; i++)
        if(TWX_RelDivs[i].ndiv == name_div && TWX_RelDivs[i].type == 3)
           return; 

    TWX_RelDivs[TWX_RelDivCounter] = new TWX_CSizeDiv(name_div, max_h);
    TWX_RelDivCounter++;

    return TWX_RelDivCounter - 1;
}

function TWX_AddRelDiv(name_div_parent, name_div, offset_x, offset_y, from_bottom, from_right)
{
    for(i = 0; i < TWX_RelDivs.length; i++)
        if(TWX_RelDivs[i].ndiv == name_div && TWX_RelDivs[i].type == 1)
           return; 

    TWX_RelDivs[TWX_RelDivCounter] = new TWX_CRelativeDiv(name_div_parent, name_div, offset_x, offset_y, from_bottom, from_right);
    TWX_RelDivCounter++;

    return TWX_RelDivCounter - 1;
}

function TWX_AddRelIndDiv(name_div_parent, name_div, offset_x, offset_y, from_bottom, from_right)
{
    for(i = 0; i < TWX_RelDivs.length; i++)
        if(TWX_RelDivs[i].ndiv == name_div && TWX_RelDivs[i].type == 1)
           return; 

    TWX_RelDivs[TWX_RelDivCounter] = new TWX_CRelativeIndDiv(name_div_parent, name_div, offset_x, offset_y, from_bottom, from_right);
    TWX_RelDivCounter++;

    return TWX_RelDivCounter - 1;
}

function TWX_ModifyRelDiv(name_div_parent, name_div, offset_x, offset_y, from_bottom, from_right)
{
    for(i = 0; i < TWX_RelDivs.length; i++)
        if(TWX_RelDivs[i].ndiv == name_div)
        {          
           TWX_RelDivs[i].npar = name_div_parent;
           TWX_RelDivs[i].ndiv = name_div;
           TWX_RelDivs[i].ox = offset_x;
           TWX_RelDivs[i].oy = offset_y;        
           TWX_RelDivs[i].fb = from_bottom;
           TWX_RelDivs[i].fr = from_right;           
           break;
        }
}

function TWX_RelDivControl()
{
   TWX_OnRelDivResizeWindow();
}

function TWX_OnRelDiv(index, force)
{
      var RelDiv = TWX_RelDivs[index];
      var obj = null;
      
      if(RelDiv.type == 1 || RelDiv.type == 2)                
         obj = document.getElementById(RelDiv.npar);
      var div = document.getElementById(RelDiv.ndiv);

      var changed = false;
      
      if(div)
      {     
         var y = 0;
         var x = 0;
         var h = 0;
         var w = 0;    
         if(obj != null)
         {
           y = DOM_FindPosY(obj);
           x = DOM_FindPosX(obj);
           h = obj.offsetHeight;
           w = obj.offsetWidth;
           if(!force)
               if(RelDiv.lastx == x && RelDiv.lasty == y && RelDiv.lasth == h && RelDiv.lastw == w)
                  return changed;          
         }                        
          
         if(RelDiv.type == 1)
         {
             if(RelDiv.fb)
                DOM_SetPosTop(div, y + h + RelDiv.oy);
             else
                DOM_SetPosTop(div, y + RelDiv.oy);
             
             if(RelDiv.fr)
                DOM_SetPosLeft(div, x + w + RelDiv.ox);
             else
                DOM_SetPosLeft(div, x + RelDiv.ox);
                
             changed = true;
         }
         
         if(RelDiv.type == 2)
         {         
             if(RelDiv.fb)
                DOM_SetHeight(div, (y + h - DOM_FindPosY(div)) + RelDiv.oy);
             else
                DOM_SetHeight(div, (y - DOM_FindPosY(div)) + RelDiv.oy);          
             
             changed = true;   
         }
              
         if(RelDiv.type == 3)
         {      
            var h = div.offsetHeight;
            var nh;
                        
            if(document.body.clientHeight < RelDiv.max_h)         
               nh = document.body.clientHeight;
            else
               nh = RelDiv.max_h;
               
            if(h != nh)
            {
               DOM_SetHeight(div, nh);
               changed = true;
            }
         }
         if(RelDiv.type == 4)
         {      
            var h = div.offsetHeight;
            var top = DOM_FindPosY(div);
            var bot = top + h;
            var nh;
                        
            nh = h + (document.body.clientHeight - bot);             
            if(nh > 10)
            {
               DOM_SetHeight(div, nh);
               changed = true;
            }
         }
         
         if(obj != null)
         {       
           RelDiv.lastx = x;
           RelDiv.lasty = y;
           RelDiv.lasth = h;
           RelDiv.lastw = w;
         }         
      }
      
      return changed;
}
      
function TWX_OnRelDivResizeWindow()
{
   if(!TWX_RelDivsEnable)
       return;
  
   TWX_RelDivsEnable = false;

   if (window.TWX_OnRelDivTimer) 
       clearTimeout(window.TWX_OnRelDivTimer);

   var changed = false;

   for(i = 0; i < TWX_RelDivs.length; i++)
       if(TWX_RelDivs[i].control == 1)
          changed |= TWX_OnRelDiv(i);
  
   if(changed)
   {
      if(TWX_RelDiv_CallBack_Changed != "")
         eval(TWX_RelDiv_CallBack_Changed);
   }
   
   if(TWX_RelDivTimerControl)
   {
      if(TWX_RelDivs.length > 0)
          window.TWX_OnRelDivTimer = setTimeout('TWX_OnRelDivResizeWindow()', (changed == true ? 50 : 1000));
   }   
   
   TWX_RelDivsEnable = true;
}

