/////////////////////////////////////////////////////////////////////

var FloatLayers          = new Array();
var FloatLayersByName    = new Array();
var FL_StartPositionName = new Array();
var FL_StartPositionX    = new Array();
var FL_StartPositionY    = new Array();
var FL_Index             = 0; 
var footer_stop_floating = 0;


if (navigator.appName=="Netscape")
{
	//footer_stop_floating = 245;
	footer_stop_floating = 170;
}
else
{
	if (navigator.appName.indexOf("Microsoft")!=-1)
	{
	    //footer_stop_floating = 265;
	    footer_stop_floating = 235;
    }
    else
    {
    	footer_stop_floating = 205;
    }
}

function addFloatLayer(n,offX,offY,spd)
{
  new FloatLayer(n,offX,offY,spd);
}

function getFloatLayer(n)
{
  return FloatLayersByName[n];
}

function getFL_StartPositionX(flname)
{
  var startLayerPosX = 0;
  
  for (var i=0;i<FL_StartPositionName.length;i++)
  {
    if (FL_StartPositionName[i] == flname)
    {
        startLayerPosX = FL_StartPositionX[i];
    }
  }
  return startLayerPosX;
}

function getFL_StartPositionY(flname)
{
  var startLayerPosY = 0;
  
  for (var i=0;i<FL_StartPositionName.length;i++)
  {
    if (FL_StartPositionName[i] == flname)
    {
        startLayerPosY = FL_StartPositionY[i];
    }
  }
  return startLayerPosY;
}

function alignFloatLayers()
{
  for(var i=0;i<FloatLayers.length;i++)FloatLayers[i].align();
}

function getXCoord(el) 
{
  x=0;
  while(el)
  {
    x+=el.offsetLeft;
    el=el.offsetParent;
  }
  return x;
}

function getYCoord(el) 
{
    y=0;
    while(el)
    {
      y+=el.offsetTop;
      el=el.offsetParent;
    }
    return y;
}

/////////////////////////////////////////////////////////////////////

FloatLayer.prototype.setFloatToTop=setTopFloater;
FloatLayer.prototype.setFloatToBottom=setBottomFloater;
FloatLayer.prototype.setFloatToLeft=setLeftFloater;
FloatLayer.prototype.setFloatToRight=setRightFloater;
FloatLayer.prototype.initialize=defineFloater;
FloatLayer.prototype.adjust=adjustFloater;
FloatLayer.prototype.align=alignFloater;

function FloatLayer(n, offX, offY, spd) 
{
  this.index=FloatLayers.length;

  FloatLayers.push(this);
  FloatLayersByName[n] = this;

  this.name    = n;
  this.floatX  = 0;
  this.floatY  = 0;
  this.tm      = null;
  this.steps   = spd;
  this.alignHorizontal=(offX>=0) ? leftFloater : rightFloater;
  this.alignVertical  =(offY>=0) ? topFloater : bottomFloater;
  this.ifloatX = Math.abs(offX);
  this.ifloatY = Math.abs(offY);
}

/////////////////////////////////////////////////////////////////////

function defineFloater()
{
  this.layer  = document.getElementById(this.name);
  this.width  = this.layer.offsetWidth;
  this.height = this.layer.offsetHeight;
  this.prevX  = this.layer.offsetLeft;
  this.prevY  = this.layer.offsetTop;
  
  FL_StartPositionX.push(Math.abs(this.prevX));
  FL_Index    = FL_StartPositionY.push(Math.abs(this.prevY));
  FL_StartPositionName.push(this.name);
  //document.getElementById('oLayPosX').innerHTML = getFL_StartPositionX(this.name);
  //document.getElementById('oLayPosY').innerHTML = getFL_StartPositionY(this.name);
  //document.getElementById('obodyheight').innerHTML = document.body.clientHeight;
  //document.getElementById('odocheight').innerHTML = $(document).height();
}

function adjustFloater() 
{
  this.tm=null;
  if(this.layer.style.position!='absolute')return;

  var dx = Math.abs(this.floatX-this.prevX);
  var dy = Math.abs(this.floatY-this.prevY);

  if (dx < this.steps/2)
      cx = (dx>=1) ? 1 : 0;
  else
      cx = Math.round(dx/this.steps);

  if (dy < this.steps/2)
      cy = (dy>=1) ? 1 : 0;
  else
      cy = Math.round(dy/this.steps);

  if (this.floatX > this.prevX)
      this.prevX += cx;
  else if (this.floatX < this.prevX)
      this.prevX -= cx;

  if (this.floatY > this.prevY)
      this.prevY += cy;
  else if (this.floatY < this.prevY)
      this.prevY -= cy;

  this.layer.style.left = this.prevX;
  this.layer.style.top  = this.prevY;

  if (cx!=0||cy!=0)
  {
     if(this.tm==null)this.tm=setTimeout('FloatLayers['+this.index+'].adjust()',50);
  }
  else
     alignFloatLayers();
}

function setLeftFloater()
{
  this.alignHorizontal=leftFloater;
}

function setRightFloater()
{
  this.alignHorizontal=rightFloater;
}

function setTopFloater()
{
  this.alignVertical=topFloater;
}

function setBottomFloater()
{
  this.alignVertical=bottomFloater;
}

function leftFloater()
{
  if (getFL_StartPositionX(this.name) < document.body.scrollLeft + this.ifloatX)
  {
     this.floatX = getFL_StartPositionX(this.name);
  }
  else 
  {
     if ($(document).width() > (document.body.scrollLeft + document.body.clientWidth - this.ifloatX - this.width))
     {
         this.floatX = document.body.scrollLeft + this.ifloatX;
     }
     else
     {
         this.floatX = this.prevX;
     }
  }  

  //document.getElementById('oX').innerHTML = this.floatX;  
}

function topFloater()
{
  if (getFL_StartPositionY(this.name) > document.body.scrollTop + this.ifloatY)
  {
     this.floatY = getFL_StartPositionY(this.name);
  }
  else
  {
     if ($(document).height() > (document.body.scrollTop + this.ifloatY + this.height + footer_stop_floating))
     {
         this.floatY = document.body.scrollTop + this.ifloatY;
     }
     else
     {
         this.floatY = this.prevY;
     }
  }  
  
  FL_Index = document.body.scrollTop + this.ifloatY + this.height + footer_stop_floating;
  //document.getElementById('oY').innerHTML = this.floatY;
  //document.getElementById('olayY').innerHTML = this.layer.offsetTop;
  //document.getElementById('obodyheight').innerHTML = document.body.clientHeight;
  //document.getElementById('obodyY').innerHTML = document.body.scrollTop;
  //document.getElementById('oLayPosX').innerHTML = getFL_StartPositionX(this.name);
  //document.getElementById('oLayPosY').innerHTML = getFL_StartPositionY(this.name);
  //document.getElementById('odocheight').innerHTML = $(document).height();
  //document.getElementById('obodyBottom').innerHTML = FL_Index;
  //document.getElementById('obodyBottomY').innerHTML = FL_Index;  
}

function rightFloater()
{
  if (getFL_StartPositionX(this.name) < document.body.scrollLeft + this.ifloatX)
  {
     this.floatX = getFL_StartPositionX(this.name);
  }
  else 
  {
     if ($(document).width() > (document.body.scrollLeft + document.body.clientWidth - this.ifloatX - this.width))
     {
         this.floatX = document.body.scrollLeft + document.body.clientWidth - this.ifloatX - this.width;
     }
     else
     {
         this.floatX = this.prevX;
     }
  }  
  
  //document.getElementById('oX').innerHTML = this.floatX;
}

function bottomFloater()
{
  if (getFL_StartPositionY(this.name) > document.body.scrollTop + this.ifloatY)
  {
     this.floatY = getFL_StartPositionY(this.name) + this.layer.offsetHeight;
  }
  else
  {
     if ($(document).height() > (document.body.scrollTop + this.ifloatY + this.height + footer_stop_floating))
     {
         this.floatY = document.body.scrollTop + document.body.clientHeight - this.ifloatY - this.height;
     }
     else
     {
         this.floatY = this.prevY;
     }
  }  

  FL_Index = document.body.scrollTop + this.ifloatY + this.height + footer_stop_floating;
  //document.getElementById('oY').innerHTML = this.floatY;
  //document.getElementById('olayY').innerHTML = this.layer.offsetTop;
  //document.getElementById('obodyheight').innerHTML = document.body.clientHeight;
  //document.getElementById('obodyY').innerHTML = document.body.scrollTop;
  //document.getElementById('oLayPosX').innerHTML = getFL_StartPositionX(this.name);
  //document.getElementById('oLayPosY').innerHTML = getFL_StartPositionY(this.name);
  //document.getElementById('odocheight').innerHTML = $(document).height();
  //document.getElementById('obodyBottom').innerHTML = FL_Index;
  //document.getElementById('obodyBottomY').innerHTML = FL_Index;
}

function alignFloater()
{
  if(this.layer==null)this.initialize();
  this.alignHorizontal();
  this.alignVertical();
  if(this.prevX!=this.floatX || this.prevY!=this.floatY)
  {
    if(this.tm==null)this.tm=setTimeout('FloatLayers['+this.index+'].adjust()',50);
  }
}
