var TWX_FlyMenuCounter = 0;
var TWX_FlyMenus = new Array();

function TWX_CFlyMenu(fm_halign, fm_valign, fm_obj_name, fm_x, fm_y, fm_w, fm_h, fm_speed, pos_fn)
{
   this.Set(fm_halign, fm_valign, fm_obj_name, fm_x, fm_y, fm_w, fm_h, fm_speed, pos_fn);
}

TWX_CFlyMenu.prototype.Set = function(fm_halign, fm_valign, fm_obj_name, fm_x, fm_y, fm_w, fm_h, fm_speed, pos_fn)
{
   this.reset = true;
   
   this.obj_par_name = null;
   this.offset_x = 0;
   this.offset_y = 0;
   
   this.lastX = -1;
   this.lastY = -1; 
   this.lastW = -1;
   this.lastH = -1; 

   this.halign = fm_halign;
   this.valign = fm_valign;
   this.obj_name = fm_obj_name;
   this.x = fm_x;
   this.y = fm_y;
   this.w = fm_w;
   this.h = fm_h;
   this.speed = fm_speed;
   
   this.pos_function = pos_fn;
   this.flymenu_adj = null;
  
   if(document.body)
      this.Reset();
       
   this.stop = false;
}

TWX_CFlyMenu.prototype.Reset = function()
{
     var FlyObj = document.getElementById(this.obj_name);
 
     if(this.w == 0 && FlyObj)
        this.w = FlyObj.offsetWidth;
     if(this.h == 0 && FlyObj)
        this.h = FlyObj.offsetHeight;
       
     this.Recalc(); 
     
     if(this.x < 1)
        this.x = 1;
     if(this.y < 1)
        this.y = 1; 
           
     this.lastX = DOM_FindPosX(FlyObj); 
     this.lastY = DOM_FindPosY(FlyObj);        
}

TWX_CFlyMenu.prototype.Recalc = function()
{
  
     if (this.halign == "left")   { this.x = this.x };
     if (this.halign == "right")  { this.x = document.body.offsetWidth - this.x - this.w}; 
     if (this.halign == "center") { this.x = Math.round((document.body.clientWidth ) / 2) - Math.round(this.w / 2)};
     if (this.valign == "top")    { this.y = this.y };
     if (this.valign == "bottom") { this.y = document.body.offsetHeight - this.y - this.h };  
     if (this.valign == "center") { this.y = Math.round((document.body.clientHeight ) / 2) - Math.round(this.h / 2)};  
}

function TWX_FlyMenuAdd(fm_halign, fm_valign, fm_obj_name, fm_x, fm_y, fm_w, fm_h, fm_speed, pos_fn)
{
    for(i = 0; i < TWX_FlyMenus.length; i++)
        if(TWX_FlyMenus[i].obj_name == fm_obj_name)
           return i; 

    TWX_FlyMenus[TWX_FlyMenuCounter] = new TWX_CFlyMenu(fm_halign, fm_valign, fm_obj_name, fm_x, fm_y, fm_w, fm_h, fm_speed, pos_fn);
   
    TWX_FlyMenuCounter++;
    
    return TWX_FlyMenuCounter - 1;
}

TWX_CFlyMenu.prototype.SetRel = function(fm_obj_par_name, fm_offset_x, fm_offset_y)
{
   this.obj_par_name = fm_obj_par_name;
   this.offset_x = fm_offset_x;
   this.offset_y = fm_offset_y;
}

function TWX_FlyMenuAddRel(fm_halign, fm_valign, fm_obj_name, fm_x, fm_y, fm_w, fm_h, fm_speed, pos_fn, fm_obj_par_name, fm_offset_x, fm_offset_y)
{
    for(i = 0; i < TWX_FlyMenus.length; i++)
        if(TWX_FlyMenus[i].obj_name == fm_obj_name)
           return i; 

    TWX_FlyMenus[TWX_FlyMenuCounter] = new TWX_CFlyMenu(fm_halign, fm_valign, fm_obj_name, fm_x, fm_y, fm_w, fm_h, fm_speed, pos_fn);
    TWX_FlyMenus[TWX_FlyMenuCounter].SetRel(fm_obj_par_name, fm_offset_x, fm_offset_y);
    
   
    TWX_FlyMenuCounter++;
    
    return TWX_FlyMenuCounter - 1;
}

function TWX_FlyMenuStop(FlyMenuIndex)
{
   var FlyMenu = TWX_FlyMenus[FlyMenuIndex];
   FlyMenu.stop = true;
}

function TWX_FlyMenuAdjustAll() 
{
   if(TWX_FlyMenus)
      for(i = 0; i < TWX_FlyMenus.length; i++)
          TWX_FlyMenuAdjust(i); 
}

function TWX_FlyMenuAdjust(FlyMenuIndex) 
{
   var FlyMenu = TWX_FlyMenus[FlyMenuIndex];
   var FlyObj = document.getElementById(FlyMenu.obj_name);
   
   if (FlyMenu.flymenu_adj) 
   {
       window.clearTimeout(FlyMenu.flymenu_adj);
       FlyMenu.flymenu_adj = null;
       if (FlyMenu.stop)
       { 
           FlyMenu.stop = false;
           return;
       }
   }  

   if(FlyMenu.lastW != document.body.clientWidth) 
   {
      FlyMenu.lastW = document.body.clientWidth;  
      FlyMenu.Recalc();  
      FlyMenu.lastX = DOM_FindPosX(FlyObj); 
   }
   if(FlyMenu.lastH != document.body.clientHeight) 
   {
      FlyMenu.lastH = document.body.clientHeight;  
      FlyMenu.Recalc();   
      FlyMenu.lastY = DOM_FindPosY(FlyObj);       
   }

   if(FlyMenu.reset) 
   {
      FlyMenu.Reset();
      FlyMenu.reset = false;    
   }  
   
   var ox = FlyMenu.x + FlyMenu.offset_x; 
   var oy = FlyMenu.y + FlyMenu.offset_y; 
        
   if(FlyMenu.obj_par_name)
   {
       var FlyObjRel = document.getElementById(FlyMenu.obj_par_name);
       if(FlyObjRel)
       {
          ox = DOM_FindPosX(FlyObjRel) + FlyMenu.offset_x; 
          oy = DOM_FindPosY(FlyObjRel) + FlyMenu.offset_y;
       }
   }
  
  var timeout = 500;
  
  if (FlyMenu.speed==0)
  {
     if(ox < 1)
        ox = 1;
     if(oy < 1)
        oy = 1;
                      
     FlyMenu.lastX = document.body.scrollLeft + ox;
     FlyMenu.lastY = document.body.scrollTop + oy;
     
     if(FlyMenu.halign != '')
        DOM_SetPosLeft(FlyObj, FlyMenu.lastX);
     
     if(FlyMenu.valign != '')   
        DOM_SetPosTop(FlyObj, FlyMenu.lastY);           
  }
  else
  {
     var dx = Math.abs(document.body.scrollLeft + ox - FlyMenu.lastX);
     var dy = Math.abs(document.body.scrollTop + oy - FlyMenu.lastY);
     var d = Math.sqrt(dx * dx + dy * dy);
     var c = Math.round(d / 10);
     var newx = FlyMenu.lastX;
     var newy = FlyMenu.lastY;
    
     if(FlyMenu.halign != '')
     {
         if (document.body.scrollLeft + ox > newx) 
         {
            newx = FlyMenu.lastX + FlyMenu.speed + c;
            if(newx > document.body.scrollLeft + ox)
               newx = document.body.scrollLeft + ox; 
         }
         if (document.body.scrollLeft + ox < newx) 
         {
            newx = FlyMenu.lastX - FlyMenu.speed - c;
            if(newx < document.body.scrollLeft + ox)
               newx = document.body.scrollLeft + ox;               
         }
         if(newx != FlyMenu.lastX)
         {          
           timeout = 30;      
           if(FlyMenu.valign == '')
              DOM_SetPosTop(FlyObj, oy);
           DOM_SetPosLeft(FlyObj, newx);
           FlyMenu.lastX = newx;    
         }           
     }
     
     if(FlyMenu.valign != '')
     {     
         if (document.body.scrollTop + oy > newy) 
         {
            newy = FlyMenu.lastY + FlyMenu.speed + c;
            if(newy > document.body.scrollTop + oy)
               newy = document.body.scrollTop + oy;               
         }
         if (document.body.scrollTop + oy < newy) 
         {
            newy = FlyMenu.lastY - FlyMenu.speed - c;
            if(newy < document.body.scrollTop + oy)
               newy = document.body.scrollTop + oy;                             
         }
         if(newy != FlyMenu.lastY)
         {                     
            timeout = 30;     
            if(FlyMenu.halign == '')
               DOM_SetPosLeft(FlyObj, ox);
            DOM_SetPosTop(FlyObj, newy);
            FlyMenu.lastY = newy;
         }
     }
  }
  
   if(FlyMenu.pos_function)
      eval(FlyMenu.pos_function);   
     
   if (FlyMenu.stop) 
       FlyMenu.stop = false;
   else
       FlyMenu.flymenu_adj = window.setTimeout("TWX_FlyMenuAdjust(" + FlyMenuIndex + ")", timeout); 
}

