// Fader
//
// Class that handles multiple fading elements simultaneously. Simply
// call Fader's fade method with the appropriate values and the Fader
// object takes care of it. Fade values are given in permilles: 0 is
// completely transparent, 1000 is completely opaque.



/*
 * Class: Fader
 *
 * Object constructor function
 */
function Fader()
{
  this.list=new Object();
  this.list.length=0;
  this.delay=35; // Smaller is smoother, but makes IE choke.


  this.fade=function(id, min, max, step)
  {
    e=document.getElementById(id);
    if (e==null || step==0 || min>=max) return;
    e.fade_min=min;
    e.fade_max=max;
    e.fade_step=Math.ceil((max-min)*this.delay/step);

    // Set initial fade value if not yet set
    if (e.fade_value==null)
      if (step>0) e.fade_value=min;
      else e.fade_value=max;
    if (e.fade_value<min) e.fade_value=min;
    if (e.fade_value>max) e.fade_value=max;

    // Add to Fader's element list
    if (!(id in this.list))
    {
      this.list[id]=e;
      this.list.length++;
      if (this.list.length==1) this.step();
    }
  } // end of this.fade()


  this.step=function()
  {
    for (i in this.list)
    {
      e=document.getElementById(i);
      if (e!=null && e.fade_value!=null)
      {
	e.fade_value+=e.fade_step;
	if (e.fade_step>0)
	{
	  // Fade in
	  e.style.zIndex=12;
	  if (e.fade_value>e.fade_max)
	  {
	    e.fade_value=e.fade_max;
	    delete this.list[i];
	    this.list.length--;
	  }
	}
	else
	{
	  // Fade out
	  e.style.zIndex=11;
	  if (e.fade_value<e.fade_min)
	  {
	    e.fade_value=e.fade_min;
	    delete this.list[i];
	    this.list.length--;
	  }
	}
	if (e.style.opacity!=null) e.style.opacity=e.fade_value/1000;
	if (e.style.filter!=null)
	  if (e.fade_value>=1000) e.style.filter='';
	  else e.style.filter='alpha(opacity='+(e.fade_value/10)+')';
	e.style.visibility=(e.fade_value>0)?'visible':'hidden';
	window.status='filter='+e.style.filter+' : visibility='+e.style.visibility;
	// window.status='filter=';
      }
    }

    if (this.list.length>0) self.setTimeout("fader.step()", this.delay);
  } // end of this.step()
}



/*
 * Fader instance
 */
var fader=new Fader();

