﻿//global array of follower onjects, is used to be able to use timers
var followers = [];

function follower(index,elementName,halign,orgtop,delayspeed) {
 
 this.timerID = 0;
 this.elementName = elementName;
 this.element = document.getElementById(elementName);
 this.findex = index;
 followers[this.findex] = this; //ad this object to the global followers array
  
 this.halign= halign;

 if (this.halign == "left") {
  this.floatX = getElementLeft(elementName);
 } else {  
  this.floatX = getViewportWidth()-getElementWidth(elementName);
  this.element.style.left = this.floatX + "px";
 }

 this.orgtop = orgtop;
 
 if ( (this.orgtop + getElementHeight(elementName)) > getViewportHeight() ) {
  this.element.style.top = (getViewportHeight()-getElementHeight(elementName)-30) + "px";    
 } else {
  this.element.style.top = this.orgtop;
 }
 
 this.floatY = getElementTop(elementName);   
 
 
 this.vscroll = scrollVertPresent();

 this.layerwidth = getElementWidth(elementName);
 this.layerheight = getElementHeight(elementName);

 this.lastX=-1;
 this.lastY=-1;
 this.ifloatX = this.floatX;
 this.ifloatY = this.floatY;
 this.delayspeed= delayspeed;
 this.prevWidth = getViewportWidth();
 this.prevHeight = getViewportHeight();
 this.prevSL = getViewportScrollLeft();
 this.prevST = getViewportScrollTop(); 
 
 this.adjust();
 this.firstTime = true;

}

follower.prototype.adjust = function() {

 var curWidth = getViewportWidth();
 var curHeight = getViewportHeight();
 var curSL = getViewportScrollLeft();
 var curST = getViewportScrollTop();
  
 if (curWidth != this.prevWidth || curHeight != this.prevHeight || curSL != this.prevSL || curST != this.prevST || this.firstTime) {
 
  if (this.halign == "left") {
   this.floatX=this.ifloatX;
  } else { //float == right
   if ( ieCheck() || safariCheck() ) { //ie and safari, don't substract pixels for scollbar
    isub = 0;
   } else { //for firefox   
	if (this.vscroll) { isub = 20; }
    else { isub = 0; }	
   } 
   this.floatX = getViewportWidth()-getElementWidth(this.elementName)-isub;
   this.element.style.left = this.floatX + "px";
  }

  this.prevWidth = curWidth;
  this.prevHeight = curHeight;  
  this.prevST = curST;
  this.prevSL = curSL;  

  this.firstTime = false;  
 }

 
 var scTop  = getViewportScrollTop(); 
 var scLeft = getViewportScrollLeft();
  
 if (this.lastX==-1 || this.delayspeed==0)
 {
  this.lastX = scLeft + this.floatX;
  this.lastY = scTop + this.floatY;
 }
 else
 {
  var dx=Math.abs(scLeft+this.floatX-this.lastX);
  var dy=Math.abs(scTop+this.floatY-this.lastY);
  var d=Math.sqrt(dx*dx+dy*dy);
  var c=Math.round(d/10);
  if (scLeft+this.floatX>this.lastX) {this.lastX=this.lastX+this.delayspeed+c;}
  if (scLeft+this.floatX<this.lastX) {this.lastX=this.lastX-this.delayspeed-c;}

  if (scTop+this.floatY>this.lastY) {this.lastY=this.lastY+this.delayspeed+c;}  
  if (scTop+this.floatY<this.lastY) {this.lastY=this.lastY-this.delayspeed-c;}  
  
 }
  
 this.element.style.left   = this.lastX + "px"; 
 this.element.style.top = this.lastY + "px"; 
 
  
 this.timerID = setTimeout('followers['+this.findex+'].adjust()', 50);

}

follower.prototype.resize = function() {
 
 if ( (this.orgtop + getElementHeight(this.elementName)) > getViewportHeight() ) {
  this.element.style.top = (getViewportHeight()-getElementHeight(this.elementName)-30) + "px";    
 } else {
  this.element.style.top = this.orgtop;
 }	 
 
 this.floatY = getElementTop(this.elementName);   
 this.lastY = this.floatY;
 
}